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