KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.web.stats;
24
25 import java.util.LinkedList JavaDoc;
26 import javax.management.j2ee.statistics.Statistic JavaDoc;
27 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
28 import com.sun.logging.LogDomains;
29 import com.sun.enterprise.admin.monitor.stats.PWCRequestStats;
30 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
31 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
32 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
33 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
34 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
35 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
36
37 /**
38  * Implementation of the PWCRequestStats interface, which exposes
39  * statistics about all HTTP listeners associated with a given virtual
40  * server.
41  */

42 public class PWCRequestStatsImpl implements PWCRequestStats {
43     
44     private GenericStatsImpl baseStatsImpl;
45
46     private long startTime;
47
48     /*
49      * The HTTP listeners associated with the virtual server whose stats
50      * are being exposed.
51      */

52     private LinkedList JavaDoc<HTTPListenerStatsImpl> httpListenerStats;
53
54     private StringStatistic lastMethod;
55     private StringStatistic lastUri;
56     private MutableCountStatistic countRequests;
57     private MutableCountStatistic countBytesReceived;
58     private MutableCountStatistic countBytesTransmitted;
59     private MutableCountStatistic rateBytesTransmitted;
60     private MutableCountStatistic maxByteTransmissionRate;
61     private MutableCountStatistic countOpenConnections;
62     private MutableCountStatistic maxOpenConnections;
63     private MutableCountStatistic count2xx;
64     private MutableCountStatistic count3xx;
65     private MutableCountStatistic count4xx;
66     private MutableCountStatistic count5xx;
67     private MutableCountStatistic countOther;
68     private MutableCountStatistic count200;
69     private MutableCountStatistic count302;
70     private MutableCountStatistic count304;
71     private MutableCountStatistic count400;
72     private MutableCountStatistic count401;
73     private MutableCountStatistic count403;
74     private MutableCountStatistic count404;
75     private MutableCountStatistic count503;
76     
77
78     /**
79      * Constructor.
80      *
81      * @param domain Domain name
82      */

83     public PWCRequestStatsImpl(String JavaDoc domain) {
84
85         httpListenerStats = new LinkedList JavaDoc();
86        
87         baseStatsImpl = new GenericStatsImpl(
88             com.sun.enterprise.admin.monitor.stats.PWCRequestStats.class,
89             this);
90         
91         // Initialize all the MutableStatistic Classes
92
initializeStatistics();
93     }
94
95     /**
96      * Adds the given <code>HTTPListenerStatsImpl</code> to the list of
97      * <code>HTTPListenerStatsImpl</code> from which this
98      * <code>PWCRequestStatsImpl</code> gathers its stats.
99      *
100      * @param stats The <code>HTTPListenerStatsImpl</code> instance to add
101      * to the list
102      */

103     public void addHttpListenerStats(HTTPListenerStatsImpl stats) {
104         httpListenerStats.add(stats);
105     }
106
107     /**
108      * Gets the method of the last request serviced.
109      *
110      * @return Method of the last request serviced
111      */

112     public StringStatistic getMethod() {
113
114         String JavaDoc lastMethod = "unknown";
115         long lastRequestTime = 0;
116
117         int size = httpListenerStats.size();
118         for (int i=0; i<size; i++) {
119             HTTPListenerStatsImpl listener = httpListenerStats.get(i);
120             if (listener.getLastRequestCompletionTime() > lastRequestTime) {
121                 lastRequestTime = listener.getLastRequestCompletionTime();
122                 lastMethod = listener.getLastRequestMethod();
123             }
124         }
125
126         return new StringStatisticImpl(
127                                 lastMethod,
128                                 "method",
129                                 "String",
130                                 "Method of the last request serviced",
131                                 startTime,
132                                 System.currentTimeMillis());
133     }
134
135     /**
136      * Gets the URI of the last request serviced.
137      *
138      * @return URI of the last request serviced
139      */

140     public StringStatistic getUri() {
141
142         String JavaDoc lastURI = "unknown";
143         long lastRequestTime = 0;
144
145         int size = httpListenerStats.size();
146         for (int i=0; i<size; i++) {
147             HTTPListenerStatsImpl listener = httpListenerStats.get(i);
148             if (listener.getLastRequestCompletionTime() > lastRequestTime) {
149                 lastRequestTime = listener.getLastRequestCompletionTime();
150                 lastURI = listener.getLastRequestURI();
151             }
152         }
153
154         return new StringStatisticImpl(
155                                 lastURI,
156                                 "uri",
157                                 "String",
158                                 "URI of the last request serviced",
159                                 startTime,
160                                 System.currentTimeMillis());
161     }
162     
163     /**
164      * Gets the number of requests serviced.
165      *
166      * @return Number of requests serviced
167      */

168     public CountStatistic JavaDoc getCountRequests() {
169
170         long count = 0;
171         int size = httpListenerStats.size();
172         for (int i=0; i<size; i++) {
173             count += httpListenerStats.get(i).getRequestCountLong();
174         }
175
176         countRequests.setCount(count);
177         return (CountStatistic JavaDoc)countRequests.unmodifiableView();
178     }
179
180     /**
181      * Gets the number of bytes received.
182      *
183      * @return Number of bytes received, or 0 if this information is
184      * not available
185      */

186     public CountStatistic JavaDoc getCountBytesReceived() {
187
188         long count = 0;
189         int size = httpListenerStats.size();
190         for (int i=0; i<size; i++) {
191             count += httpListenerStats.get(i).getBytesReceivedLong();
192         }
193
194         countBytesReceived.setCount(count);
195         return (CountStatistic JavaDoc)countBytesReceived.unmodifiableView();
196     }
197     
198     /**
199      * Gets the number of bytes transmitted.
200      *
201      * @return Number of bytes transmitted, or 0 if this information
202      * is not available
203      */

204     public CountStatistic JavaDoc getCountBytesTransmitted() {
205
206         long count = 0;
207         int size = httpListenerStats.size();
208         for (int i=0; i<size; i++) {
209             count += httpListenerStats.get(i).getBytesSentLong();
210         }
211
212         countBytesTransmitted.setCount(count);
213         return (CountStatistic JavaDoc)countBytesTransmitted.unmodifiableView();
214     }
215     
216     /**
217      * Gets the rate (in bytes per second) at which data was transmitted
218      * over some server-defined interval.
219      *
220      * @return Rate (in bytes per second) at which data was
221      * transmitted over some server-defined interval, or 0 if this
222      * information is not available
223      */

224     public CountStatistic JavaDoc getRateBytesTransmitted() {
225         rateBytesTransmitted.setCount(0);
226         return (CountStatistic JavaDoc)rateBytesTransmitted.unmodifiableView();
227     }
228     
229     /**
230      * Gets the maximum rate at which data was transmitted over some
231      * server-defined interval.
232      *
233      * @return Maximum rate at which data was transmitted over some
234      * server-defined interval, or 0 if this information is not available.
235      */

236     public CountStatistic JavaDoc getMaxByteTransmissionRate() {
237         maxByteTransmissionRate.setCount(0);
238         return (CountStatistic JavaDoc)maxByteTransmissionRate.unmodifiableView();
239     }
240     
241     /**
242      * Gets the number of open connections.
243      *
244      * @return Number of open connections, or 0 if this information
245      * is not available
246      */

247     public CountStatistic JavaDoc getCountOpenConnections() {
248
249         long count = 0;
250         int size = httpListenerStats.size();
251         for (int i=0; i<size; i++) {
252             count += httpListenerStats.get(i).getCountOpenConnectionsLong();
253         }
254
255         countOpenConnections.setCount(count);
256         return (CountStatistic JavaDoc)countOpenConnections.unmodifiableView();
257     }
258     
259     /**
260      * Gets the maximum number of open connections.
261      *
262      * @return Maximum number of open connections, or 0 if this
263      * information is not available
264      */

265     public CountStatistic JavaDoc getMaxOpenConnections() {
266
267         long count = 0;
268         int size = httpListenerStats.size();
269         for (int i=0; i<size; i++) {
270             count += httpListenerStats.get(i).getMaxOpenConnectionsLong();
271         }
272
273         maxOpenConnections.setCount(count);
274         return (CountStatistic JavaDoc)maxOpenConnections.unmodifiableView();
275     }
276
277     /**
278      * Gets the number of 200-level responses sent.
279      *
280      * @return Number of 200-level responses sent
281      */

282     public CountStatistic JavaDoc getCount2xx() {
283
284         long count = 0;
285         int size = httpListenerStats.size();
286         for (int i=0; i<size; i++) {
287             count += httpListenerStats.get(i).getCount2xxLong();
288         }
289
290         count2xx.setCount(count);
291         return (CountStatistic JavaDoc)count2xx.unmodifiableView();
292     }
293     
294     /**
295      * Gets the number of 300-level responses sent.
296      *
297      * @return Number of 300-level responses sent
298      */

299     public CountStatistic JavaDoc getCount3xx() {
300
301         long count = 0;
302         int size = httpListenerStats.size();
303         for (int i=0; i<size; i++) {
304             count += httpListenerStats.get(i).getCount3xxLong();
305         }
306
307         count3xx.setCount(count);
308         return (CountStatistic JavaDoc)count3xx.unmodifiableView();
309     }
310
311     /**
312      * Gets the number of 400-level responses sent.
313      *
314      * @return Number of 400-level responses sent
315      */

316     public CountStatistic JavaDoc getCount4xx() {
317
318         long count = 0;
319         int size = httpListenerStats.size();
320         for (int i=0; i<size; i++) {
321             count += httpListenerStats.get(i).getCount4xxLong();
322         }
323
324         count4xx.setCount(count);
325         return (CountStatistic JavaDoc)count4xx.unmodifiableView();
326     }
327
328     /**
329      * Gets the number of 500-level responses sent.
330      *
331      * @return Number of 500-level responses sent
332      */

333     public CountStatistic JavaDoc getCount5xx() {
334
335         long count = 0;
336         int size = httpListenerStats.size();
337         for (int i=0; i<size; i++) {
338             count += httpListenerStats.get(i).getCount5xxLong();
339         }
340
341         count5xx.setCount(count);
342         return (CountStatistic JavaDoc)count5xx.unmodifiableView();
343     }
344     
345     /**
346      * Gets the number of responses sent that were not 200, 300, 400,
347      * or 500 level.
348      *
349      * @return Number of responses sent that were not 200, 300, 400,
350      * or 500 level
351      */

352     public CountStatistic JavaDoc getCountOther() {
353
354         long count = 0;
355         int size = httpListenerStats.size();
356         for (int i=0; i<size; i++) {
357             count += httpListenerStats.get(i).getCountOtherLong();
358         }
359
360         countOther.setCount(count);
361         return (CountStatistic JavaDoc)countOther.unmodifiableView();
362     }
363
364     /**
365      * Gets the number of responses with a 200 response code.
366      *
367      * @return Number of responses with a 200 response code
368      */

369     public CountStatistic JavaDoc getCount200() {
370
371         long count = 0;
372         int size = httpListenerStats.size();
373         for (int i=0; i<size; i++) {
374             count += httpListenerStats.get(i).getCount200Long();
375         }
376
377         count200.setCount(count);
378         return (CountStatistic JavaDoc)count200.unmodifiableView();
379     }
380
381     /**
382      * Gets the number of responses with a 302 response code.
383      *
384      * @return Number of responses with a 302 response code
385      */

386     public CountStatistic JavaDoc getCount302() {
387
388         long count = 0;
389         int size = httpListenerStats.size();
390         for (int i=0; i<size; i++) {
391             count += httpListenerStats.get(i).getCount302Long();
392         }
393
394         count302.setCount(count);
395         return (CountStatistic JavaDoc)count302.unmodifiableView();
396     }
397
398     /**
399      * Gets the number of responses with a 304 response code.
400      *
401      * @return Number of responses with a 304 response code
402      */

403     public CountStatistic JavaDoc getCount304() {
404
405         long count = 0;
406         int size = httpListenerStats.size();
407         for (int i=0; i<size; i++) {
408             count += httpListenerStats.get(i).getCount304Long();
409         }
410
411         count304.setCount(count);
412         return (CountStatistic JavaDoc)count304.unmodifiableView();
413     }
414
415     /**
416      * Gets the number of responses with a 400 response code.
417      *
418      * @return Number of responses with a 400 response code
419      */

420     public CountStatistic JavaDoc getCount400() {
421
422         long count = 0;
423         int size = httpListenerStats.size();
424         for (int i=0; i<size; i++) {
425             count += httpListenerStats.get(i).getCount400Long();
426         }
427
428         count400.setCount(count);
429         return (CountStatistic JavaDoc)count400.unmodifiableView();
430     }
431     
432     /**
433      * Gets the number of responses with a 401 response code.
434      *
435      * @return Number of responses with a 401 response code
436      */

437     public CountStatistic JavaDoc getCount401() {
438
439         long count = 0;
440         int size = httpListenerStats.size();
441         for (int i=0; i<size; i++) {
442             count += httpListenerStats.get(i).getCount401Long();
443         }
444
445         count401.setCount(count);
446         return (CountStatistic JavaDoc)count401.unmodifiableView();
447     }
448     
449     /**
450      * Gets the number of responses with a 403 response code.
451      *
452      * @return Number of responses with a 403 response code
453      */

454     public CountStatistic JavaDoc getCount403() {
455
456         long count = 0;
457         int size = httpListenerStats.size();
458         for (int i=0; i<size; i++) {
459             count += httpListenerStats.get(i).getCount403Long();
460         }
461
462         count403.setCount(count);
463         return (CountStatistic JavaDoc)count403.unmodifiableView();
464     }
465
466     /**
467      * Gets the number of responses with a 404 response code.
468      *
469      * @return Number of responses with a 404 response code
470      */

471     public CountStatistic JavaDoc getCount404() {
472
473         long count = 0;
474         int size = httpListenerStats.size();
475         for (int i=0; i<size; i++) {
476             count += httpListenerStats.get(i).getCount404Long();
477         }
478
479         count404.setCount(count);
480         return (CountStatistic JavaDoc)count404.unmodifiableView();
481     }
482
483     /**
484      * Gets the number of responses with a 503 response code.
485      *
486      * @return Number of responses with a 503 response code
487      */

488     public CountStatistic JavaDoc getCount503() {
489
490         long count = 0;
491         int size = httpListenerStats.size();
492         for (int i=0; i<size; i++) {
493             count += httpListenerStats.get(i).getCount503Long();
494         }
495
496         count503.setCount(count);
497         return (CountStatistic JavaDoc)count503.unmodifiableView();
498     }
499
500     /**
501      * Gets all statistics exposed by this PWCRequestStatsImpl
502      *
503      * @return Array of all statistics exposed by this PWCRequestStatsImpl
504      */

505     public Statistic JavaDoc[] getStatistics() {
506         return baseStatsImpl.getStatistics();
507     }
508
509     /**
510      * Gets the statistic with the given name.
511      *
512      * @param name Statistic name
513      *
514      * @return Statistic with the given name
515      */

516     public Statistic JavaDoc getStatistic(String JavaDoc name) {
517         return baseStatsImpl.getStatistic(name);
518     }
519
520     /**
521      * Gets the names of all statistics exposed by this PWCRequestStatsImpl
522      *
523      * @return Array of the names of all statistics exposed by this
524      * PWCRequestStatsImpl
525      */

526     public String JavaDoc[] getStatisticNames() {
527         return baseStatsImpl.getStatisticNames();
528     }
529
530    
531     private void initializeStatistics() {
532
533         startTime = System.currentTimeMillis();
534         
535         CountStatistic JavaDoc c = new CountStatisticImpl("CountRequests");
536         countRequests = new MutableCountStatisticImpl(c);
537         
538         c = new CountStatisticImpl("CountBytesReceived");
539         countBytesReceived = new MutableCountStatisticImpl(c);
540         
541         c = new CountStatisticImpl("CountBytesTransmitted");
542         countBytesTransmitted = new MutableCountStatisticImpl(c);
543         
544         c = new CountStatisticImpl("RateBytesTransmitted");
545         rateBytesTransmitted = new MutableCountStatisticImpl(c);
546
547         c = new CountStatisticImpl("MaxByteTransmissionRate");
548         maxByteTransmissionRate = new MutableCountStatisticImpl(c);
549         
550         c = new CountStatisticImpl("CountOpenConnections");
551         countOpenConnections = new MutableCountStatisticImpl(c);
552
553         c = new CountStatisticImpl("MaxOpenConnections");
554         maxOpenConnections = new MutableCountStatisticImpl(c);
555         
556         c = new CountStatisticImpl("Count2xx");
557         count2xx = new MutableCountStatisticImpl(c);
558
559         c = new CountStatisticImpl("Count3xx");
560         count3xx = new MutableCountStatisticImpl(c);
561
562         c = new CountStatisticImpl("Count4xx");
563         count4xx = new MutableCountStatisticImpl(c);
564
565         c = new CountStatisticImpl("Count5xx");
566         count5xx = new MutableCountStatisticImpl(c);
567
568         c = new CountStatisticImpl("CountOther");
569         countOther = new MutableCountStatisticImpl(c);
570
571         c = new CountStatisticImpl("Count200");
572         count200 = new MutableCountStatisticImpl(c);
573
574         c = new CountStatisticImpl("Count302");
575         count302 = new MutableCountStatisticImpl(c);
576
577         c = new CountStatisticImpl("Count304");
578         count304 = new MutableCountStatisticImpl(c);
579
580         c = new CountStatisticImpl("Count400");
581         count400 = new MutableCountStatisticImpl(c);
582
583         c = new CountStatisticImpl("Count401");
584         count401 = new MutableCountStatisticImpl(c);
585
586         c = new CountStatisticImpl("Count403");
587         count403 = new MutableCountStatisticImpl(c);
588
589         c = new CountStatisticImpl("Count404");
590         count404 = new MutableCountStatisticImpl(c);
591
592         c = new CountStatisticImpl("Count503");
593         count503 = new MutableCountStatisticImpl(c);
594     }
595
596 }
597
Popular Tags