1 18 package org.apache.activemq.util; 19 20 import org.apache.log4j.helpers.LogLog; 21 22 import javax.jms.Connection ; 23 import javax.jms.ConnectionFactory ; 24 import javax.jms.JMSException ; 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import java.util.Hashtable ; 29 30 36 public class JndiJmsLogAppender extends JmsLogAppenderSupport { 37 38 private String jndiName; 39 private String userName; 40 private String password; 41 42 private String initialContextFactoryName; 43 private String providerURL; 44 private String urlPkgPrefixes; 45 private String securityPrincipalName; 46 private String securityCredentials; 47 48 public JndiJmsLogAppender() { 49 } 50 51 public String getJndiName() { 52 return jndiName; 53 } 54 55 public void setJndiName(String jndiName) { 56 this.jndiName = jndiName; 57 } 58 59 public String getUserName() { 60 return userName; 61 } 62 63 public void setUserName(String userName) { 64 this.userName = userName; 65 } 66 67 public String getPassword() { 68 return password; 69 } 70 71 public void setPassword(String password) { 72 this.password = password; 73 } 74 75 76 public String getInitialContextFactoryName() { 79 return initialContextFactoryName; 80 } 81 82 public void setInitialContextFactoryName(String initialContextFactoryName) { 83 this.initialContextFactoryName = initialContextFactoryName; 84 } 85 86 public String getProviderURL() { 87 return providerURL; 88 } 89 90 public void setProviderURL(String providerURL) { 91 this.providerURL = providerURL; 92 } 93 94 public String getUrlPkgPrefixes() { 95 return urlPkgPrefixes; 96 } 97 98 public void setUrlPkgPrefixes(String urlPkgPrefixes) { 99 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 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 } 168 | Popular Tags |