KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stat > StatControllerImpl


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package stat;
25
26 import org.objectweb.fractal.julia.loader.Initializable;
27 import org.objectweb.fractal.julia.loader.Tree;
28
29 public class StatControllerImpl implements Initializable, StatController {
30
31   /**
32    * Number of calls to preMethod, i.e., the total number of method calls.
33    */

34
35   private int calls;
36
37   /**
38    * Number of calls to postMethod, i.e., the total number of method success
39    * (since postMethod is not called inside a finally block - see
40    * StatCodeGenerator - it is not called if the intercepted method throws an
41    * exception).
42    */

43
44   private int success;
45
46   /**
47    * Number of read field accesses.
48    */

49
50   private int reads;
51
52   /**
53    * Number of write field accesses.
54    */

55
56   private int writes;
57
58   /**
59    * Total execution time, in milliseconds
60    */

61
62   private long totalTime;
63
64   /**
65    * True if counters must be reinitialized when getter methods are called.
66    */

67
68   private boolean reset;
69
70   // implementation of the Initializable interface
71

72   public void initialize (final Tree args) {
73     String JavaDoc s = args.getSubTree(0).toString();
74     if (s.equals("on")) {
75       reset = true;
76     }
77   }
78
79   // implementation of the StatController interface
80

81   public int getNumberOfMethodCall () {
82     int calls = this.calls;
83     if (reset) {
84       this.calls = 0;
85     }
86     return calls;
87   }
88
89   public int getNumberOfMethodSuccess () {
90     int success = this.success;
91     if (reset) {
92       this.success = 0;
93     }
94     return success;
95   }
96
97   public int getNumberOfFieldRead () {
98     int reads = this.reads;
99     if (reset) {
100       this.reads = 0;
101     }
102     return reads;
103   }
104
105   public int getNumberOfFieldWrite () {
106     int writes = this.writes;
107     if (reset) {
108       this.writes = 0;
109     }
110     return writes;
111   }
112
113   public long getTotalExecutionTime () {
114     long totalTime = this.totalTime;
115     if (reset) {
116       this.totalTime = 0;
117     }
118     return totalTime;
119   }
120
121   // methods called by the associated interceptor
122

123   public long preMethod (final String JavaDoc method) {
124     synchronized (this) {
125       ++calls;
126       return System.currentTimeMillis();
127     }
128   }
129
130   public void postMethod (final String JavaDoc method, long start) {
131     synchronized (this) {
132       ++success;
133       totalTime += System.currentTimeMillis() - start;
134     }
135   }
136
137   public void getField (final String JavaDoc field) {
138     ++reads;
139   }
140
141   public void setField (final String JavaDoc field) {
142     ++writes;
143   }
144 }
145
Popular Tags