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.view;
22  
23  import org.apache.log4j.Logger;
24  
25  import java.util.Hashtable;
26  
27  import net.sf.jour.filter.*;
28  
29  import net.sf.jour.rt.view.config.*;
30  import net.sf.jour.rt.agent.ProfilerEvent;
31  
32  /***
33   * TODO Add docs
34   * Created on 30.07.2004
35   *
36   * Contributing Author(s):
37   *
38   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
39   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
40   *
41   * @author vlads
42   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/06 02:31:42 $
43   */
44  public class ClassMatchFilter extends MatchListFilter {
45  
46  	protected static final Logger log = Logger.getLogger(ClassMatchFilter.class);
47  	
48      private class MethodsFilter extends MatchStringListFilter {
49  		MatchStringListFilter methodsSignature;
50  		
51  		MethodsFilter() {
52  			this.name = "Methods";
53  			this.methodsSignature = new MatchStringListFilter();
54  			this.methodsSignature.name = "signatureList";
55  		}
56  		
57          public boolean readContext(Object o) {
58              if (o instanceof SignatureType) {
59                  addSignature((SignatureType) o);
60                  return true;
61              } else {
62                  return super.readContext(o);
63              }
64          }
65  		
66          void addSignature(SignatureType sigDef) {
67              if (sigDef.isEnabled()) {
68  				this.methodsSignature.addInclude(sigDef.getNames(), false);
69  				this.methodsSignature.readConfig(sigDef.getInclude());
70  				this.methodsSignature.readConfig(sigDef.getExclude());
71              }
72          }
73  		
74          public void readConfig(MethodsType funcDef) {
75  			super.addInclude(funcDef.getNames(), false);
76  			super.readConfig(funcDef.getContent());
77          }
78          
79          public boolean matchMethod(String methodName) {
80              return super.match(methodName);
81          }
82  		
83          public boolean match(String methodName, String signature) {
84              return super.match(methodName)
85                  && (this.methodsSignature.isEmpty()
86                      || ((!this.methodsSignature.isEmpty()) && this.methodsSignature.match(signature)));
87          }
88  		
89  		public int matchState(Object obj) {
90  			if (obj instanceof ProfilerEvent) {
91  				ProfilerEvent event = (ProfilerEvent)obj;
92  				if (debug) {
93  					ClassMatchFilter.log.debug("match methods"); 
94  				}
95  				if (this.match(event.getMethodName(), event.getMethodSignature())) {
96  					return MATCH_YES;
97  				} else {
98  					return MATCH_NO;
99  				}
100 			} else {
101 				return super.matchState(obj);
102 			}
103 		}
104 		
105 		public void debug() {
106 			super.debug();
107 			this.methodsSignature.debug();
108 		}
109     }
110 		
111 	private class ClassesFilter extends MatchStringListFilter {
112 		MatchListFilter methods;
113 		Hashtable cache;
114 
115 		public ClassesFilter() {
116 			this.name = "Classes";
117 			methods = new MatchListFilter();
118 			methods.name = "MethodsList";
119 			cache = new Hashtable();
120 		}
121 
122 		/*
123 		 * Cache answer by MethodID.
124 		 * 
125 		 * @see net.sf.jour.filter.MatchFilter#matchState(java.lang.Object)
126 		 */
127 		public int matchState(Object obj) {
128 			if (obj instanceof ProfilerEvent) {
129 				ProfilerEvent event = (ProfilerEvent)obj;
130 				
131 				Integer value = (Integer)cache.get(event.getMethodID());
132 				int rc;
133 				
134                 if (value == null) {
135                     rc = super.matchState(event.getClassName());
136 
137                     if (isMatch(rc) && (!methods.isEmpty())) {
138                         rc = methods.matchState(event);
139 						if (debug) {
140 							ClassMatchFilter.log.debug("match class = true, match methods = " 
141 							  + match2String(rc));
142 						}
143                     }
144 					cache.put(event.getMethodID(), new Integer(rc));
145                 } else {
146                 	rc = value.intValue();
147                     if (debug) {
148                         ClassMatchFilter.log.debug("match class use cache = " + match2String(rc));
149                     }
150                 }
151 				
152 				return rc;
153 				
154 			} else {
155 				return super.matchState(obj);
156 			}
157 		}
158 		
159 		public void debug() {
160 			super.debug();
161 			this.methods.debug();
162 		}
163 		
164 		public void addMethods(MethodsType def) {
165 			if (!def.isEnabled()) return;
166 			MethodsFilter cf = new MethodsFilter();
167 			cf.readConfig(def);
168 			methods.addInclude(cf);
169 		}
170 
171 		public boolean readContext(Object o) {
172 			if (o instanceof MethodsType) {
173 				addMethods((MethodsType) o);
174 				return true;
175 			} else {
176 				return super.readContext(o);
177 			}
178 		}
179 		
180         public void readConfig(ClassesType classesDef) {
181             super.addInclude(classesDef.getNames(), false);
182 			super.readConfig(classesDef.getContent());
183         }
184 	}
185 
186 	/***
187 	 *
188 	 */
189 	public ClassMatchFilter() {
190 	}
191 
192 	public void addClasses(ClassesType classesDef) {
193 		if (!classesDef.isEnabled()) return;
194 		ClassesFilter cf = new ClassesFilter();
195 		cf.readConfig(classesDef);
196 		addInclude(cf);
197 	}
198 	
199 	public boolean readContext(Object o) {
200 		if (o instanceof Classes) {
201 			addClasses((Classes)o);
202 			return true;
203 		} else {
204 			return super.readContext(o);
205 		}
206 	}
207 }