| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.io.CharArrayWriter; |
| 36 | | | import java.io.PrintWriter; |
| 37 | | | import java.lang.ref.WeakReference; |
| 38 | | | import java.security.AccessController; |
| 39 | | | import java.security.PrivilegedActionException; |
| 40 | | | import java.security.PrivilegedExceptionAction; |
| 41 | | | import java.util.ArrayList; |
| 42 | | | import java.util.List; |
| 43 | | | import java.util.logging.ErrorManager; |
| 44 | | | import java.util.logging.Filter; |
| 45 | | | import java.util.logging.Formatter; |
| 46 | | | import java.util.logging.Handler; |
| 47 | | | import java.util.logging.LogManager; |
| 48 | | | import java.util.logging.LogRecord; |
| 49 | | | |
| 50 | | | import javax.jms.JMSException; |
| 51 | | | import javax.jms.Queue; |
| 52 | | | import javax.jms.QueueConnection; |
| 53 | | | import javax.jms.QueueConnectionFactory; |
| 54 | | | import javax.jms.QueueSender; |
| 55 | | | import javax.jms.QueueSession; |
| 56 | | | import javax.jms.Session; |
| 57 | | | import javax.jms.TextMessage; |
| 58 | | | import javax.naming.Context; |
| 59 | | | import javax.naming.InitialContext; |
| 60 | | | import javax.naming.NamingException; |
| 61 | | | |
| 62 | | | import org.jcoderz.commons.LoggableImpl; |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | <br> |
| 69 | | | |
| 70 | | | <code></code><br> |
| 71 | | | <br><br> |
| 72 | | | |
| 73 | | | <code></code><br> |
| 74 | | | <br><br> |
| 75 | | | |
| 76 | | | <code></code><br> |
| 77 | | | |
| 78 | | | |
| 79 | | | <br><br> |
| 80 | | | |
| 81 | | | <code></code><br> |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | (1) | public class JmsHandler |
| 89 | | | extends Handler |
| 90 | | | { |
| 91 | 0 | | private static final String CLASSNAME = JmsHandler.class.getName(); |
| 92 | 0 | | private static final String CLIENT_ID |
| 93 | | | = "JmsHandler@" + LoggableImpl.INSTANCE_ID; |
| 94 | | | |
| 95 | 0 | | private static final String JMS_FACTORY_NAME_PROPERTY |
| 96 | | | = CLASSNAME + "." + "factory"; |
| 97 | 0 | | private static final String JMS_QUEUE_NAME_PROPERTY |
| 98 | | | = CLASSNAME + "." + "queue"; |
| 99 | 0 | | private static final String JMS_FILTER_PROPERTY |
| 100 | | | = CLASSNAME + "." + "filter"; |
| 101 | 0 | | private static final String JMS_FORMATTER_PROPERTY |
| 102 | | | = CLASSNAME + "." + "formatter"; |
| 103 | | | |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | private static final boolean SESSION_TRANSACTION_MODE = false; |
| 109 | | | |
| 110 | | | |
| 111 | | | |
| 112 | | | |
| 113 | | | private static final int SESSION_ACKNOWLEDGE_MODE = Session.AUTO_ACKNOWLEDGE; |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | |
| 120 | 0 | | private final ThreadLocal mJmsSessions = new ThreadLocal(); |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | 0 | | private final ThreadLocal mJmsSenders = new ThreadLocal(); |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | 0 | | private final List mAllSessions = new ArrayList(); |
| 135 | | | |
| 136 | 0 | | private final LogManager mManager = LogManager.getLogManager(); |
| 137 | | | |
| 138 | 0 | | private QueueConnection mJmsConnection = null; |
| 139 | 0 | | private Queue mJmsQueue = null; |
| 140 | | | |
| 141 | | | private Context mContext; |
| 142 | | | |
| 143 | | | private String mFactoryName; |
| 144 | | | private String mQueueName; |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | {@link } |
| 151 | | | |
| 152 | | | |
| 153 | | | |
| 154 | | | private static final class DefaultFormatter |
| 155 | | | extends Formatter |
| 156 | | | { |
| 157 | | | |
| 158 | 0 | | private final ThreadLocal mPrintWriters = new ThreadLocal(); |
| 159 | | | |
| 160 | 0 | | private final ThreadLocal mCharWriters = new ThreadLocal(); |
| 161 | | | |
| 162 | | | private final XmlPrinter mXmlPrinter; |
| 163 | | | private final DisplayOptions mDisplayOptions; |
| 164 | | | |
| 165 | | | private DefaultFormatter () |
| 166 | | | throws InstantiationException |
| 167 | 0 | | { |
| 168 | 0 | | mXmlPrinter = new XmlPrinter(); |
| 169 | 0 | (2) | mDisplayOptions = new DisplayOptions(); |
| 170 | 0 | | mDisplayOptions.displayMessageStackTrace(false); |
| 171 | 0 | | mDisplayOptions.displayStackTrace(false); |
| 172 | 0 | | mXmlPrinter.setDisplayOptions(mDisplayOptions); |
| 173 | 0 | | } |
| 174 | | | |
| 175 | | | {@inheritDoc} |
| 176 | | | public String format (LogRecord record) |
| 177 | | | { |
| 178 | 0 | | final CharArrayWriter writer = getCharWriter(); |
| 179 | 0 | | writer.reset(); |
| 180 | 0 | | final PrintWriter printer = getPrintWriter(); |
| 181 | 0 | | mXmlPrinter.print(printer, new LogElement(record)); |
| 182 | 0 | | return writer.toString(); |
| 183 | | | } |
| 184 | | | |
| 185 | | | private PrintWriter getPrintWriter () |
| 186 | | | { |
| 187 | 0 | | PrintWriter rc = (PrintWriter) mPrintWriters.get(); |
| 188 | 0 | | if (rc == null) |
| 189 | | | { |
| 190 | 0 | | final CharArrayWriter cw = getCharWriter(); |
| 191 | 0 | | rc = new PrintWriter(cw); |
| 192 | 0 | | mPrintWriters.set(rc); |
| 193 | | | } |
| 194 | 0 | | return rc; |
| 195 | | | } |
| 196 | | | |
| 197 | | | private CharArrayWriter getCharWriter () |
| 198 | | | { |
| 199 | 0 | | CharArrayWriter rc = (CharArrayWriter) mCharWriters.get(); |
| 200 | 0 | | if (rc == null) |
| 201 | | | { |
| 202 | 0 | | rc = new CharArrayWriter(); |
| 203 | 0 | | mCharWriters.set(rc); |
| 204 | | | } |
| 205 | 0 | | return rc; |
| 206 | | | } |
| 207 | | | } |
| 208 | | | |
| 209 | | | |
| 210 | | | |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | | | |
| 215 | | | @throws |
| 216 | | | @throws |
| 217 | | | @throws |
| 218 | | | |
| 219 | | | @throws |
| 220 | | | @throws |
| 221 | | | |
| 222 | | | @throws |
| 223 | | | |
| 224 | | | public JmsHandler () |
| 225 | | | throws SecurityException, |
| 226 | | | NamingException, |
| 227 | | | InstantiationException, |
| 228 | | | IllegalAccessException, |
| 229 | | | ClassNotFoundException, |
| 230 | | | JMSException |
| 231 | | | { |
| 232 | 0 | | super(); |
| 233 | 0 | | mManager.checkAccess(); |
| 234 | 0 | | configure(); |
| 235 | 0 | | connect(); |
| 236 | 0 | | } |
| 237 | | | |
| 238 | | | {@inheritDoc} |
| 239 | | | public void close () |
| 240 | | | throws SecurityException |
| 241 | | | { |
| 242 | 0 | | mManager.checkAccess(); |
| 243 | 0 | | setFormatter(null); |
| 244 | 0 | | setFilter(null); |
| 245 | 0 | | synchronized (mAllSessions) |
| 246 | | | { |
| 247 | 0 | | while (! mAllSessions.isEmpty()) |
| 248 | | | { |
| 249 | 0 | | final WeakReference ref = (WeakReference) mAllSessions.remove(0); |
| 250 | 0 | | final QueueSession session = (QueueSession) ref.get(); |
| 251 | 0 | | if (session != null) |
| 252 | | | { |
| 253 | | | try |
| 254 | | | { |
| 255 | 0 | | session.close(); |
| 256 | | | } |
| 257 | 0 | | catch (Exception ex) |
| 258 | | | { |
| 259 | 0 | | reportError("Error closing jms session: " + session, |
| 260 | | | ex, ErrorManager.CLOSE_FAILURE); |
| 261 | 0 | | } |
| 262 | | | } |
| 263 | 0 | | } |
| 264 | 0 | | } |
| 265 | | | try |
| 266 | | | { |
| 267 | 0 | | mJmsConnection.close(); |
| 268 | | | } |
| 269 | 0 | | catch (JMSException jex) |
| 270 | | | { |
| 271 | 0 | | reportError("Error closing jms connection: " + mJmsConnection, |
| 272 | | | jex, ErrorManager.CLOSE_FAILURE); |
| 273 | 0 | | } |
| 274 | 0 | | } |
| 275 | | | |
| 276 | | | {@inheritDoc} |
| 277 | | | public void flush () |
| 278 | | | { |
| 279 | | | |
| 280 | 0 | | } |
| 281 | | | |
| 282 | | | {@inheritDoc} |
| 283 | | | public void publish (final LogRecord record) |
| 284 | | | { |
| 285 | 0 | | if (getFilter().isLoggable(record)) |
| 286 | | | { |
| 287 | 0 | | sendRecord(record); |
| 288 | | | } |
| 289 | 0 | | } |
| 290 | | | |
| 291 | | | private void configure () |
| 292 | | | throws NamingException, |
| 293 | | | SecurityException, |
| 294 | | | InstantiationException, |
| 295 | | | IllegalAccessException, |
| 296 | | | ClassNotFoundException |
| 297 | | | { |
| 298 | 0 | | configureJndiContext(); |
| 299 | 0 | | configureResources(); |
| 300 | 0 | | configureFilter(); |
| 301 | 0 | | configureFormatter(); |
| 302 | 0 | | } |
| 303 | | | |
| 304 | | | private void configureJndiContext () |
| 305 | | | throws NamingException |
| 306 | | | { |
| 307 | 0 | | final Context context = new InitialContext(); |
| 308 | 0 | | mContext = context; |
| 309 | 0 | | } |
| 310 | | | |
| 311 | | | private void configureFilter () |
| 312 | | | throws SecurityException, |
| 313 | | | InstantiationException, |
| 314 | | | IllegalAccessException, |
| 315 | | | ClassNotFoundException |
| 316 | | | { |
| 317 | 0 | | final String filterClass = mManager.getProperty(JMS_FILTER_PROPERTY); |
| 318 | 0 | | if (filterClass == null || filterClass.length() == 0) |
| 319 | | | { |
| 320 | 0 | | setFilter(new MessageIdFilter()); |
| 321 | | | } |
| 322 | | | else |
| 323 | | | { |
| 324 | | | try |
| 325 | | | { |
| 326 | 0 | | AccessController.doPrivileged( |
| 327 | | | new PrivilegedExceptionAction() |
| 328 | 0 | | { |
| 329 | | | public Object run () |
| 330 | | | throws SecurityException, |
| 331 | | | InstantiationException, |
| 332 | | | IllegalAccessException, |
| 333 | | | ClassNotFoundException |
| 334 | | | { |
| 335 | 0 | | setFilter((Filter) Class.forName(filterClass) |
| 336 | | | .newInstance()); |
| 337 | 0 | | return null; |
| 338 | | | } |
| 339 | | | } |
| 340 | | | ); |
| 341 | | | } |
| 342 | 0 | | catch (PrivilegedActionException e) |
| 343 | | | { |
| 344 | 0 | | final InstantiationException iex = new InstantiationException( |
| 345 | | | "Could not install the Filter: " + filterClass); |
| 346 | 0 | | iex.initCause(e); |
| 347 | 0 | | throw iex; |
| 348 | 0 | | } |
| 349 | | | } |
| 350 | 0 | | } |
| 351 | | | |
| 352 | | | private void configureFormatter () |
| 353 | | | throws SecurityException, |
| 354 | | | InstantiationException, |
| 355 | | | IllegalAccessException, |
| 356 | | | ClassNotFoundException |
| 357 | | | { |
| 358 | 0 | | final String formatter = mManager.getProperty(JMS_FORMATTER_PROPERTY); |
| 359 | 0 | | if (formatter == null || formatter.length() == 0) |
| 360 | | | { |
| 361 | 0 | | setFormatter(new DefaultFormatter()); |
| 362 | | | } |
| 363 | | | else |
| 364 | | | { |
| 365 | | | try |
| 366 | | | { |
| 367 | 0 | | AccessController.doPrivileged( |
| 368 | | | new PrivilegedExceptionAction() |
| 369 | 0 | | { |
| 370 | | | public Object run () |
| 371 | | | throws SecurityException, |
| 372 | | | InstantiationException, |
| 373 | | | IllegalAccessException, |
| 374 | | | ClassNotFoundException |
| 375 | | | { |
| 376 | 0 | | setFormatter((Formatter) Class.forName(formatter) |
| 377 | | | .newInstance()); |
| 378 | 0 | | return null; |
| 379 | | | } |
| 380 | | | } |
| 381 | | | ); |
| 382 | | | } |
| 383 | 0 | | catch (PrivilegedActionException e) |
| 384 | | | { |
| 385 | 0 | | final InstantiationException iex |
| 386 | | | = new InstantiationException( |
| 387 | | | "Could not install the Formatter: " + formatter); |
| 388 | 0 | | iex.initCause(e); |
| 389 | 0 | | throw iex; |
| 390 | 0 | | } |
| 391 | | | } |
| 392 | 0 | | } |
| 393 | | | |
| 394 | | | private void configureResources () |
| 395 | | | throws InstantiationException |
| 396 | | | { |
| 397 | 0 | | final String factory = mManager.getProperty(JMS_FACTORY_NAME_PROPERTY); |
| 398 | 0 | | if ((factory == null) || (factory.length() == 0)) |
| 399 | | | { |
| 400 | 0 | | throw new InstantiationException("No jms connection factory configured" |
| 401 | | | + " in properties file with property: " |
| 402 | | | + JMS_FACTORY_NAME_PROPERTY); |
| 403 | | | } |
| 404 | 0 | | final String queue = mManager.getProperty(JMS_QUEUE_NAME_PROPERTY); |
| 405 | 0 | | if ((queue == null) || (queue.length() == 0)) |
| 406 | | | { |
| 407 | 0 | | throw new InstantiationException("No jms queue configured in " |
| 408 | | | + "properties file with property: " + JMS_QUEUE_NAME_PROPERTY); |
| 409 | | | } |
| 410 | 0 | | mFactoryName = factory; |
| 411 | 0 | | mQueueName = queue; |
| 412 | 0 | | } |
| 413 | | | |
| 414 | | | |
| 415 | | | |
| 416 | | | |
| 417 | | | |
| 418 | | | @throws |
| 419 | | | @throws |
| 420 | | | |
| 421 | | | private void connect () |
| 422 | | | throws NamingException, JMSException |
| 423 | | | { |
| 424 | 0 | | final QueueConnectionFactory factory |
| 425 | | | = (QueueConnectionFactory) mContext.lookup(mFactoryName); |
| 426 | 0 | | final Queue queue = (Queue) mContext.lookup(mQueueName); |
| 427 | | | |
| 428 | 0 | | mJmsConnection = factory.createQueueConnection(); |
| 429 | 0 | | mJmsConnection.setClientID(CLIENT_ID); |
| 430 | 0 | | mJmsQueue = queue; |
| 431 | 0 | | } |
| 432 | | | |
| 433 | | | |
| 434 | | | |
| 435 | | | |
| 436 | | | |
| 437 | | | @return |
| 438 | | | |
| 439 | | | @throws |
| 440 | | | |
| 441 | | | private QueueSender getSender () |
| 442 | | | throws JMSException |
| 443 | | | { |
| 444 | 0 | | QueueSender rc = (QueueSender) mJmsSenders.get(); |
| 445 | 0 | | if (rc == null) |
| 446 | | | { |
| 447 | | | try |
| 448 | | | { |
| 449 | 0 | | rc = (QueueSender) AccessController.doPrivileged( |
| 450 | | | new PrivilegedExceptionAction() |
| 451 | 0 | | { |
| 452 | | | public Object run () |
| 453 | | | throws JMSException |
| 454 | | | { |
| 455 | 0 | | return installSender(); |
| 456 | | | } |
| 457 | | | }); |
| 458 | | | } |
| 459 | 0 | | catch (PrivilegedActionException e) |
| 460 | | | { |
| 461 | 0 | | final JMSException jex |
| 462 | | | = new JMSException("Could not install a QueueSender:" + e); |
| 463 | 0 | | jex.initCause(e); |
| 464 | 0 | | throw jex; |
| 465 | 0 | | } |
| 466 | | | } |
| 467 | 0 | | return rc; |
| 468 | | | } |
| 469 | | | |
| 470 | | | |
| 471 | | | |
| 472 | | | |
| 473 | | | |
| 474 | | | @return |
| 475 | | | |
| 476 | | | @throws |
| 477 | | | |
| 478 | | | private QueueSession getSession () |
| 479 | | | throws JMSException |
| 480 | | | { |
| 481 | 0 | | QueueSession rc = (QueueSession) mJmsSessions.get(); |
| 482 | 0 | | if (rc == null) |
| 483 | | | { |
| 484 | 0 | | final QueueSession session = mJmsConnection.createQueueSession( |
| 485 | | | SESSION_TRANSACTION_MODE, SESSION_ACKNOWLEDGE_MODE); |
| 486 | 0 | | mJmsSessions.set(session); |
| 487 | 0 | | synchronized (mAllSessions) |
| 488 | | | { |
| 489 | 0 | | mAllSessions.add(new WeakReference(session)); |
| 490 | 0 | | } |
| 491 | 0 | | rc = session; |
| 492 | | | } |
| 493 | 0 | | return rc; |
| 494 | | | } |
| 495 | | | |
| 496 | | | private QueueSender installSender () |
| 497 | | | throws JMSException |
| 498 | | | { |
| 499 | 0 | | final QueueSession session = getSession(); |
| 500 | 0 | | QueueSender sender = null; |
| 501 | | | try |
| 502 | | | { |
| 503 | 0 | | sender = session.createSender(mJmsQueue); |
| 504 | 0 | | mJmsSenders.set(sender); |
| 505 | | | } |
| 506 | | | finally |
| 507 | | | { |
| 508 | 0 | | if (sender == null) |
| 509 | | | { |
| 510 | | | |
| 511 | 0 | | mJmsSessions.set(null); |
| 512 | 0 | | session.close(); |
| 513 | | | } |
| 514 | | | } |
| 515 | 0 | | return sender; |
| 516 | | | } |
| 517 | | | |
| 518 | | | private void sendRecord (final LogRecord record) |
| 519 | | | { |
| 520 | 0 | | String text = null; |
| 521 | | | try |
| 522 | | | { |
| 523 | 0 | | text = getFormatter().format(record); |
| 524 | | | } |
| 525 | 0 | | catch (Exception ex) |
| 526 | | | { |
| 527 | 0 | | reportError("Error formatting the log record", |
| 528 | | | ex, ErrorManager.FORMAT_FAILURE); |
| 529 | 0 | | } |
| 530 | 0 | | if (text != null) |
| 531 | | | { |
| 532 | | | try |
| 533 | | | { |
| 534 | 0 | | final TextMessage msg = getSession().createTextMessage(); |
| 535 | 0 | | msg.setText(text); |
| 536 | 0 | | getSender().send(msg); |
| 537 | | | } |
| 538 | 0 | | catch (JMSException ex) |
| 539 | | | { |
| 540 | 0 | | reportError("Error publishing a log record", ex, |
| 541 | | | ErrorManager.WRITE_FAILURE); |
| 542 | 0 | | } |
| 543 | | | } |
| 544 | 0 | | } |
| 545 | | | } |