KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > processor > AsynchronousLogProcessor


1 package org.jzonic.jlo.processor;
2
3 import org.jzonic.jlo.LogEvent;
4 import org.jzonic.jlo.LogGenerator;
5 import org.jzonic.jlo.LogRecord;
6
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 /**
11  * The AsynchronousLogProcessor will process all incoming
12  * log request inside a separate thread.
13  *
14  *@author mecky
15  *@created 29. Januar 2002
16  */

17 public class AsynchronousLogProcessor extends AbstractLogProcessor implements Runnable JavaDoc {
18     
19     private static List JavaDoc events = new Vector JavaDoc();
20     private static long counter = 0;
21     private static Thread JavaDoc threadCleanerUpper = null;
22     private boolean running = false;
23     int milliSecondSleepTime = 500;
24
25     /**
26      * Constructor for the LogHandler object
27      */

28     public AsynchronousLogProcessor() {
29         running = true;
30         Thread JavaDoc thread = new Thread JavaDoc(this);
31         thread.setDaemon(true);
32         thread.start();
33     }
34     
35     public void run() {
36         LogEvent le;
37         while ( running ) {
38             while ( getEvents().iterator().hasNext() ) {
39                 le = getNextRecord();
40                 handlePipes(le);
41                 handle(le);
42                 handleSpecialChannels(le.getLogRecord());
43             }
44             // time to sleep.
45
try {
46                 Thread.sleep(this.milliSecondSleepTime);
47             }
48             catch (Exception JavaDoc e) {
49                 // we do nothing here
50
}
51         }
52     }
53           
54     private synchronized List JavaDoc getEvents() {
55         return events;
56     }
57     
58     /**
59      * This method adds a logevent to the qeue of the LogHandler.
60      *
61      *@param handler the handler for this logevent
62      *@param formatter the formatter for this logevent (can be null)
63      *@param lr the LogRecord
64      */

65     public void processEvent(LogGenerator lg,LogRecord lr) {
66         LogEvent le = new LogEvent(lg.getHandler(), lg.getFormatter(), lr);
67         if ( lg.getFilter() != null ) {
68             if ( lg.getFilter().match(lr.getMessage() )) {
69                 getEvents().add(le);
70             }
71         }
72         else {
73             getEvents().add(le);
74         }
75     }
76     
77     public void flush() {
78         while ( getEvents().iterator().hasNext() ) {
79             LogEvent le = getNextRecord();
80             if (le.getFormatter() != null) {
81                 String JavaDoc msg = le.getFormatter().formatMessage(le.getLogRecord());
82                 le.getHandler().publish(msg);
83             } else {
84                 le.getHandler().publish(le.getLogRecord());
85             }
86             
87         }
88     }
89
90     private synchronized LogEvent getNextRecord() {
91         LogEvent le = (LogEvent) getEvents().iterator().next();
92         getEvents().remove(le);
93         return le;
94     }
95     
96     public String JavaDoc getProcessorName() {
97         return "AsynchronousLogProcessor";
98     }
99     
100 }
101
Popular Tags