001: package org.jzonic.jlo.processor;
002:
003: import org.jzonic.jlo.LogEvent;
004: import org.jzonic.jlo.LogGenerator;
005: import org.jzonic.jlo.LogRecord;
006:
007: import java.util.List;
008: import java.util.Vector;
009:
010: /**
011: * The AsynchronousLogProcessor will process all incoming
012: * log request inside a separate thread.
013: *
014: *@author mecky
015: *@created 29. Januar 2002
016: */
017: public class AsynchronousLogProcessor extends AbstractLogProcessor
018: implements Runnable {
019:
020: private static List events = new Vector();
021: private static long counter = 0;
022: private static Thread threadCleanerUpper = null;
023: private boolean running = false;
024: int milliSecondSleepTime = 500;
025:
026: /**
027: * Constructor for the LogHandler object
028: */
029: public AsynchronousLogProcessor() {
030: running = true;
031: Thread thread = new Thread(this );
032: thread.setDaemon(true);
033: thread.start();
034: }
035:
036: public void run() {
037: LogEvent le;
038: while (running) {
039: while (getEvents().iterator().hasNext()) {
040: le = getNextRecord();
041: handlePipes(le);
042: handle(le);
043: handleSpecialChannels(le.getLogRecord());
044: }
045: // time to sleep.
046: try {
047: Thread.sleep(this .milliSecondSleepTime);
048: } catch (Exception e) {
049: // we do nothing here
050: }
051: }
052: }
053:
054: private synchronized List getEvents() {
055: return events;
056: }
057:
058: /**
059: * This method adds a logevent to the qeue of the LogHandler.
060: *
061: *@param handler the handler for this logevent
062: *@param formatter the formatter for this logevent (can be null)
063: *@param lr the LogRecord
064: */
065: public void processEvent(LogGenerator lg, LogRecord lr) {
066: LogEvent le = new LogEvent(lg.getHandler(), lg.getFormatter(),
067: lr);
068: if (lg.getFilter() != null) {
069: if (lg.getFilter().match(lr.getMessage())) {
070: getEvents().add(le);
071: }
072: } else {
073: getEvents().add(le);
074: }
075: }
076:
077: public void flush() {
078: while (getEvents().iterator().hasNext()) {
079: LogEvent le = getNextRecord();
080: if (le.getFormatter() != null) {
081: String msg = le.getFormatter().formatMessage(
082: le.getLogRecord());
083: le.getHandler().publish(msg);
084: } else {
085: le.getHandler().publish(le.getLogRecord());
086: }
087:
088: }
089: }
090:
091: private synchronized LogEvent getNextRecord() {
092: LogEvent le = (LogEvent) getEvents().iterator().next();
093: getEvents().remove(le);
094: return le;
095: }
096:
097: public String getProcessorName() {
098: return "AsynchronousLogProcessor";
099: }
100:
101: }
|