1
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
42 public class ServerStatistics
43 implements DataSampleProcessor<PortletRequestDataSample> {
44
45 public ServerStatistics(CompanyLocalService companyLocalService) {
46 _companyLocalService = companyLocalService;
47
48 CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
49
50 _companyStatisticsByCompanyId.put(
51 systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
52 _companyStatisticsByWebId.put(
53 systemCompanyStatistics.getWebId(), systemCompanyStatistics);
54 }
55
56 public Set<Long> getCompanyIds() {
57 return _companyStatisticsByCompanyId.keySet();
58 }
59
60 public CompanyStatistics getCompanyStatistics(long companyId)
61 throws MonitoringException {
62
63 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
64 companyId);
65
66 if (companyStatistics == null) {
67 throw new MonitoringException(
68 "No statistics found for company id " + companyId);
69 }
70
71 return companyStatistics;
72 }
73
74 public CompanyStatistics getCompanyStatistics(String webId)
75 throws MonitoringException {
76
77 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
78 webId);
79
80 if (companyStatistics == null) {
81 throw new MonitoringException(
82 "No statistics found for web id " + webId);
83 }
84
85 return companyStatistics;
86 }
87
88 public Set<CompanyStatistics> getCompanyStatisticsSet() {
89 return new HashSet<CompanyStatistics>(
90 _companyStatisticsByWebId.values());
91 }
92
93 public Set<String> getPortletIds() {
94 Set<String> portletIds = new HashSet<String>();
95
96 for (CompanyStatistics containerStatistics :
97 _companyStatisticsByWebId.values()) {
98
99 portletIds.addAll(containerStatistics.getPortletIds());
100 }
101
102 return portletIds;
103 }
104
105 public Set<String> getWebIds() {
106 return _companyStatisticsByWebId.keySet();
107 }
108
109 public void processDataSample(
110 PortletRequestDataSample portletRequestDataSample)
111 throws MonitoringException {
112
113 long companyId = portletRequestDataSample.getCompanyId();
114
115 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
116 companyId);
117
118 if (companyStatistics == null) {
119 try {
120 Company company = _companyLocalService.getCompany(companyId);
121
122 companyStatistics = register(company.getWebId());
123 }
124 catch (Exception e) {
125 throw new IllegalStateException(
126 "Unable to get company with company id " + companyId, e);
127 }
128 }
129
130 companyStatistics.processDataSample(portletRequestDataSample);
131 }
132
133 public synchronized CompanyStatistics register(String webId) {
134 CompanyStatistics companyStatistics = new CompanyStatistics(
135 _companyLocalService, webId);
136
137 _companyStatisticsByCompanyId.put(
138 companyStatistics.getCompanyId(), companyStatistics);
139 _companyStatisticsByWebId.put(webId, companyStatistics);
140
141 return companyStatistics;
142 }
143
144 public void reset() {
145 for (long companyId : _companyStatisticsByCompanyId.keySet()) {
146 reset(companyId);
147 }
148 }
149
150 public void reset(long companyId) {
151 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
152 companyId);
153
154 if (companyStatistics == null) {
155 return;
156 }
157
158 companyStatistics.reset();
159 }
160
161 public void reset(String webId) {
162 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
163 webId);
164
165 if (companyStatistics == null) {
166 return;
167 }
168
169 companyStatistics.reset();
170 }
171
172 public synchronized void unregister(String webId) {
173 CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
174 webId);
175
176 if (companyStatistics != null) {
177 _companyStatisticsByCompanyId.remove(
178 companyStatistics.getCompanyId());
179 }
180 }
181
182 private CompanyLocalService _companyLocalService;
183 private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
184 new TreeMap<Long, CompanyStatistics>();
185 private Map<String, CompanyStatistics> _companyStatisticsByWebId =
186 new TreeMap<String, CompanyStatistics>();
187
188 }