1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.monitoring.statistics.portlet;
16  
17  import com.liferay.portal.model.Company;
18  import com.liferay.portal.monitoring.MonitoringException;
19  import com.liferay.portal.monitoring.statistics.DataSampleProcessor;
20  import com.liferay.portal.service.CompanyLocalService;
21  
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.TreeMap;
26  
27  /**
28   * <a href="ServerStatistics.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Michael C. Han
31   * @author Brian Wing Shun Chan
32   */
33  public class ServerStatistics
34      implements DataSampleProcessor<PortletRequestDataSample> {
35  
36      public void afterPropertiesSet() {
37          CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
38  
39          _companyStatisticsByCompanyId.put(
40              systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
41          _companyStatisticsByWebId.put(
42              systemCompanyStatistics.getWebId(), systemCompanyStatistics);
43      }
44  
45      public Set<Long> getCompanyIds() {
46          return _companyStatisticsByCompanyId.keySet();
47      }
48  
49      public CompanyStatistics getCompanyStatistics(long companyId)
50          throws MonitoringException {
51  
52          CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
53              companyId);
54  
55          if (companyStatistics == null) {
56              throw new MonitoringException(
57                  "No statistics found for company id " + companyId);
58          }
59  
60          return companyStatistics;
61      }
62  
63      public CompanyStatistics getCompanyStatistics(String webId)
64          throws MonitoringException {
65  
66          CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
67              webId);
68  
69          if (companyStatistics == null) {
70              throw new MonitoringException(
71                  "No statistics found for web id " + webId);
72          }
73  
74          return companyStatistics;
75      }
76  
77      public Set<CompanyStatistics> getCompanyStatisticsSet() {
78          return new HashSet<CompanyStatistics>(
79              _companyStatisticsByWebId.values());
80      }
81  
82      public Set<String> getPortletIds() {
83          Set<String> portletIds = new HashSet<String>();
84  
85          for (CompanyStatistics containerStatistics :
86                  _companyStatisticsByWebId.values()) {
87  
88              portletIds.addAll(containerStatistics.getPortletIds());
89          }
90  
91          return portletIds;
92      }
93  
94      public Set<String> getWebIds() {
95          return _companyStatisticsByWebId.keySet();
96      }
97  
98      public void processDataSample(
99              PortletRequestDataSample portletRequestDataSample)
100         throws MonitoringException {
101 
102         long companyId = portletRequestDataSample.getCompanyId();
103 
104         CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
105             companyId);
106 
107         if (companyStatistics == null) {
108             try {
109                 Company company = _companyLocalService.getCompany(companyId);
110 
111                 companyStatistics = register(company.getWebId());
112             }
113             catch (Exception e) {
114                 throw new IllegalStateException(
115                     "Unable to get company with company id " + companyId, e);
116             }
117         }
118 
119         companyStatistics.processDataSample(portletRequestDataSample);
120     }
121 
122     public synchronized CompanyStatistics register(String webId) {
123         CompanyStatistics companyStatistics = new CompanyStatistics(
124             _companyLocalService, webId);
125 
126         _companyStatisticsByCompanyId.put(
127             companyStatistics.getCompanyId(), companyStatistics);
128         _companyStatisticsByWebId.put(webId, companyStatistics);
129 
130         return companyStatistics;
131     }
132 
133     public void reset() {
134         for (long companyId : _companyStatisticsByCompanyId.keySet()) {
135             reset(companyId);
136         }
137     }
138 
139     public void reset(long companyId) {
140         CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
141             companyId);
142 
143         if (companyStatistics == null) {
144             return;
145         }
146 
147         companyStatistics.reset();
148     }
149 
150     public void reset(String webId) {
151         CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
152             webId);
153 
154         if (companyStatistics == null) {
155             return;
156         }
157 
158         companyStatistics.reset();
159     }
160 
161     public void setCompanyLocalService(
162         CompanyLocalService companyLocalService) {
163 
164         _companyLocalService = companyLocalService;
165     }
166 
167     public synchronized void unregister(String webId) {
168         CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
169             webId);
170 
171         if (companyStatistics != null) {
172             _companyStatisticsByCompanyId.remove(
173                 companyStatistics.getCompanyId());
174         }
175     }
176 
177     private CompanyLocalService _companyLocalService;
178     private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
179         new TreeMap<Long, CompanyStatistics>();
180     private Map<String, CompanyStatistics> _companyStatisticsByWebId =
181         new TreeMap<String, CompanyStatistics>();
182 
183 }