001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.monitoring.statistics.service;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.MethodKey;
019    import com.liferay.portal.monitoring.RequestStatus;
020    import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
021    import com.liferay.portal.monitoring.statistics.RequestStatistics;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class ServiceStatistics
030            implements DataSampleProcessor<ServiceRequestDataSample> {
031    
032            public ServiceStatistics(String className) {
033                    _className = className;
034            }
035    
036            public long getAverageTime(String methodName, String[] parameterTypes)
037                    throws SystemException {
038    
039                    try {
040                            MethodKey methodKey = new MethodKey(
041                                    _className, methodName, parameterTypes);
042    
043                            RequestStatistics requestStatistics = _methodRequestStatistics.get(
044                                    methodKey);
045    
046                            if (requestStatistics != null) {
047                                    return requestStatistics.getAverageTime();
048                            }
049                    }
050                    catch (ClassNotFoundException e) {
051                            throw new SystemException(e);
052                    }
053    
054                    return -1;
055            }
056    
057            public long getErrorCount(String methodName, String[] parameterTypes)
058                    throws SystemException {
059    
060                    try {
061                            MethodKey methodKey = new MethodKey(
062                                    _className, methodName, parameterTypes);
063    
064                            RequestStatistics requestStatistics = _methodRequestStatistics.get(
065                                    methodKey);
066    
067                            if (requestStatistics != null) {
068                                    return requestStatistics.getErrorCount();
069                            }
070                    }
071                    catch (ClassNotFoundException e) {
072                            throw new SystemException(e);
073                    }
074    
075                    return -1;
076            }
077    
078            public long getMaxTime(String methodName, String[] parameterTypes)
079                    throws SystemException {
080    
081                    try {
082                            MethodKey methodKey = new MethodKey(
083                                    _className, methodName, parameterTypes);
084    
085                            RequestStatistics requestStatistics = _methodRequestStatistics.get(
086                                    methodKey);
087    
088                            if (requestStatistics != null) {
089                                    return requestStatistics.getMaxTime();
090                            }
091                    }
092                    catch (ClassNotFoundException e) {
093                            throw new SystemException(e);
094                    }
095    
096                    return -1;
097            }
098    
099            public long getMinTime(String methodName, String[] parameterTypes)
100                    throws SystemException {
101    
102                    try {
103                            MethodKey methodKey = new MethodKey(
104                                    _className, methodName, parameterTypes);
105    
106                            RequestStatistics requestStatistics = _methodRequestStatistics.get(
107                                    methodKey);
108    
109                            if (requestStatistics != null) {
110                                    return requestStatistics.getMinTime();
111                            }
112                    }
113                    catch (ClassNotFoundException e) {
114                            throw new SystemException(e);
115                    }
116    
117                    return -1;
118            }
119    
120            public long getRequestCount(String methodName, String[] parameterTypes)
121                    throws SystemException {
122    
123                    try {
124                            MethodKey methodKey = new MethodKey(
125                                    _className, methodName, parameterTypes);
126    
127                            RequestStatistics requestStatistics = _methodRequestStatistics.get(
128                                    methodKey);
129    
130                            if (requestStatistics != null) {
131                                    return requestStatistics.getRequestCount();
132                            }
133                    }
134                    catch (ClassNotFoundException e) {
135                            throw new SystemException(e);
136                    }
137    
138                    return -1;
139            }
140    
141            public void processDataSample(
142                    ServiceRequestDataSample serviceRequestDataSample) {
143    
144                    MethodKey methodKey = serviceRequestDataSample.getMethodKey();
145    
146                    RequestStatistics requestStatistics = _methodRequestStatistics.get(
147                            methodKey);
148    
149                    if (requestStatistics == null) {
150                            requestStatistics = new RequestStatistics(methodKey.toString());
151    
152                            _methodRequestStatistics.put(methodKey, requestStatistics);
153                    }
154    
155                    RequestStatus requestStatus =
156                            serviceRequestDataSample.getRequestStatus();
157    
158                    if (requestStatus == RequestStatus.ERROR) {
159                            requestStatistics.incrementError();
160                    }
161                    else if (requestStatus == RequestStatus.TIMEOUT) {
162                            requestStatistics.incrementTimeout();
163                    }
164                    else if (requestStatus == RequestStatus.SUCCESS) {
165                            requestStatistics.incrementSuccessDuration(
166                                    serviceRequestDataSample.getDuration());
167                    }
168            }
169    
170            private String _className;
171            private Map<MethodKey, RequestStatistics> _methodRequestStatistics =
172                    new ConcurrentHashMap<MethodKey, RequestStatistics>();
173    
174    }