001    /**
002     * Copyright (c) 2000-2013 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.monitoring.statistics.DataSampleProcessor;
018    import com.liferay.portal.monitoring.jmx.MethodSignature;
019    
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    /**
024     * @author Michael C. Han
025     */
026    public class ServerStatistics
027            implements DataSampleProcessor<ServiceRequestDataSample> {
028    
029            public long getAverageTime(
030                    String className, String methodName, String[] parameterTypes) {
031    
032                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
033    
034                    if (serviceStatistics != null) {
035                            return serviceStatistics.getAverageTime(methodName, parameterTypes);
036                    }
037    
038                    return -1;
039            }
040    
041            public long getErrorCount(
042                    String className, String methodName, String[] parameterTypes) {
043    
044                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
045    
046                    if (serviceStatistics != null) {
047                            return serviceStatistics.getErrorCount(methodName, parameterTypes);
048                    }
049    
050                    return -1;
051            }
052    
053            public long getMaxTime(
054                    String className, String methodName, String[] parameterTypes) {
055    
056                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
057    
058                    if (serviceStatistics != null) {
059                            return serviceStatistics.getMaxTime(methodName, parameterTypes);
060                    }
061    
062                    return -1;
063            }
064    
065            public long getMinTime(
066                    String className, String methodName, String[] parameterTypes) {
067    
068                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
069    
070                    if (serviceStatistics != null) {
071                            return serviceStatistics.getMinTime(methodName, parameterTypes);
072                    }
073    
074                    return -1;
075            }
076    
077            public long getRequestCount(
078                    String className, String methodName, String[] parameterTypes) {
079    
080                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
081    
082                    if (serviceStatistics != null) {
083                            return serviceStatistics.getRequestCount(
084                                    methodName, parameterTypes);
085                    }
086    
087                    return -1;
088            }
089    
090            @Override
091            public void processDataSample(
092                    ServiceRequestDataSample serviceRequestDataSample) {
093    
094                    MethodSignature methodSignature =
095                            serviceRequestDataSample.getMethodSignature();
096    
097                    String className = methodSignature.getClassName();
098    
099                    ServiceStatistics serviceStatistics = _serviceStatistics.get(className);
100    
101                    if (serviceStatistics == null) {
102                            serviceStatistics = new ServiceStatistics(className);
103    
104                            _serviceStatistics.put(className, serviceStatistics);
105                    }
106    
107                    serviceStatistics.processDataSample(serviceRequestDataSample);
108            }
109    
110            private Map<String, ServiceStatistics> _serviceStatistics =
111                    new ConcurrentHashMap<String, ServiceStatistics>();
112    
113    }