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.*;
24  
25  import net.sf.jour.rt.agent.ProfilerEvent;
26  import net.sf.jour.util.queue.Queue;
27  
28  import org.apache.log4j.helpers.AppenderAttachableImpl;
29  import org.apache.log4j.spi.AppenderAttachable;
30  import org.apache.log4j.spi.LoggingEvent;
31  
32  import org.apache.log4j.*;
33  
34  /***
35   * Convers Jour Events in messages to printable log4j events.
36   *
37   * Created on 06.12.2004
38   * Contributing Author(s):
39   *
40   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
41   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
42   *
43   * @author vlads
44   * @version $Revision: 1.1 $ ($Author: vlads $)  $Date: 2004/12/07 09:05:03 $
45   */
46  public class DequeueForLog4jAppender extends AppenderSkeleton implements AppenderAttachable {
47  
48      protected static final Logger log = Logger.getLogger(DequeueForLog4jAppender.class);
49      
50      AppenderAttachableImpl aai;
51  
52      public DequeueForLog4jAppender() {
53          aai = new AppenderAttachableImpl();
54      }
55  
56      public void addAppender(Appender newAppender) {
57          synchronized (aai) {
58              aai.addAppender(newAppender);
59          }
60      }
61  
62      public Enumeration getAllAppenders() {
63          synchronized (aai) {
64              return aai.getAllAppenders();
65          }
66      }
67  
68      public Appender getAppender(String name) {
69          synchronized (aai) {
70              return aai.getAppender(name);
71          }
72      }
73  
74      public void removeAllAppenders() {
75          synchronized (aai) {
76              aai.removeAllAppenders();
77          }
78      }
79  
80      public void removeAppender(Appender appender) {
81          synchronized (aai) {
82              aai.removeAppender(appender);
83          }
84      }
85  
86      public void removeAppender(String name) {
87          synchronized (aai) {
88              aai.removeAppender(name);
89          }
90      }
91  
92      /***
93       * @deprecated Will be removed with no replacement.
94       */
95      public boolean requiresLayout() {
96          return false;
97      }
98  
99      /***
100      * Is the appender passed as parameter attached to this category?
101      */
102     public boolean isAttached(Appender appender) {
103         return aai.isAttached(appender);
104     }
105 
106     public void append(LoggingEvent event, ProfilerEvent pEvent) {
107     	//pe.setThreadName(threadName);
108     	LoggingProfilerEvent extEvent = 
109     	    new LoggingProfilerEvent(event.fqnOfCategoryClass, 
110     	            log, //TODO 
111     	            event.getLevel(), pEvent);
112         synchronized (aai) {
113             if (aai != null) {
114                 aai.appendLoopOnAppenders(extEvent);
115             }
116         }       
117     }
118     
119     public void append(LoggingEvent event) {
120         Object message = event.getMessage();
121         if (message instanceof Queue) {
122             Queue eventQueue = (Queue)message;
123             for (Iterator i = eventQueue.iterator(); i.hasNext(); ) {
124                 Object obj =  i.next();
125                 if (obj instanceof ProfilerEvent) {
126                     ProfilerEvent pe = (ProfilerEvent) obj;
127                 	//pe.setThreadName(threadName);
128                     append(event, pe);
129             	}
130             }
131         } else if (message instanceof ProfilerEvent) {    
132             append(event, (ProfilerEvent)message);
133         } else {            
134             synchronized (aai) {
135                 if ((aai != null) && (event != null)) {
136                     aai.appendLoopOnAppenders(event);
137                 }
138             }
139         }
140 
141     }
142 
143     public void close() {
144         aai.removeAllAppenders();
145     }
146 }