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.util.Hashtable; 020 021 import javax.jms.Connection; 022 import javax.jms.ConnectionFactory; 023 import javax.jms.JMSException; 024 import javax.naming.Context; 025 import javax.naming.InitialContext; 026 import javax.naming.NamingException; 027 028 import org.apache.log4j.helpers.LogLog; 029 030 /** 031 * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory to 032 * use for logging events. 033 * 034 * @version $Revision: 565003 $ 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 // to customize the JNDI context 076 // ------------------------------------------------------------------------- 077 public String getInitialContextFactoryName() { 078 return initialContextFactoryName; 079 } 080 081 public void setInitialContextFactoryName(String initialContextFactoryName) { 082 this.initialContextFactoryName = initialContextFactoryName; 083 } 084 085 public String getProviderURL() { 086 return providerURL; 087 } 088 089 public void setProviderURL(String providerURL) { 090 this.providerURL = providerURL; 091 } 092 093 public String getUrlPkgPrefixes() { 094 return urlPkgPrefixes; 095 } 096 097 public void setUrlPkgPrefixes(String urlPkgPrefixes) { 098 this.urlPkgPrefixes = urlPkgPrefixes; 099 } 100 101 public String getSecurityPrincipalName() { 102 return securityPrincipalName; 103 } 104 105 public void setSecurityPrincipalName(String securityPrincipalName) { 106 this.securityPrincipalName = securityPrincipalName; 107 } 108 109 public String getSecurityCredentials() { 110 return securityCredentials; 111 } 112 113 public void setSecurityCredentials(String securityCredentials) { 114 this.securityCredentials = securityCredentials; 115 } 116 117 // Implementation methods 118 // ------------------------------------------------------------------------- 119 protected Connection createConnection() throws JMSException, NamingException { 120 InitialContext context = createInitialContext(); 121 LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName); 122 ConnectionFactory factory = (ConnectionFactory)context.lookup(jndiName); 123 if (factory == null) { 124 throw new JMSException("No such ConnectionFactory for name: " + jndiName); 125 } 126 if (userName != null) { 127 return factory.createConnection(userName, password); 128 } else { 129 return factory.createConnection(); 130 } 131 } 132 133 protected InitialContext createInitialContext() throws NamingException { 134 if (initialContextFactoryName == null) { 135 return new InitialContext(); 136 } else { 137 Hashtable<String, String> env = new Hashtable<String, String>(); 138 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName); 139 if (providerURL != null) { 140 env.put(Context.PROVIDER_URL, providerURL); 141 } else { 142 LogLog.warn("You have set InitialContextFactoryName option but not the " + "ProviderURL. This is likely to cause problems."); 143 } 144 if (urlPkgPrefixes != null) { 145 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes); 146 } 147 148 if (securityPrincipalName != null) { 149 env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName); 150 if (securityCredentials != null) { 151 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 152 } else { 153 LogLog.warn("You have set SecurityPrincipalName option but not the " + "SecurityCredentials. This is likely to cause problems."); 154 } 155 } 156 LogLog.debug("Looking up JNDI context with environment: " + env); 157 return new InitialContext(env); 158 } 159 } 160 }