KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jmx > julia > stat > BasicStatController


1 /***
2  * Fractal JMX
3  * Copyright (C) 2003 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: fractal@objectweb.org
20  */

21 package org.objectweb.fractal.jmx.julia.stat;
22
23 import org.objectweb.fractal.julia.loader.Tree;
24 import org.objectweb.fractal.julia.loader.Initializable;
25
26 /**
27  * Simple implementation of {@link StatController}.
28  * <UL>
29  * <LI>The number of (intercepted) method calls, returned by this implementation,
30  * is incremented as usual each time a call is made on the server interfaces of the component
31  * (control interfaces excluded).
32  * <LI>The rate of (intercepted) method calls, returned by this implementation, are re-computed every {@link #_period period}.
33  * The value of this period must be positive and can be fixed by {@link #initialize initialize}.
34  * </UL>
35  *
36  * @version 0.1
37  */

38 public class BasicStatController implements StatController, Initializable {
39
40     /**
41      * The minimal time interval used for rate re-computation, in milliseconds (default value is 5000).
42      * <p>Its value must be positive and can be fixed by {@link #initialize initialize}.
43      */

44     protected long _period = 5000;
45
46     /**
47      * Number of calls to preMethod, i.e., the total number of method calls.
48      */

49     private int _calls;
50
51     /**
52      * Number of calls to preMethod during the current sample.
53      */

54     private int _callsCrtSample;
55
56     /**
57      * Rate of calls to preMethod during the last sample.
58      */

59     private double _rateOfMethodCall;
60
61     /**
62      * Number of calls to postMethod, i.e., the total number of method success
63      * (since postMethod is not called inside a finally block - see
64      * StatCodeGenerator - it is not called if the intercepted method throws an
65      * exception).
66      */

67     private int _success;
68
69     /**
70      * Number of calls to postMethod during the current sample.
71      */

72     private int _successCrtSample;
73
74     /**
75      * Rate of calls to preMethod during the last sample.
76      */

77     private double _rateOfMethodSuccess;
78
79     /**
80      * Sample start time
81      */

82     private long _startSample = System.currentTimeMillis();
83
84     // -------------------------------------------------------------------------
85
// implementation of the Initializable interface
86
// -------------------------------------------------------------------------
87
/**
88      * Initializes this object with the given arguments.
89      * The first subtree (if any) must represent the value of {@link #_period period}.
90      *
91      * @param args the arguments to be used to initialize this object.
92      * @throws NumberFormatException
93      * if the first subtree is not a valid string representation of a long value.
94      * @throws IllegalStateException if the firts subtree does not represent a positive value.
95      */

96     public void initialize(final Tree args) {
97         //System.out.println("-- " + args.toString() + ", period=" + period);
98
if (args.getSize() == 0)
99             return;
100         _period = Long.valueOf(args.getSubTree(0).toString()).longValue();
101         if (_period < 1)
102             throw new IllegalStateException JavaDoc("period=" + _period + " is invalid. The value must be positive.");
103         //System.out.println("-- " + args.toString() + ", period=" + period);
104
}
105
106     // -------------------------------------------------------------------------
107
// implementation of the StatController interface
108
// -------------------------------------------------------------------------
109
public synchronized void reset() {
110         _calls = 0;
111         _callsCrtSample = 0;
112         _rateOfMethodCall = 0;
113         _success = 0;
114         _successCrtSample = 0;
115         _rateOfMethodSuccess = 0;
116         _startSample = System.currentTimeMillis();
117     }
118
119     public int getNumberOfMethodCall() {
120         return _calls;
121     }
122
123     public int getNumberOfMethodSuccess() {
124         return _success;
125     }
126
127     public double getRateOfMethodCall() {
128         resetSample();
129         return _rateOfMethodCall;
130     }
131
132     public double getRateOfMethodSuccess() {
133         resetSample();
134         return _rateOfMethodSuccess;
135     }
136
137     private synchronized void resetSample() {
138         long t = System.currentTimeMillis();
139         if (t < _startSample + _period)
140             return;
141         _rateOfMethodCall = (double)(_callsCrtSample * 1000) / (t - _startSample);
142         _callsCrtSample = 0;
143         _rateOfMethodSuccess = (double)(_successCrtSample * 1000) / (t - _startSample);
144         _successCrtSample = 0;
145         _startSample = t;
146     }
147
148     // -------------------------------------------------------------------------
149
// methods called by the associated interceptor
150
// -------------------------------------------------------------------------
151
/**
152      * A preMethod called by the associated interceptor.
153      * This preMethod increments the number of method calls that have been made on the server
154      * interfaces of the component (control interfaces excluded).
155      *
156      * @param method the method that is intercepted.
157      */

158     public void statPreMethod(final String JavaDoc method) {
159         synchronized (this) {
160             ++_calls;
161             ++_callsCrtSample;
162         }
163     }
164
165     /**
166      * A postMethod called by the associated interceptor.
167      * Since postMethod is not called inside a finally block - see
168      * StatCodeGenerator - it is not called if the intercepted method throws an
169      * exception.
170      * <P>This postMethod increments the number of method calls that have been made on the server
171      * interfaces of the component (control interfaces excluded), and that have
172      * "succeded", i.e., that have not thrown an exception.
173      *
174      * @param method the method that is intercepted.
175      */

176     public void statPostMethod(final String JavaDoc method) {
177         synchronized (this) {
178             ++_success;
179             ++_successCrtSample;
180         }
181     }
182 }
183
184
Popular Tags