KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > tracing > TracingAC


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

17
18 package org.objectweb.jac.aspects.tracing;
19
20 import java.util.*;
21 import org.objectweb.jac.core.*;
22 import org.objectweb.jac.util.Log;
23 import org.objectweb.jac.wrappers.*;
24
25 /**
26  * This sample aspect component traces the calls on all the objects of
27  * the program.
28  *
29  * <p>To be active this aspect component must be configured with the
30  * <code>addTrace</code> method.
31  *
32  * @see org.objectweb.jac.wrappers.VerboseWrapper */

33
34 public class TracingAC extends AspectComponent implements TracingConf {
35
36     VerboseWrapper timedWrapper;
37     VerboseWrapper namedWrapper;
38     VerboseWrapper stackWrapper;
39     VerboseWrapper wrappersWrapper;
40
41     public TracingAC () {
42         timedWrapper = new VerboseWrapper(this,VerboseWrapper.TIMED);
43         namedWrapper = new VerboseWrapper(this,VerboseWrapper.NAMED);
44         stackWrapper = new VerboseWrapper(this,VerboseWrapper.STACK);
45         wrappersWrapper = new VerboseWrapper(this,VerboseWrapper.WRAPPERS);
46     }
47     
48     public void addTrace(String JavaDoc wrappeeExpr,
49                          String JavaDoc wrappeeClassExpr,
50                          String JavaDoc wrappeeMethodExpr) {
51         pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
52                  timedWrapper, null);
53     }
54
55     // TODO: Handle other types of traces
56

57     public void addNamedTrace(String JavaDoc wrappeeExpr,
58                               String JavaDoc wrappeeClassExpr,
59                               String JavaDoc wrappeeMethodExpr) {
60         pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
61                  namedWrapper, null);
62     }
63
64     public void addStackTrace(String JavaDoc wrappeeExpr,
65                               String JavaDoc wrappeeClassExpr,
66                               String JavaDoc wrappeeMethodExpr) {
67
68         pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
69                  stackWrapper, null);
70     }
71
72     public void addWrappersTrace(String JavaDoc wrappeeExpr,
73                                  String JavaDoc wrappeeClassExpr,
74                                  String JavaDoc wrappeeMethodExpr) {
75         pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
76                  wrappersWrapper,null);
77     }
78
79     public void addRecording(String JavaDoc wrappeeExpr,
80                              String JavaDoc wrappeeClassExpr,
81                              String JavaDoc wrappeeMethodExpr) {
82
83         pointcut(wrappeeExpr+" && !recorder0",
84                  wrappeeClassExpr,
85                  wrappeeMethodExpr,
86                  RecordingWrapper.class.getName(), null, false);
87     }
88
89     Hashtable counters = new Hashtable();
90
91     public void addCounter(String JavaDoc name,
92                            String JavaDoc wrappeeExpr,
93                            String JavaDoc wrappeeClassExpr,
94                            String JavaDoc wrappeeMethodExpr) {
95
96         Counter c = (Counter) counters.get(name);
97         if (c == null) {
98             c = new Counter();
99             counters.put(name, c);
100         }
101         pointcut(wrappeeExpr, wrappeeClassExpr, wrappeeMethodExpr,
102                  new SimpleCountingWrapper(this,c), null);
103     }
104
105     public void addOptimizedCounter(String JavaDoc name,
106                                     String JavaDoc wrappeeExpr,
107                                     String JavaDoc wrappeeClassExpr,
108                                     String JavaDoc wrappeeMethodExpr,
109                                     String JavaDoc fieldName,
110                                     String JavaDoc argNumber) {
111       
112         Counter c = (Counter) counters.get(name);
113         if (c == null) {
114             c = new Counter();
115             counters.put(name,c);
116         }
117         if (!fieldName.equals("")) {
118             pointcut(wrappeeExpr, wrappeeClassExpr,
119                      wrappeeMethodExpr,
120                      new OptimizedCountingWrapper(this,c,fieldName),
121                      null);
122         }
123         if (!argNumber.equals("")) {
124             pointcut(wrappeeExpr, wrappeeClassExpr,
125                      wrappeeMethodExpr,
126                      new OptimizedCountingWrapper(
127                          this, c, (new Integer JavaDoc(argNumber)).intValue()),
128                      null );
129         }
130     }
131
132     /**
133      * Skips the counting wrapper if this call is part of a global
134      * conting optimization performed by an
135      * <code>OptimizedCountingWrapper</code>
136      *
137      * @param wrapper the wrapper
138      * @param wrappingMethod the wrapping method that is about to be
139      * run
140      * @see OptimizedCountingWrapper */

141
142     public boolean beforeRunningWrapper (Wrapper wrapper,
143                                          String JavaDoc wrappingMethod) {
144         if (attr("tracing.globalIncr")!=null)
145             return false;
146         return true;
147     }
148
149
150 }
151
Popular Tags