KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > stats > WebModuleStatsImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.stats;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import javax.management.j2ee.statistics.Statistic JavaDoc;
31 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
32 import com.sun.logging.LogDomains;
33 import com.sun.enterprise.web.MonitorStatsCapable;
34 import com.sun.enterprise.admin.monitor.stats.WebModuleStats;
35 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
36 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
37 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
38 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
39 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
40 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
41 import com.sun.enterprise.web.monitor.PwcWebModuleStats;
42 import org.apache.catalina.Manager;
43
44
45 /**
46  * Class gathering web module statistics.
47  */

48 public class WebModuleStatsImpl implements WebModuleStats {
49
50     private static Logger JavaDoc _logger
51         = LogDomains.getLogger(LogDomains.WEB_LOGGER);
52
53     protected Manager sessionManager;
54     protected GenericStatsImpl baseStatsImpl;
55
56     private PwcWebModuleStats pwcWebStats;
57     private long initTime;
58
59     /*
60      * JSP related stats
61      */

62     protected MutableCountStatistic jspCount;
63     protected MutableCountStatistic jspReloadCount;
64     protected MutableCountStatistic jspErrorCount;
65
66     /*
67      * Session management related stats
68      */

69     protected MutableCountStatistic sessionsTotal;
70     protected MutableCountStatistic activeSessionsCurrent;
71     protected MutableCountStatistic activeSessionsHigh;
72     protected MutableCountStatistic rejectedSessionsTotal;
73     protected MutableCountStatistic expiredSessionsTotal;
74     protected MutableCountStatistic processingTimeMillis;
75     
76
77     /**
78      * Constructor.
79      */

80     public WebModuleStatsImpl() {
81         this(com.sun.enterprise.admin.monitor.stats.WebModuleStats.class);
82     }
83
84     /**
85      * Constructor.
86      *
87      * @param inter Interface exposed by this instance to the monitoring
88      * framework
89      */

90     protected WebModuleStatsImpl(Class JavaDoc inter) {
91         baseStatsImpl = new GenericStatsImpl(inter, this);
92
93         // initialize all the MutableStatistic Classes
94
initializeStatistics();
95     }
96
97     
98     /**
99      * Sets the session manager that this WebModuleStatsImpl is going to query
100      * for session-related stats.
101      *
102      * @param manager Session manager
103      */

104     public void setSessionManager(Manager manager) {
105         if (manager == null) {
106             throw new IllegalArgumentException JavaDoc("Null session manager");
107         }
108         this.sessionManager = manager;
109     }
110
111
112     /**
113      * Sets the PwcWebModuleStats instance to which this WebModuleStatsImpl is
114      * going to delegate.
115      *
116      * @param pwcWebStats PwcWebModuleStats instance to which to delegate
117      */

118     public void setPwcWebModuleStats(PwcWebModuleStats pwcWebStats) {
119         if (pwcWebStats == null) {
120             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
121         }
122         this.pwcWebStats = pwcWebStats;
123     }
124
125
126     /**
127      * Gets the number of JSPs that have been loaded in the web module
128      * associated with this WebModuleStatsImpl.
129      *.
130      * @return Number of JSPs that have been loaded
131      */

132     public CountStatistic JavaDoc getJspCount() {
133         if (pwcWebStats == null) {
134             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
135         }
136         jspCount.setCount(pwcWebStats.getJspCount());
137         return (CountStatistic JavaDoc) jspCount.unmodifiableView();
138     }
139     
140     
141     /**
142      * Gets the number of JSPs that have been reloaded in the web module
143      * associated with this WebModuleStatsImpl.
144      *.
145      * @return Number of JSPs that have been reloaded
146      */

147     public CountStatistic JavaDoc getJspReloadCount() {
148         if (pwcWebStats == null) {
149             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
150         }
151         jspReloadCount.setCount(pwcWebStats.getJspReloadCount());
152         return (CountStatistic JavaDoc) jspReloadCount.unmodifiableView();
153     }
154
155
156     /**
157      * Gets the number of errors that were triggered by JSP invocations.
158      *.
159      * @return Number of errors triggered by JSP invocations
160      */

161     public CountStatistic JavaDoc getJspErrorCount() {
162         if (pwcWebStats == null) {
163             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
164         }
165         jspErrorCount.setCount(pwcWebStats.getJspErrorCount());
166         return (CountStatistic JavaDoc) jspErrorCount.unmodifiableView();
167     }
168
169
170     /**
171      * Gets the total number of sessions that have been created for the web
172      * module associated with this WebModuleStatsImpl.
173      *.
174      * @return Total number of sessions created
175      */

176     public CountStatistic JavaDoc getSessionsTotal() {
177         if (pwcWebStats == null) {
178             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
179         }
180         sessionsTotal.setCount(pwcWebStats.getSessionsTotal());
181         return (CountStatistic JavaDoc) sessionsTotal.unmodifiableView();
182     }
183
184
185     /**
186      * Gets the number of currently active sessions for the web
187      * module associated with this WebModuleStatsImpl.
188      *.
189      * @return Number of currently active sessions
190      */

191     public CountStatistic JavaDoc getActiveSessionsCurrent() {
192         if (pwcWebStats == null) {
193             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
194         }
195         activeSessionsCurrent.setCount(pwcWebStats.getActiveSessionsCurrent());
196         return (CountStatistic JavaDoc) activeSessionsCurrent.unmodifiableView();
197     }
198
199
200     /**
201      * Gets the maximum number of concurrently active sessions for the web
202      * module associated with this WebModuleStatsImpl.
203      *
204      * @return Maximum number of concurrently active sessions
205      */

206     public CountStatistic JavaDoc getActiveSessionsHigh() {
207         if (pwcWebStats == null) {
208             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
209         }
210         activeSessionsHigh.setCount(pwcWebStats.getActiveSessionsHigh());
211         return (CountStatistic JavaDoc) activeSessionsHigh.unmodifiableView();
212     }
213
214
215     /**
216      * Gets the total number of rejected sessions for the web
217      * module associated with this WebModuleStatsImpl.
218      *
219      * <p>This is the number of sessions that were not created because the
220      * maximum allowed number of sessions were active.
221      *
222      * @return Total number of rejected sessions
223      */

224     public CountStatistic JavaDoc getRejectedSessionsTotal() {
225         if (pwcWebStats == null) {
226             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
227         }
228         rejectedSessionsTotal.setCount(pwcWebStats.getRejectedSessionsTotal());
229         return (CountStatistic JavaDoc) rejectedSessionsTotal.unmodifiableView();
230     }
231
232
233     /**
234      * Gets the total number of expired sessions for the web
235      * module associated with this WebModuleStatsImpl.
236      *.
237      * @return Total number of expired sessions
238      */

239     public CountStatistic JavaDoc getExpiredSessionsTotal() {
240         if (pwcWebStats == null) {
241             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
242         }
243         expiredSessionsTotal.setCount(pwcWebStats.getExpiredSessionsTotal());
244         return (CountStatistic JavaDoc) expiredSessionsTotal.unmodifiableView();
245     }
246
247
248     /**
249      * Gets the cumulative processing times of all servlets in the web module
250      * associated with this WebModuleStatsImpl.
251      *
252      * @return Cumulative processing times of all servlets in the web module
253      * associated with this WebModuleStatsImpl
254      */

255     public CountStatistic JavaDoc getServletProcessingTimes() {
256         if (pwcWebStats == null) {
257             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
258         }
259         processingTimeMillis.setCount(
260             pwcWebStats.getServletProcessingTimesMillis());
261         return (CountStatistic JavaDoc) processingTimeMillis.unmodifiableView();
262     }
263
264
265     /**
266      * Returns comma-separated list of all sessions currently active in the web
267      * module associated with this WebModuleStatsImpl.
268      *
269      * @return Comma-separated list of all sessions currently active in the
270      * web module associated with this WebModuleStatsImpl
271      */

272     public StringStatistic getSessions() {
273     
274         StringBuffer JavaDoc sb = null;
275
276         if (pwcWebStats == null) {
277             throw new IllegalArgumentException JavaDoc("Null PwcWebModuleStats");
278         }
279
280         String JavaDoc sessionIds = pwcWebStats.getSessionIds();
281         if (sessionIds != null) {
282             sb = new StringBuffer JavaDoc();
283             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(sessionIds, " ");
284             boolean first = true;
285             while (tokenizer.hasMoreTokens()) {
286                 if (!first) {
287                     sb.append(", ");
288                 } else {
289                     first = false;
290                 }
291                 String JavaDoc sessionId = tokenizer.nextToken();
292                 sb.append(sessionId);
293                 HashMap JavaDoc map = pwcWebStats.getSession(sessionId);
294                 if (map != null) {
295                     sb.append(":");
296                     sb.append(map);
297                 }
298             }
299         }
300
301         return new StringStatisticImpl(
302                     sb != null ? sb.toString(): null,
303                     "Sessions",
304                     "String",
305                     "List of currently active sessions",
306                     initTime,
307                     System.currentTimeMillis());
308     }
309
310
311     /**
312      * Resets this WebModuleStats.
313      */

314     public void reset() {
315
316         // Reset session stats
317
if (sessionManager instanceof MonitorStatsCapable) {
318             ((MonitorStatsCapable)sessionManager).resetMonitorStats();
319         }
320
321         if (pwcWebStats != null) {
322             pwcWebStats.reset();
323         }
324     }
325
326
327     /**
328      * Gets all statistics exposed by this WebModuleStatsImpl instance.
329      *
330      * @return Array of all statistics exposed by this WebModuleStatsImpl
331      * instance
332      */

333     public Statistic JavaDoc[] getStatistics() {
334         return baseStatsImpl.getStatistics();
335     }
336
337     
338     /**
339      * Queries for a statistic by name.
340      *
341      * @param name The name of the statistic to query for
342      *
343      * @return The statistic corresponding to the given name
344      */

345     public Statistic JavaDoc getStatistic(String JavaDoc name) {
346         return baseStatsImpl.getStatistic(name);
347     }
348
349     
350     /**
351      * Returns the names of all statistics that may be retrieved from this
352      * WebModuleStatsImpl instance.
353      *
354      * @return Array of names of all statistics exposed by this
355      * WebModuleStatsImpl instance
356      */

357     public String JavaDoc[] getStatisticNames() {
358         return baseStatsImpl.getStatisticNames();
359     }
360
361     
362     private void initializeStatistics() {
363         
364         jspCount = new MutableCountStatisticImpl(
365                         new CountStatisticImpl("JspCount"));
366         jspReloadCount = new MutableCountStatisticImpl(
367                         new CountStatisticImpl("JspReloadCount"));
368         jspErrorCount = new MutableCountStatisticImpl(
369                         new CountStatisticImpl("JspErrorCount"));
370         sessionsTotal = new MutableCountStatisticImpl(
371                         new CountStatisticImpl("SessionsTotal"));
372         activeSessionsCurrent = new MutableCountStatisticImpl(
373                         new CountStatisticImpl("ActiveSessionsCurrent"));
374         activeSessionsHigh = new MutableCountStatisticImpl(
375                         new CountStatisticImpl("ActiveSessionsHigh"));
376         rejectedSessionsTotal = new MutableCountStatisticImpl(
377                         new CountStatisticImpl("RejectedSessionsTotal"));
378         expiredSessionsTotal = new MutableCountStatisticImpl(
379                         new CountStatisticImpl("ExpiredSessionsTotal"));
380         processingTimeMillis = new MutableCountStatisticImpl(
381                     new CountStatisticImpl("ServletProcessingTimes"));
382         initTime = System.currentTimeMillis ();
383     }
384
385 }
386
Popular Tags