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.portlet;
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<PortletRequestDataSample> {
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> getPortletIds() {
081                    Set<String> portletIds = new HashSet<String>();
082    
083                    for (CompanyStatistics containerStatistics :
084                                    _companyStatisticsByWebId.values()) {
085    
086                            portletIds.addAll(containerStatistics.getPortletIds());
087                    }
088    
089                    return portletIds;
090            }
091    
092            public Set<String> getWebIds() {
093                    return _companyStatisticsByWebId.keySet();
094            }
095    
096            public void processDataSample(
097                            PortletRequestDataSample portletRequestDataSample)
098                    throws MonitoringException {
099    
100                    long companyId = portletRequestDataSample.getCompanyId();
101    
102                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
103                            companyId);
104    
105                    if (companyStatistics == null) {
106                            try {
107                                    Company company = _companyLocalService.getCompany(companyId);
108    
109                                    companyStatistics = register(company.getWebId());
110                            }
111                            catch (Exception e) {
112                                    throw new IllegalStateException(
113                                            "Unable to get company with company id " + companyId, e);
114                            }
115                    }
116    
117                    companyStatistics.processDataSample(portletRequestDataSample);
118            }
119    
120            public synchronized CompanyStatistics register(String webId) {
121                    CompanyStatistics companyStatistics = new CompanyStatistics(
122                            _companyLocalService, webId);
123    
124                    _companyStatisticsByCompanyId.put(
125                            companyStatistics.getCompanyId(), companyStatistics);
126                    _companyStatisticsByWebId.put(webId, companyStatistics);
127    
128                    return companyStatistics;
129            }
130    
131            public void reset() {
132                    for (long companyId : _companyStatisticsByCompanyId.keySet()) {
133                            reset(companyId);
134                    }
135            }
136    
137            public void reset(long companyId) {
138                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
139                            companyId);
140    
141                    if (companyStatistics == null) {
142                            return;
143                    }
144    
145                    companyStatistics.reset();
146            }
147    
148            public void reset(String webId) {
149                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
150                            webId);
151    
152                    if (companyStatistics == null) {
153                            return;
154                    }
155    
156                    companyStatistics.reset();
157            }
158    
159            public void setCompanyLocalService(
160                    CompanyLocalService companyLocalService) {
161    
162                    _companyLocalService = companyLocalService;
163            }
164    
165            public synchronized void unregister(String webId) {
166                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
167                            webId);
168    
169                    if (companyStatistics != null) {
170                            _companyStatisticsByCompanyId.remove(
171                                    companyStatistics.getCompanyId());
172                    }
173            }
174    
175            private CompanyLocalService _companyLocalService;
176            private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
177                    new TreeMap<Long, CompanyStatistics>();
178            private Map<String, CompanyStatistics> _companyStatisticsByWebId =
179                    new TreeMap<String, CompanyStatistics>();
180    
181    }