1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.monitoring.statistics.portal;
24  
25  import com.liferay.portal.model.Company;
26  import com.liferay.portal.monitoring.MonitoringException;
27  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
28  import com.liferay.portal.service.CompanyLocalService;
29  
30  import java.util.HashSet;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.TreeMap;
34  
35  /**
36   * <a href="ServerStatistics.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Michael C. Han
39   * @author Brian Wing Shun Chan
40   */
41  public class ServerStatistics
42      implements DataSampleProcessor<PortalRequestDataSample> {
43  
44      public void afterPropertiesSet() {
45          CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
46  
47          _companyStatisticsByCompanyId.put(
48              systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
49          _companyStatisticsByWebId.put(
50              systemCompanyStatistics.getWebId(), systemCompanyStatistics);
51      }
52  
53      public Set<Long> getCompanyIds() {
54          return _companyStatisticsByCompanyId.keySet();
55      }
56  
57      public CompanyStatistics getCompanyStatistics(long companyId)
58          throws MonitoringException {
59  
60          CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
61              companyId);
62  
63          if (companyStatistics == null) {
64              throw new MonitoringException(
65                  "No statistics found for company id " + companyId);
66          }
67  
68          return companyStatistics;
69      }
70  
71      public CompanyStatistics getCompanyStatistics(String webId)
72          throws MonitoringException {
73  
74          CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
75              webId);
76  
77          if (companyStatistics == null) {
78              throw new MonitoringException(
79                  "No statistics found for web id " + webId);
80          }
81  
82          return companyStatistics;
83      }
84  
85      public Set<CompanyStatistics> getCompanyStatisticsSet() {
86          return new HashSet<CompanyStatistics>(
87              _companyStatisticsByWebId.values());
88      }
89  
90      public Set<String> getWebIds() {
91          return _companyStatisticsByWebId.keySet();
92      }
93  
94      public void processDataSample(
95          PortalRequestDataSample portalRequestDataSample) {
96  
97          long companyId = portalRequestDataSample.getCompanyId();
98  
99          CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
100             companyId);
101 
102         if (companyStatistics == null) {
103             try {
104                 Company company = _companyLocalService.getCompany(companyId);
105 
106                 companyStatistics = register(company.getWebId());
107             }
108             catch (Exception e) {
109                 throw new IllegalStateException(
110                     "Unable to get company with company id " + companyId);
111             }
112         }
113 
114         companyStatistics.processDataSample(portalRequestDataSample);
115     }
116 
117     public synchronized CompanyStatistics register(String webId) {
118         CompanyStatistics companyStatistics = new CompanyStatistics(
119             _companyLocalService, webId);
120 
121         _companyStatisticsByCompanyId.put(
122             companyStatistics.getCompanyId(), companyStatistics);
123         _companyStatisticsByWebId.put(webId, companyStatistics);
124 
125         return companyStatistics;
126     }
127 
128     public void reset() {
129         for (long companyId : _companyStatisticsByCompanyId.keySet()) {
130             reset(companyId);
131         }
132     }
133 
134     public void reset(long companyId) {
135         CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
136             companyId);
137 
138         if (companyStatistics == null) {
139             return;
140         }
141 
142         companyStatistics.reset();
143     }
144 
145     public void reset(String webId) {
146         CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
147             webId);
148 
149         if (companyStatistics == null) {
150             return;
151         }
152 
153         companyStatistics.reset();
154     }
155 
156     public void setCompanyLocalService(
157         CompanyLocalService companyLocalService) {
158 
159         _companyLocalService = companyLocalService;
160     }
161 
162     public synchronized void unregister(String webId) {
163         CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
164             webId);
165 
166         if (companyStatistics != null) {
167             _companyStatisticsByCompanyId.remove(
168                 companyStatistics.getCompanyId());
169         }
170     }
171 
172     private CompanyLocalService _companyLocalService;
173     private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
174         new TreeMap<Long, CompanyStatistics>();
175     private Map<String, CompanyStatistics> _companyStatisticsByWebId =
176         new TreeMap<String, CompanyStatistics>();
177 
178 }