1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.log4j.ext;
22
23 import java.util.Enumeration;
24 import java.util.Hashtable;
25 import java.util.Iterator;
26
27 import net.sf.jour.rt.agent.BulkEventFileWriter;
28 import net.sf.jour.rt.agent.BulkEventLogger;
29 import net.sf.jour.rt.agent.SystemInfoEvent;
30 import net.sf.jour.rt.agent.ThreadNumber;
31 import net.sf.jour.util.queue.Queue;
32
33 import org.apache.log4j.AppenderSkeleton;
34 import org.apache.log4j.Logger;
35 import org.apache.log4j.spi.LoggingEvent;
36
37 /***
38 * TODO Add docs
39 *
40 * Created on 07.12.2004
41 * Contributing Author(s):
42 *
43 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
44 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
45 *
46 * @author vlads
47 * @version $Revision: 1.1 $ ($Author: vlads $) $Date: 2004/12/07 09:05:03 $
48 */
49 public class BulkEventFileWriterAppender extends AppenderSkeleton {
50
51 protected final Logger log = Logger.getLogger(BulkEventFileWriterAppender.class);
52
53 Hashtable writers = new Hashtable();
54
55 public void append(LoggingEvent event) {
56 Object obj = event.getMessage();
57 if (obj instanceof Queue) {
58 write((Queue)obj);
59 } else {
60 log.debug("BulkEventFileWriterAppender got:" + obj);
61 log.error("BulkEventFileWriterAppender, Event not supported " + obj.getClass().getName());
62 }
63 }
64
65 BulkEventFileWriter getReciver(Object key) {
66 BulkEventFileWriter reciver = (BulkEventFileWriter) writers.get(key);
67 if (reciver == null) {
68 reciver = new BulkEventFileWriter();
69 writers.put(key, reciver);
70 }
71 return reciver;
72 }
73
74 public void write(Queue eventQueue) {
75 log.debug("write");
76
77
78 ThreadNumber thread = null;
79 SystemInfoEvent systemInfo = null;
80 for (Iterator i = eventQueue.iterator(); i.hasNext(); ) {
81 Object obj = i.next();
82 if (obj instanceof ThreadNumber) {
83 thread = (ThreadNumber)obj;
84 } else if (obj instanceof SystemInfoEvent) {
85 systemInfo = (SystemInfoEvent)obj;
86 }
87 if ((thread != null) && (systemInfo != null)) {
88 break;
89 }
90 }
91 getReciver(ThreadNumber.getUniqueThreadName(thread, systemInfo)).write(eventQueue, thread, systemInfo);
92 }
93
94 public void close() {
95 for (Enumeration e = writers.keys(); e.hasMoreElements();) {
96 Object key = e.nextElement();
97 BulkEventLogger w = (BulkEventLogger) writers.get(key);
98 if (w != null) {
99 w.close();
100 }
101 }
102 }
103
104 public boolean requiresLayout() {
105 return false;
106 }
107 }