1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.text.*; 57 58 import javax.jms.*; 59 import javax.naming.*; 60 61 import com.protomatter.util.*; 62 63 75 public class JMSLog 76 extends BasicLogger 77 implements JMSConstants 78 { 79 private String topicName = null; 80 private String factoryName = null; 81 private String user = null; 82 private String pass = null; 83 84 private boolean persistent = false; 85 private int priority = 5; 86 private int ttl = 30 * 60 * 1000; 88 private TopicSession session = null; 89 private TopicPublisher publisher = null; 90 private boolean running = false; 91 92 96 public JMSLog() 97 { 98 super(); 99 } 100 101 104 public int getPriority() 105 { 106 return this.priority; 107 } 108 111 public void setPriority(int priority) 112 { 113 this.ttl = priority; 114 } 115 116 119 public int getTTL() 120 { 121 return this.ttl; 122 } 123 126 public void setTTL(int ttl) 127 { 128 this.ttl = ttl; 129 } 130 131 134 public boolean getPersistent() 135 { 136 return this.persistent; 137 } 138 141 public void setPersistent(boolean persistent) 142 { 143 this.persistent = persistent; 144 } 145 146 149 public String getTopicName() 150 { 151 return this.topicName; 152 } 153 156 public void setTopicName(String topicName) 157 { 158 this.topicName = topicName; 159 } 160 161 164 public String getFactoryName() 165 { 166 return this.factoryName; 167 } 168 171 public void setFactoryName(String factoryName) 172 { 173 this.factoryName = factoryName; 174 } 175 176 180 public String getUser() 181 { 182 return this.user; 183 } 184 188 public void setUser(String user) 189 { 190 this.user = user; 191 } 192 193 197 public String getPass() 198 { 199 return this.pass; 200 } 201 205 public void setPass(String pass) 206 { 207 this.pass = pass; 208 } 209 210 277 public final void log(SyslogMessage sm) 278 { 279 if (!running) 280 return; 281 282 try 283 { 284 String text = null; 285 if (sm.detail == null) 286 { 287 text = ""; 288 } 289 else 290 { 291 StringBuffer b = new StringBuffer (256); 292 formatter.formatMessageDetail(b, sm); 293 text = b.toString(); 294 } 295 String ip = sm.host.getHostAddress(); 296 String msg = (sm.msg == null) ? "" : sm.msg.toString(); 297 String threadName = (sm.thread != null) ? sm.thread.getName() : ""; 298 299 synchronized(session) 302 { 303 TextMessage m = session.createTextMessage(); 304 if (text != null) 305 m.setText(text); 306 307 m.setStringProperty(JMS_PROP_MSG_TYPE, JMS_PROP_MSG_TYPE_VALUE); 308 309 m.setStringProperty(JMS_PROP_HOST, ip); 310 m.setLongProperty(JMS_PROP_TIME, sm.time); 311 m.setStringProperty(JMS_PROP_CHANNEL, sm.channel); 312 m.setStringProperty(JMS_PROP_LOGGER, sm.loggerClassname); 313 m.setStringProperty(JMS_PROP_MESSAGE, msg); 314 m.setIntProperty(JMS_PROP_LEVEL, sm.level); 315 m.setStringProperty(JMS_PROP_THREAD, threadName); 316 317 publisher.publish(m); 318 } 319 } 320 catch (JMSException x) 321 { 322 System.err.println("############################################################"); 323 System.err.println(MessageFormat.format( 324 Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_MESSAGE_1), 325 new Object [] { "JMSLog" })); 326 System.err.println(MessageFormat.format( 327 Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_MESSAGE_2), 328 new Object [] { getName() } )); 329 System.err.println(""); 330 System.err.println(Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_CAUSE_MESSAGE)); 331 x.printStackTrace(); 332 System.err.println(""); 333 System.err.println(Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_ORIGINAL_MESSAGE)); 334 StringBuffer b = new StringBuffer (256); 335 (new SimpleSyslogTextFormatter()).formatLogEntry(b, sm); 336 System.err.println(b); 337 System.err.println("############################################################"); 338 } 339 } 340 341 344 public void shutdown() 345 { 346 this.running = false; 347 } 348 349 public void start() 350 { 351 try 352 { 353 Context ctx = new InitialContext(); 355 356 Topic topic = (Topic)ctx.lookup(topicName); 357 358 TopicConnectionFactory tcf = 359 (TopicConnectionFactory)ctx.lookup(factoryName); 360 361 TopicConnection connection = null; 362 if (user != null) 363 connection = tcf.createTopicConnection(user, pass); 364 else 365 connection = tcf.createTopicConnection(); 366 367 session = 368 connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 369 370 connection.start(); 371 372 publisher = session.createPublisher(topic); 373 publisher.setPriority(this.priority); 374 publisher.setTimeToLive(this.ttl); 375 publisher.setDeliveryMode(this.persistent 376 ? DeliveryMode.PERSISTENT 377 : DeliveryMode.NON_PERSISTENT); 378 379 running = true; 380 } 381 catch (NamingException nx) 382 { 383 throw new ChainedRuntimeException(MessageFormat.format( 384 Syslog.getResourceString(MessageConstants.JMS_NAMING_EXCEPITON_MESSAGE), 385 new Object [] { "NamingException", "JMSLog" }), nx); 386 } 387 catch (JMSException jx) 388 { 389 throw new ChainedRuntimeException(MessageFormat.format( 390 Syslog.getResourceString(MessageConstants.JMS_JMS_EXCEPITON_MESSAGE), 391 new Object [] { "JMSException", "JMSLog" }), jx); 392 } 393 } 394 395 public void flush() 396 { 397 } 399 } 400 | Popular Tags |