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.rt.agent;
22  
23  import org.apache.log4j.Logger;
24  
25  import net.sf.jour.filter.MatchFilter;
26  import net.sf.jour.rt.swingmonitor.*;
27  import net.sf.jour.rt.view.ViewFilter;
28  /***
29   * 
30   * TODO move to package net.sf.jour.rt.monitor
31   *  
32   * Created on 04.12.2004
33   *
34   * Contributing Author(s):
35   *
36   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
37   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
38   *
39   * @author michaellif
40   * @version $Revision: 1.7 $ ($Author: vlads $) $Date: 2004/12/16 17:40:17 $
41   */
42  public class EventProcessor implements Runnable {
43  
44      protected static final Logger log = Logger.getLogger(EventProcessor.class);
45      
46      /*** DOCUMENT ME! */
47      private ViewFilter viewFilter;
48      
49      private InstanceCounter instanceCounter;
50      
51      private MethodExceutionStatistics methodExecutionStatistics;
52      
53      private JMVRuntimeStatistics jvmInfo;
54     
55      long countTotal;
56      
57      long countAcepted;
58  
59      /*** DOCUMENT ME! */
60      private static EventProcessor instance;
61  
62      private static EventQueueLogger eventLogger = (EventQueueLogger) EventQueueLogger.getInstance();
63  
64      /***
65       * Creates a new EventQueueLogger object.
66       */
67      private EventProcessor() {
68          instanceCounter = new InstanceCounter();
69          viewFilter = new ViewFilter();
70          methodExecutionStatistics = new MethodExceutionStatistics(viewFilter);
71          jvmInfo = new JMVRuntimeStatistics();
72          new Thread(this).start();
73      }
74  
75      /***
76       * DOCUMENT ME!
77       * 
78       * @return DOCUMENT ME!
79       */
80      public static synchronized EventProcessor getInstance() {
81          if (instance == null) {
82              instance = new EventProcessor();
83          }
84          return instance;
85      }
86  
87      public InstanceCounter getInstanceCounter() {
88          return instanceCounter;
89      }
90  
91      public MethodExceutionStatistics getMethodExecutionStatistics() {
92          return methodExecutionStatistics;
93      }
94      
95      public synchronized boolean processEvent(Event event) {
96          if (viewFilter.filer(event)) {
97      		return false;
98      	}
99          MatchFilter filter = viewFilter.getLastMatch();
100         
101         if (ProfilerEvent.class.isAssignableFrom(event.getClass())) {
102             ProfilerEvent pEvent = (ProfilerEvent) event;
103             if (pEvent.onConstruction()) {
104                 this.instanceCounter.constructed(pEvent.getClassName());
105             } else if (pEvent.onDeletion()) {
106                 this.instanceCounter.destroyed(pEvent.getClassName());
107             }
108             return this.methodExecutionStatistics.processEvent(pEvent, filter);
109             
110         } else if (JVMInfoEvent.class.isAssignableFrom(event.getClass())) {
111             JVMInfoEvent jEvent = (JVMInfoEvent) event;
112             return this.jvmInfo.processEvent(jEvent, filter);
113         } else {
114             return false;
115         }
116     }
117 
118     public synchronized void reset() {
119         this.countTotal = 0;
120         this.countAcepted = 0;
121         this.methodExecutionStatistics.reset();
122         this.instanceCounter.reset();
123         this.jvmInfo.reset();
124         
125     }
126     
127     private void processAndCountEvent(Event event) {
128         countTotal ++;
129         if (processEvent(event)) {
130             countAcepted ++;
131         } 
132     }
133     
134     public void run() {
135         while (true) {
136             try {
137 	            Event[] events = eventLogger.getEventsFromQueue();
138 	            for (int i = 0; i < events.length; i++) {
139 	                processAndCountEvent(events[i]);
140 	            }
141                 Thread.sleep(200);
142             } catch (Exception e) {
143                 log.error("Thread error", e);
144             }
145         }
146     }
147 
148     /***
149      * @return Returns the countTotal.
150      */
151     public long getCountTotal() {
152         return countTotal;
153     }
154     /***
155      * @return Returns the countAcepted.
156      */
157     public long getCountAcepted() {
158         return this.countAcepted;
159     }
160     /***
161      * @return Returns the jvmInfo.
162      */
163     public JMVRuntimeStatistics getJvmInfo() {
164         return jvmInfo;
165     }
166 }