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     */
017    package org.apache.activemq.util;
018    
019    import java.net.URISyntaxException;
020    
021    import javax.jms.Connection;
022    import javax.jms.JMSException;
023    
024    import org.apache.activemq.ActiveMQConnection;
025    
026    /**
027     * A JMS 1.1 log4j appender which uses ActiveMQ by default and does not require
028     * any JNDI configurations
029     * 
030     * @version $Revision: 565003 $
031     */
032    public class JmsLogAppender extends JmsLogAppenderSupport {
033        private String uri = "tcp://localhost:61616";
034        private String userName;
035        private String password;
036    
037        public JmsLogAppender() {
038        }
039    
040        public String getUri() {
041            return uri;
042        }
043    
044        public void setUri(String uri) {
045            this.uri = uri;
046        }
047    
048        public String getUserName() {
049            return userName;
050        }
051    
052        public void setUserName(String userName) {
053            this.userName = userName;
054        }
055    
056        public String getPassword() {
057            return password;
058        }
059    
060        public void setPassword(String password) {
061            this.password = password;
062        }
063    
064        protected Connection createConnection() throws JMSException {
065            if (userName != null) {
066                try {
067                    return ActiveMQConnection.makeConnection(userName, password, uri);
068                } catch (URISyntaxException e) {
069                    throw new JMSException("Unable to connect to a broker using " + "userName: \'" + userName + "\' password \'" + password + "\' uri \'" + uri + "\' :: error - " + e.getMessage());
070                }
071            } else {
072                try {
073                    return ActiveMQConnection.makeConnection(uri);
074                } catch (URISyntaxException e) {
075                    throw new JMSException("Unable to connect to a broker using " + "uri \'" + uri + "\' :: error - " + e.getMessage());
076                }
077            }
078        }
079    }