View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
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 }