1 16 17 package org.apache.log4j.net; 18 19 import org.apache.log4j.AppenderSkeleton; 20 import org.apache.log4j.spi.LoggingEvent; 21 import org.apache.log4j.spi.ErrorCode; 22 import org.apache.log4j.helpers.LogLog; 23 24 import java.util.Properties ; 25 import javax.jms.TopicConnection ; 26 import javax.jms.TopicConnectionFactory ; 27 import javax.jms.Topic ; 28 import javax.jms.TopicPublisher ; 29 import javax.jms.TopicSession ; 30 import javax.jms.Session ; 31 import javax.jms.ObjectMessage ; 32 import javax.naming.InitialContext ; 33 import javax.naming.Context ; 34 import javax.naming.NameNotFoundException ; 35 import javax.naming.NamingException ; 36 37 100 public class JMSAppender extends AppenderSkeleton { 101 102 String securityPrincipalName; 103 String securityCredentials; 104 String initialContextFactoryName; 105 String urlPkgPrefixes; 106 String providerURL; 107 String topicBindingName; 108 String tcfBindingName; 109 String userName; 110 String password; 111 boolean locationInfo; 112 113 TopicConnection topicConnection; 114 TopicSession topicSession; 115 TopicPublisher topicPublisher; 116 117 public 118 JMSAppender() { 119 } 120 121 126 public 127 void setTopicConnectionFactoryBindingName(String tcfBindingName) { 128 this.tcfBindingName = tcfBindingName; 129 } 130 131 134 public 135 String getTopicConnectionFactoryBindingName() { 136 return tcfBindingName; 137 } 138 139 144 public 145 void setTopicBindingName(String topicBindingName) { 146 this.topicBindingName = topicBindingName; 147 } 148 149 152 public 153 String getTopicBindingName() { 154 return topicBindingName; 155 } 156 157 158 162 public 163 boolean getLocationInfo() { 164 return locationInfo; 165 } 166 167 170 public void activateOptions() { 171 TopicConnectionFactory topicConnectionFactory; 172 173 try { 174 Context jndi; 175 176 LogLog.debug("Getting initial context."); 177 if(initialContextFactoryName != null) { 178 Properties env = new Properties ( ); 179 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName); 180 if(providerURL != null) { 181 env.put(Context.PROVIDER_URL, providerURL); 182 } else { 183 LogLog.warn("You have set InitialContextFactoryName option but not the " 184 +"ProviderURL. This is likely to cause problems."); 185 } 186 if(urlPkgPrefixes != null) { 187 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes); 188 } 189 190 if(securityPrincipalName != null) { 191 env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName); 192 if(securityCredentials != null) { 193 env.put(Context.SECURITY_CREDENTIALS, securityCredentials); 194 } else { 195 LogLog.warn("You have set SecurityPrincipalName option but not the " 196 +"SecurityCredentials. This is likely to cause problems."); 197 } 198 } 199 jndi = new InitialContext (env); 200 } else { 201 jndi = new InitialContext (); 202 } 203 204 LogLog.debug("Looking up ["+tcfBindingName+"]"); 205 topicConnectionFactory = (TopicConnectionFactory ) lookup(jndi, tcfBindingName); 206 LogLog.debug("About to create TopicConnection."); 207 if(userName != null) { 208 topicConnection = topicConnectionFactory.createTopicConnection(userName, 209 password); 210 } else { 211 topicConnection = topicConnectionFactory.createTopicConnection(); 212 } 213 214 LogLog.debug("Creating TopicSession, non-transactional, " 215 +"in AUTO_ACKNOWLEDGE mode."); 216 topicSession = topicConnection.createTopicSession(false, 217 Session.AUTO_ACKNOWLEDGE); 218 219 LogLog.debug("Looking up topic name ["+topicBindingName+"]."); 220 Topic topic = (Topic ) lookup(jndi, topicBindingName); 221 222 LogLog.debug("Creating TopicPublisher."); 223 topicPublisher = topicSession.createPublisher(topic); 224 225 LogLog.debug("Starting TopicConnection."); 226 topicConnection.start(); 227 228 jndi.close(); 229 } catch(Exception e) { 230 errorHandler.error("Error while activating options for appender named ["+name+ 231 "].", e, ErrorCode.GENERIC_FAILURE); 232 } 233 } 234 235 protected Object lookup(Context ctx, String name) throws NamingException { 236 try { 237 return ctx.lookup(name); 238 } catch(NameNotFoundException e) { 239 LogLog.error("Could not find name ["+name+"]."); 240 throw e; 241 } 242 } 243 244 protected boolean checkEntryConditions() { 245 String fail = null; 246 247 if(this.topicConnection == null) { 248 fail = "No TopicConnection"; 249 } else if(this.topicSession == null) { 250 fail = "No TopicSession"; 251 } else if(this.topicPublisher == null) { 252 fail = "No TopicPublisher"; 253 } 254 255 if(fail != null) { 256 errorHandler.error(fail +" for JMSAppender named ["+name+"]."); 257 return false; 258 } else { 259 return true; 260 } 261 } 262 263 266 public synchronized void close() { 267 269 if(this.closed) 270 return; 271 272 LogLog.debug("Closing appender ["+name+"]."); 273 this.closed = true; 274 275 try { 276 if(topicSession != null) 277 topicSession.close(); 278 if(topicConnection != null) 279 topicConnection.close(); 280 } catch(Exception e) { 281 LogLog.error("Error while closing JMSAppender ["+name+"].", e); 282 } 283 topicPublisher = null; 285 topicSession = null; 286 topicConnection = null; 287 } 288 289 292 public void append(LoggingEvent event) { 293 if(!checkEntryConditions()) { 294 return; 295 } 296 297 try { 298 ObjectMessage msg = topicSession.createObjectMessage(); 299 if(locationInfo) { 300 event.getLocationInformation(); 301 } 302 msg.setObject(event); 303 topicPublisher.publish(msg); 304 } catch(Exception e) { 305 errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e, 306 ErrorCode.GENERIC_FAILURE); 307 } 308 } 309 310 315 public String getInitialContextFactoryName() { 316 return initialContextFactoryName; 317 } 318 319 328 public void setInitialContextFactoryName(String initialContextFactoryName) { 329 this.initialContextFactoryName = initialContextFactoryName; 330 } 331 332 public String getProviderURL() { 333 return providerURL; 334 } 335 336 public void setProviderURL(String providerURL) { 337 this.providerURL = providerURL; 338 } 339 340 String getURLPkgPrefixes( ) { 341 return urlPkgPrefixes; 342 } 343 344 public void setURLPkgPrefixes(String urlPkgPrefixes ) { 345 this.urlPkgPrefixes = urlPkgPrefixes; 346 } 347 348 public String getSecurityCredentials() { 349 return securityCredentials; 350 } 351 352 public void setSecurityCredentials(String securityCredentials) { 353 this.securityCredentials = securityCredentials; 354 } 355 356 357 public String getSecurityPrincipalName() { 358 return securityPrincipalName; 359 } 360 361 public void setSecurityPrincipalName(String securityPrincipalName) { 362 this.securityPrincipalName = securityPrincipalName; 363 } 364 365 public String getUserName() { 366 return userName; 367 } 368 369 376 public void setUserName(String userName) { 377 this.userName = userName; 378 } 379 380 public String getPassword() { 381 return password; 382 } 383 384 387 public void setPassword(String password) { 388 this.password = password; 389 } 390 391 392 396 public void setLocationInfo(boolean locationInfo) { 397 this.locationInfo = locationInfo; 398 } 399 400 401 405 public boolean requiresLayout() { 406 return false; 407 } 408 } 409 | Popular Tags |