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.portal;
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<PortalRequestDataSample> {
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> getWebIds() {
83          return _companyStatisticsByWebId.keySet();
84      }
85  
86      public void processDataSample(
87          PortalRequestDataSample portalRequestDataSample) {
88  
89          long companyId = portalRequestDataSample.getCompanyId();
90  
91          CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
92              companyId);
93  
94          if (companyStatistics == null) {
95              try {
96                  Company company = _companyLocalService.getCompany(companyId);
97  
98                  companyStatistics = register(company.getWebId());
99              }
100             catch (Exception e) {
101                 throw new IllegalStateException(
102                     "Unable to get company with company id " + companyId);
103             }
104         }
105 
106         companyStatistics.processDataSample(portalRequestDataSample);
107     }
108 
109     public synchronized CompanyStatistics register(String webId) {
110         CompanyStatistics companyStatistics = new CompanyStatistics(
111             _companyLocalService, webId);
112 
113         _companyStatisticsByCompanyId.put(
114             companyStatistics.getCompanyId(), companyStatistics);
115         _companyStatisticsByWebId.put(webId, companyStatistics);
116 
117         return companyStatistics;
118     }
119 
120     public void reset() {
121         for (long companyId : _companyStatisticsByCompanyId.keySet()) {
122             reset(companyId);
123         }
124     }
125 
126     public void reset(long companyId) {
127         CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
128             companyId);
129 
130         if (companyStatistics == null) {
131             return;
132         }
133 
134         companyStatistics.reset();
135     }
136 
137     public void reset(String webId) {
138         CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
139             webId);
140 
141         if (companyStatistics == null) {
142             return;
143         }
144 
145         companyStatistics.reset();
146     }
147 
148     public void setCompanyLocalService(
149         CompanyLocalService companyLocalService) {
150 
151         _companyLocalService = companyLocalService;
152     }
153 
154     public synchronized void unregister(String webId) {
155         CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
156             webId);
157 
158         if (companyStatistics != null) {
159             _companyStatisticsByCompanyId.remove(
160                 companyStatistics.getCompanyId());
161         }
162     }
163 
164     private CompanyLocalService _companyLocalService;
165     private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
166         new TreeMap<Long, CompanyStatistics>();
167     private Map<String, CompanyStatistics> _companyStatisticsByWebId =
168         new TreeMap<String, CompanyStatistics>();
169 
170 }