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