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