KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > monitor > PortalMonitor


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.container.monitor;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import org.exoplatform.container.SessionContainer;
10 /**
11  * Tue, May 27, 2003 @
12  * @author: Tuan Nguyen
13  * @version: $Id: PortalMonitorServiceImpl.java,v 1.2 2004/04/29 15:07:27 tuan08 Exp $
14  * @since: 0.0
15  * @email: tuan08@yahoo.com
16  */

17 public class PortalMonitor {
18
19   static int NUMBER_OF_REQUEST_MONITOR = 10 ;
20   static long TIME_RANGE = 200 ; // 200ms
21

22   private List JavaDoc requestMonitors_ ;
23   private PortalMonitorListenerStack listeners_ ;
24   
25   public PortalMonitor() {
26     requestMonitors_ = new ArrayList JavaDoc() ;
27     long min = 0 ;
28     long max = TIME_RANGE - 1;
29     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR ; i++) {
30       requestMonitors_.add(new RequestMonitorData(min, max)) ;
31       min += TIME_RANGE ;
32       max += TIME_RANGE ;
33     }
34     listeners_ = new PortalMonitorListenerStack(3) ;
35   }
36   
37   public void addListener(PortalMonitorListener listener) {
38     listeners_.add(listener) ;
39   }
40   
41   final public void log(ActionData data) {
42     long executionTime = data.getHandleTime();
43     int index = (int) (executionTime / TIME_RANGE) ;
44     if (index >= NUMBER_OF_REQUEST_MONITOR) index = NUMBER_OF_REQUEST_MONITOR - 1 ;
45     RequestMonitorData monitor = (RequestMonitorData) requestMonitors_.get(index) ;
46     monitor.logRequest(executionTime) ;
47   }
48
49   public void error(String JavaDoc errorMessage, Throwable JavaDoc t) {
50     listeners_.onError(this, errorMessage, t) ;
51     SessionContainer scontainer = SessionContainer.getInstance() ;
52     if(scontainer != null) {
53       scontainer.getMonitor().error(errorMessage, t) ;
54     }
55   }
56
57   final public List JavaDoc getRequestMonitorData() { return requestMonitors_ ; }
58   
59   final public int getRequestCounter() {
60     int counter = 0 ;
61     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR ; i++) {
62       RequestMonitorData monitor = (RequestMonitorData) requestMonitors_.get(i) ;
63       counter += monitor.getRequestCounter() ;
64     }
65     return counter ;
66   }
67
68   final public long minExecutionTime() {
69     long minExecutionTime = 0 ;
70     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR ; i++) {
71       RequestMonitorData monitor = (RequestMonitorData) requestMonitors_.get(i) ;
72       if (i == 0) {
73         minExecutionTime = monitor.minExecutionTime() ;
74       } else {
75         if (monitor.minExecutionTime() < minExecutionTime)
76           minExecutionTime = monitor.minExecutionTime() ;
77       }
78     }
79     return minExecutionTime ;
80   }
81
82   final public long maxExecutionTime() {
83     long maxExecutionTime = 0 ;
84     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR ; i++) {
85       RequestMonitorData monitor = (RequestMonitorData) requestMonitors_.get(i) ;
86       if (i == 0) {
87         maxExecutionTime = monitor.maxExecutionTime() ;
88       } else {
89         if (monitor.maxExecutionTime() > maxExecutionTime)
90           maxExecutionTime = monitor.maxExecutionTime() ;
91       }
92     }
93     return maxExecutionTime ;
94   }
95
96   final public long averageExecutionTime() {
97     long sumExecutionTime = 0 ;
98     int requestCounter = 0 ;
99     for (int i = 0; i < NUMBER_OF_REQUEST_MONITOR ; i++) {
100       RequestMonitorData monitor = (RequestMonitorData) requestMonitors_.get(i) ;
101       sumExecutionTime += monitor.sumExecutionTime() ;
102       requestCounter += monitor.getRequestCounter() ;
103     }
104     if (requestCounter == 0) return 0 ;
105     return sumExecutionTime/requestCounter ;
106   }
107 }
Popular Tags