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.portal;
016    
017    import com.liferay.portal.model.Company;
018    import com.liferay.portal.monitoring.MonitoringException;
019    import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
020    import com.liferay.portal.service.CompanyLocalService;
021    
022    import java.util.HashSet;
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.TreeMap;
026    
027    /**
028     * @author Michael C. Han
029     * @author Brian Wing Shun Chan
030     */
031    public class ServerStatistics
032            implements DataSampleProcessor<PortalRequestDataSample> {
033    
034            public void afterPropertiesSet() {
035                    CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
036    
037                    _companyStatisticsByCompanyId.put(
038                            systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
039                    _companyStatisticsByWebId.put(
040                            systemCompanyStatistics.getWebId(), systemCompanyStatistics);
041            }
042    
043            public Set<Long> getCompanyIds() {
044                    return _companyStatisticsByCompanyId.keySet();
045            }
046    
047            public CompanyStatistics getCompanyStatistics(long companyId)
048                    throws MonitoringException {
049    
050                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
051                            companyId);
052    
053                    if (companyStatistics == null) {
054                            throw new MonitoringException(
055                                    "No statistics found for company id " + companyId);
056                    }
057    
058                    return companyStatistics;
059            }
060    
061            public CompanyStatistics getCompanyStatistics(String webId)
062                    throws MonitoringException {
063    
064                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
065                            webId);
066    
067                    if (companyStatistics == null) {
068                            throw new MonitoringException(
069                                    "No statistics found for web id " + webId);
070                    }
071    
072                    return companyStatistics;
073            }
074    
075            public Set<CompanyStatistics> getCompanyStatisticsSet() {
076                    return new HashSet<CompanyStatistics>(
077                            _companyStatisticsByWebId.values());
078            }
079    
080            public Set<String> getWebIds() {
081                    return _companyStatisticsByWebId.keySet();
082            }
083    
084            public void processDataSample(
085                    PortalRequestDataSample portalRequestDataSample) {
086    
087                    long companyId = portalRequestDataSample.getCompanyId();
088    
089                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
090                            companyId);
091    
092                    if (companyStatistics == null) {
093                            try {
094                                    Company company = _companyLocalService.getCompany(companyId);
095    
096                                    companyStatistics = register(company.getWebId());
097                            }
098                            catch (Exception e) {
099                                    throw new IllegalStateException(
100                                            "Unable to get company with company id " + companyId);
101                            }
102                    }
103    
104                    companyStatistics.processDataSample(portalRequestDataSample);
105            }
106    
107            public synchronized CompanyStatistics register(String webId) {
108                    CompanyStatistics companyStatistics = new CompanyStatistics(
109                            _companyLocalService, webId);
110    
111                    _companyStatisticsByCompanyId.put(
112                            companyStatistics.getCompanyId(), companyStatistics);
113                    _companyStatisticsByWebId.put(webId, companyStatistics);
114    
115                    return companyStatistics;
116            }
117    
118            public void reset() {
119                    for (long companyId : _companyStatisticsByCompanyId.keySet()) {
120                            reset(companyId);
121                    }
122            }
123    
124            public void reset(long companyId) {
125                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
126                            companyId);
127    
128                    if (companyStatistics == null) {
129                            return;
130                    }
131    
132                    companyStatistics.reset();
133            }
134    
135            public void reset(String webId) {
136                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
137                            webId);
138    
139                    if (companyStatistics == null) {
140                            return;
141                    }
142    
143                    companyStatistics.reset();
144            }
145    
146            public void setCompanyLocalService(
147                    CompanyLocalService companyLocalService) {
148    
149                    _companyLocalService = companyLocalService;
150            }
151    
152            public synchronized void unregister(String webId) {
153                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
154                            webId);
155    
156                    if (companyStatistics != null) {
157                            _companyStatisticsByCompanyId.remove(
158                                    companyStatistics.getCompanyId());
159                    }
160            }
161    
162            private CompanyLocalService _companyLocalService;
163            private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
164                    new TreeMap<Long, CompanyStatistics>();
165            private Map<String, CompanyStatistics> _companyStatisticsByWebId =
166                    new TreeMap<String, CompanyStatistics>();
167    
168    }