001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.util;
019    
020    import org.apache.log4j.helpers.LogLog;
021    
022    import javax.jms.Connection;
023    import javax.jms.ConnectionFactory;
024    import javax.jms.JMSException;
025    import javax.naming.Context;
026    import javax.naming.InitialContext;
027    import javax.naming.NamingException;
028    import java.util.Hashtable;
029    
030    /**
031     * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory
032     * to use for logging events.
033     *
034     * @version $Revision: 1.1.1.1 $
035     */
036    public class JndiJmsLogAppender extends JmsLogAppenderSupport {
037    
038        private String jndiName;
039        private String userName;
040        private String password;
041    
042        private String initialContextFactoryName;
043        private String providerURL;
044        private String urlPkgPrefixes;
045        private String securityPrincipalName;
046        private String securityCredentials;
047    
048        public JndiJmsLogAppender() {
049        }
050    
051        public String getJndiName() {
052            return jndiName;
053        }
054    
055        public void setJndiName(String jndiName) {
056            this.jndiName = jndiName;
057        }
058    
059        public String getUserName() {
060            return userName;
061        }
062    
063        public void setUserName(String userName) {
064            this.userName = userName;
065        }
066    
067        public String getPassword() {
068            return password;
069        }
070    
071        public void setPassword(String password) {
072            this.password = password;
073        }
074    
075    
076        // to customize the JNDI context
077        //-------------------------------------------------------------------------
078        public String getInitialContextFactoryName() {
079            return initialContextFactoryName;
080        }
081    
082        public void setInitialContextFactoryName(String initialContextFactoryName) {
083            this.initialContextFactoryName = initialContextFactoryName;
084        }
085    
086        public String getProviderURL() {
087            return providerURL;
088        }
089    
090        public void setProviderURL(String providerURL) {
091            this.providerURL = providerURL;
092        }
093    
094        public String getUrlPkgPrefixes() {
095            return urlPkgPrefixes;
096        }
097    
098        public void setUrlPkgPrefixes(String urlPkgPrefixes) {
099            this.urlPkgPrefixes = urlPkgPrefixes;
100        }
101    
102        public String getSecurityPrincipalName() {
103            return securityPrincipalName;
104        }
105    
106        public void setSecurityPrincipalName(String securityPrincipalName) {
107            this.securityPrincipalName = securityPrincipalName;
108        }
109    
110        public String getSecurityCredentials() {
111            return securityCredentials;
112        }
113    
114        public void setSecurityCredentials(String securityCredentials) {
115            this.securityCredentials = securityCredentials;
116        }
117    
118        // Implementation methods
119        //-------------------------------------------------------------------------
120        protected Connection createConnection() throws JMSException, NamingException {
121            InitialContext context = createInitialContext();
122            LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
123            ConnectionFactory factory = (ConnectionFactory) context.lookup(jndiName);
124            if (factory == null) {
125                throw new JMSException("No such ConnectionFactory for name: " + jndiName);
126            }
127            if (userName != null) {
128                return factory.createConnection(userName, password);
129            }
130            else {
131                return factory.createConnection();
132            }
133        }
134    
135        protected InitialContext createInitialContext() throws NamingException {
136            if (initialContextFactoryName == null) {
137                return new InitialContext();
138            }
139            else {
140                Hashtable env = new Hashtable();
141                env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
142                if (providerURL != null) {
143                    env.put(Context.PROVIDER_URL, providerURL);
144                }
145                else {
146                    LogLog.warn("You have set InitialContextFactoryName option but not the "
147                            + "ProviderURL. This is likely to cause problems.");
148                }
149                if (urlPkgPrefixes != null) {
150                    env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
151                }
152    
153                if (securityPrincipalName != null) {
154                    env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
155                    if (securityCredentials != null) {
156                        env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
157                    }
158                    else {
159                        LogLog.warn("You have set SecurityPrincipalName option but not the "
160                                + "SecurityCredentials. This is likely to cause problems.");
161                    }
162                }
163                LogLog.debug("Looking up JNDI context with environment: " + env);
164                return new InitialContext(env);
165            }
166        }
167    }