1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.monitoring.statistics.service;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.util.MethodKey;
19  import com.liferay.portal.monitoring.RequestStatus;
20  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
21  import com.liferay.portal.monitoring.statistics.RequestStatistics;
22  
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  /**
27   * <a href="ServiceStatistics.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Michael C. Han
30   */
31  public class ServiceStatistics
32      implements DataSampleProcessor<ServiceRequestDataSample> {
33  
34      public ServiceStatistics(String className) {
35          _className = className;
36      }
37  
38      public long getAverageTime(String methodName, String[] parameterTypes)
39          throws SystemException {
40  
41          try {
42              MethodKey methodKey = new MethodKey(
43                  _className, methodName, parameterTypes);
44  
45              RequestStatistics requestStatistics = _methodRequestStatistics.get(
46                  methodKey);
47  
48              if (requestStatistics != null) {
49                  return requestStatistics.getAverageTime();
50              }
51          }
52          catch (ClassNotFoundException e) {
53              throw new SystemException(e);
54          }
55  
56          return -1;
57      }
58  
59      public long getErrorCount(String methodName, String[] parameterTypes)
60          throws SystemException {
61  
62          try {
63              MethodKey methodKey = new MethodKey(
64                  _className, methodName, parameterTypes);
65  
66              RequestStatistics requestStatistics = _methodRequestStatistics.get(
67                  methodKey);
68  
69              if (requestStatistics != null) {
70                  return requestStatistics.getErrorCount();
71              }
72          }
73          catch (ClassNotFoundException e) {
74              throw new SystemException(e);
75          }
76  
77          return -1;
78      }
79  
80      public long getMaxTime(String methodName, String[] parameterTypes)
81          throws SystemException {
82  
83          try {
84              MethodKey methodKey = new MethodKey(
85                  _className, methodName, parameterTypes);
86  
87              RequestStatistics requestStatistics = _methodRequestStatistics.get(
88                  methodKey);
89  
90              if (requestStatistics != null) {
91                  return requestStatistics.getMaxTime();
92              }
93          }
94          catch (ClassNotFoundException e) {
95              throw new SystemException(e);
96          }
97  
98          return -1;
99      }
100 
101     public long getMinTime(String methodName, String[] parameterTypes)
102         throws SystemException {
103 
104         try {
105             MethodKey methodKey = new MethodKey(
106                 _className, methodName, parameterTypes);
107 
108             RequestStatistics requestStatistics = _methodRequestStatistics.get(
109                 methodKey);
110 
111             if (requestStatistics != null) {
112                 return requestStatistics.getMinTime();
113             }
114         }
115         catch (ClassNotFoundException e) {
116             throw new SystemException(e);
117         }
118 
119         return -1;
120     }
121 
122     public long getRequestCount(String methodName, String[] parameterTypes)
123         throws SystemException {
124 
125         try {
126             MethodKey methodKey = new MethodKey(
127                 _className, methodName, parameterTypes);
128 
129             RequestStatistics requestStatistics = _methodRequestStatistics.get(
130                 methodKey);
131 
132             if (requestStatistics != null) {
133                 return requestStatistics.getRequestCount();
134             }
135         }
136         catch (ClassNotFoundException e) {
137             throw new SystemException(e);
138         }
139 
140         return -1;
141     }
142 
143     public void processDataSample(
144         ServiceRequestDataSample serviceRequestDataSample) {
145 
146         MethodKey methodKey = serviceRequestDataSample.getMethodKey();
147 
148         RequestStatistics requestStatistics = _methodRequestStatistics.get(
149             methodKey);
150 
151         if (requestStatistics == null) {
152             requestStatistics = new RequestStatistics(methodKey.toString());
153 
154             _methodRequestStatistics.put(methodKey, requestStatistics);
155         }
156 
157         RequestStatus requestStatus =
158             serviceRequestDataSample.getRequestStatus();
159 
160         if (requestStatus == RequestStatus.ERROR) {
161             requestStatistics.incrementError();
162         }
163         else if (requestStatus == RequestStatus.TIMEOUT) {
164             requestStatistics.incrementTimeout();
165         }
166         else if (requestStatus == RequestStatus.SUCCESS) {
167             requestStatistics.incrementSuccessDuration(
168                 serviceRequestDataSample.getDuration());
169         }
170     }
171 
172     private String _className;
173     private Map<MethodKey, RequestStatistics> _methodRequestStatistics =
174         new ConcurrentHashMap<MethodKey, RequestStatistics>();
175 
176 }