001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.net.URISyntaxException;
020import javax.jms.Connection;
021import javax.jms.JMSException;
022
023import org.apache.activemq.ActiveMQConnection;
024
025/**
026 * A JMS 1.1 log4j appender which uses ActiveMQ by default and does not require
027 * any JNDI configurations
028 * 
029 * 
030 */
031public class JmsLogAppender extends JmsLogAppenderSupport {
032    private String uri = "tcp://localhost:61616";
033    private String userName;
034    private String password;
035
036    public JmsLogAppender() {
037    }
038
039    public String getUri() {
040        return uri;
041    }
042
043    public void setUri(String uri) {
044        this.uri = uri;
045    }
046
047    public String getUserName() {
048        return userName;
049    }
050
051    public void setUserName(String userName) {
052        this.userName = userName;
053    }
054
055    public String getPassword() {
056        return password;
057    }
058
059    public void setPassword(String password) {
060        this.password = password;
061    }
062
063    protected Connection createConnection() throws JMSException {
064        if (userName != null) {
065            try {
066                return ActiveMQConnection.makeConnection(userName, password, uri);
067            } catch (URISyntaxException e) {
068                throw new JMSException("Unable to connect to a broker using " + "userName: \'" + userName + "\' password \'" + password + "\' uri \'" + uri + "\' :: error - " + e.getMessage());
069            }
070        } else {
071            try {
072                return ActiveMQConnection.makeConnection(uri);
073            } catch (URISyntaxException e) {
074                throw new JMSException("Unable to connect to a broker using " + "uri \'" + uri + "\' :: error - " + e.getMessage());
075            }
076        }
077    }
078}