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 net.sf.jour.timer.NativeTimer;
24  import net.sf.jour.util.TimeUtil;
25  
26  /***
27   * TODO Add docs
28   * 
29   * Created on 02.12.2004
30   *
31   * Contributing Author(s):
32   *
33   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
34   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
35   * 
36   * @author michaellif
37   * @version $Revision: 1.12 $ ($Author: vlads $) $Date: 2004/12/15 08:00:37 $
38   */
39  public class ProfilerEvent /*TODO extends LocationInfoEvent */ implements EventTimed {
40  	
41  	/***
42  	 * Stable <code>serialVersionUID</code>.
43  	 */
44  	private static final long serialVersionUID = 29132123L;
45  	
46  	transient private boolean isConstructed;
47  
48      public static int counter = 0;
49      
50      public static final int START = 0;
51      public static final int END = 1;
52      
53      private double timeStamp;
54      private long id;
55      
56      private int type;
57      
58      private Throwable throwable;
59  
60      private EventIdentifier eventIdentifier;
61      //private StackTraceElement[] stackTrace;
62      
63      transient private String threadName;
64      //transient private Class instanceClass;
65      
66  	private ProfilerEvent() {
67  		
68  	}
69  	
70      public ProfilerEvent(int type, Throwable t, EventIdentifier eventIdentifier) {
71          synchronized (this) {
72              this.timeStamp = NativeTimer.getTimeSafe();
73              this.id = counter++;
74          }
75          this.type = type;
76          this.throwable = t;
77          if (eventIdentifier == null) {
78  			//this.stackTrace = new Throwable().getStackTrace();
79          } else {
80          	//this.stackTrace = null;
81      	}
82          this.eventIdentifier = eventIdentifier;
83          this.threadName = Thread.currentThread().getName();
84      }
85      
86      /*public StackTraceElement[] getStackTrace() {
87          return stackTrace;
88      }*/
89  
90      /* To be used by log4j ..? may be faster to use getClassName()
91      Class getOriginClass() {
92          return instanceClass;
93      }*/
94      
95      public boolean onMethodStart() {
96          return (this.type == START);
97      }
98  
99      public boolean onMethodEnd() {
100         return (this.type == END);
101     }
102 
103     public Throwable getThrowable() {
104         return this.throwable;
105     }
106 
107     
108     public String getThreadName() {
109         return this.threadName;
110     }
111 
112     public Object getThreadID() {
113         return this.threadName;
114     }
115     
116     public void setThreadName(String threadName) {
117         this.threadName = threadName;
118     }
119     
120     public boolean onConstruction() {
121         if (this.eventIdentifier != null) {
122             return this.eventIdentifier.isConstruction();
123         } else {
124             return this.getMethodName().trim().equalsIgnoreCase("<init>") && (type == END);
125         }
126     }
127 
128     public boolean onDeletion() {
129         if (this.eventIdentifier != null) {
130             return this.eventIdentifier.isDeletion();
131         } else {
132             return this.getMethodName().trim().equalsIgnoreCase("finalize") && (type == END);
133         }
134     }
135     
136     public String getClassName() {
137         if (this.eventIdentifier != null) {
138             return this.eventIdentifier.getClassName();
139         } else {
140             return "j1.3";// this.stackTrace[1].getClassName();
141         }
142     }
143 
144     public String getMethodName() {
145         if (this.eventIdentifier != null) {
146             return this.eventIdentifier.getMethodName();
147         } else {
148             return "j1.3"; //this.stackTrace[1].getMethodName();
149         }
150     }
151 	
152 	public String getMethodSignature() {
153 		if (this.eventIdentifier != null) {
154 			return this.eventIdentifier.getMethodSignature();
155 		} else {
156 			return "?";
157 		}
158 	}
159 	
160 	public Object getMethodID() {
161 		if (this.eventIdentifier != null) {
162 			return this.eventIdentifier.getKey();
163 		} else {
164 			return new String("?");
165 		}
166 	}
167 	
168     public double getTimestamp() {
169         return this.timeStamp;
170     }
171 
172     public long getTimestampLong() {
173         return new Double(this.timeStamp).longValue();
174     }
175     
176     public String getType() {
177         return ((type == START)?"START":"END");
178     }
179     
180     /*
181      * For better dump use net.sf.jour.log4j.ext.DequeueForLog4jAppender.
182      */
183     
184     public String toString() {
185         return "ProfilerEvent [className=" + getClassName() + "," +
186 		"methodName=" + getMethodName() + "," +
187 		"threadName=" + getThreadID() + "," +
188 		getType() +
189 		"timeStamp=" + TimeUtil.timeStamp2string(this.timeStamp) + "]";
190     }
191 }
192