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.portlet;
016    
017    import com.liferay.portal.kernel.monitoring.MonitoringException;
018    import com.liferay.portal.kernel.monitoring.statistics.DataSampleProcessor;
019    import com.liferay.portal.model.Company;
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            @Override
097            public void processDataSample(
098                            PortletRequestDataSample portletRequestDataSample)
099                    throws MonitoringException {
100    
101                    long companyId = portletRequestDataSample.getCompanyId();
102    
103                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
104                            companyId);
105    
106                    if (companyStatistics == null) {
107                            try {
108                                    Company company = _companyLocalService.getCompany(companyId);
109    
110                                    companyStatistics = register(company.getWebId());
111                            }
112                            catch (Exception e) {
113                                    throw new IllegalStateException(
114                                            "Unable to get company with company id " + companyId, e);
115                            }
116                    }
117    
118                    companyStatistics.processDataSample(portletRequestDataSample);
119            }
120    
121            public synchronized CompanyStatistics register(String webId) {
122                    CompanyStatistics companyStatistics = new CompanyStatistics(
123                            _companyLocalService, webId);
124    
125                    _companyStatisticsByCompanyId.put(
126                            companyStatistics.getCompanyId(), companyStatistics);
127                    _companyStatisticsByWebId.put(webId, companyStatistics);
128    
129                    return companyStatistics;
130            }
131    
132            public void reset() {
133                    for (long companyId : _companyStatisticsByCompanyId.keySet()) {
134                            reset(companyId);
135                    }
136            }
137    
138            public void reset(long companyId) {
139                    CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
140                            companyId);
141    
142                    if (companyStatistics == null) {
143                            return;
144                    }
145    
146                    companyStatistics.reset();
147            }
148    
149            public void reset(String webId) {
150                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
151                            webId);
152    
153                    if (companyStatistics == null) {
154                            return;
155                    }
156    
157                    companyStatistics.reset();
158            }
159    
160            public void setCompanyLocalService(
161                    CompanyLocalService companyLocalService) {
162    
163                    _companyLocalService = companyLocalService;
164            }
165    
166            public synchronized void unregister(String webId) {
167                    CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
168                            webId);
169    
170                    if (companyStatistics != null) {
171                            _companyStatisticsByCompanyId.remove(
172                                    companyStatistics.getCompanyId());
173                    }
174            }
175    
176            private CompanyLocalService _companyLocalService;
177            private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
178                    new TreeMap<Long, CompanyStatistics>();
179            private Map<String, CompanyStatistics> _companyStatisticsByWebId =
180                    new TreeMap<String, CompanyStatistics>();
181    
182    }