001
014
015 package com.liferay.portal.monitoring.statistics.portal;
016
017 import com.liferay.portal.kernel.monitoring.MonitoringException;
018 import com.liferay.portal.kernel.monitoring.statistics.DataSampleProcessor;
019 import com.liferay.portal.model.Company;
020 import com.liferay.portal.service.CompanyLocalService;
021
022 import java.util.HashSet;
023 import java.util.Map;
024 import java.util.Set;
025 import java.util.TreeMap;
026
027
031 public class ServerStatistics
032 implements DataSampleProcessor<PortalRequestDataSample> {
033
034 public void afterPropertiesSet() {
035 CompanyStatistics systemCompanyStatistics = new CompanyStatistics();
036
037 _companyStatisticsByCompanyId.put(
038 systemCompanyStatistics.getCompanyId(), systemCompanyStatistics);
039 _companyStatisticsByWebId.put(
040 systemCompanyStatistics.getWebId(), systemCompanyStatistics);
041 }
042
043 public Set<Long> getCompanyIds() {
044 return _companyStatisticsByCompanyId.keySet();
045 }
046
047 public CompanyStatistics getCompanyStatistics(long companyId)
048 throws MonitoringException {
049
050 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
051 companyId);
052
053 if (companyStatistics == null) {
054 throw new MonitoringException(
055 "No statistics found for company id " + companyId);
056 }
057
058 return companyStatistics;
059 }
060
061 public CompanyStatistics getCompanyStatistics(String webId)
062 throws MonitoringException {
063
064 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
065 webId);
066
067 if (companyStatistics == null) {
068 throw new MonitoringException(
069 "No statistics found for web id " + webId);
070 }
071
072 return companyStatistics;
073 }
074
075 public Set<CompanyStatistics> getCompanyStatisticsSet() {
076 return new HashSet<CompanyStatistics>(
077 _companyStatisticsByWebId.values());
078 }
079
080 public Set<String> getWebIds() {
081 return _companyStatisticsByWebId.keySet();
082 }
083
084 @Override
085 public void processDataSample(
086 PortalRequestDataSample portalRequestDataSample) {
087
088 long companyId = portalRequestDataSample.getCompanyId();
089
090 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
091 companyId);
092
093 if (companyStatistics == null) {
094 try {
095 Company company = _companyLocalService.getCompany(companyId);
096
097 companyStatistics = register(company.getWebId());
098 }
099 catch (Exception e) {
100 throw new IllegalStateException(
101 "Unable to get company with company id " + companyId);
102 }
103 }
104
105 companyStatistics.processDataSample(portalRequestDataSample);
106 }
107
108 public synchronized CompanyStatistics register(String webId) {
109 CompanyStatistics companyStatistics = new CompanyStatistics(
110 _companyLocalService, webId);
111
112 _companyStatisticsByCompanyId.put(
113 companyStatistics.getCompanyId(), companyStatistics);
114 _companyStatisticsByWebId.put(webId, companyStatistics);
115
116 return companyStatistics;
117 }
118
119 public void reset() {
120 for (long companyId : _companyStatisticsByCompanyId.keySet()) {
121 reset(companyId);
122 }
123 }
124
125 public void reset(long companyId) {
126 CompanyStatistics companyStatistics = _companyStatisticsByCompanyId.get(
127 companyId);
128
129 if (companyStatistics == null) {
130 return;
131 }
132
133 companyStatistics.reset();
134 }
135
136 public void reset(String webId) {
137 CompanyStatistics companyStatistics = _companyStatisticsByWebId.get(
138 webId);
139
140 if (companyStatistics == null) {
141 return;
142 }
143
144 companyStatistics.reset();
145 }
146
147 public void setCompanyLocalService(
148 CompanyLocalService companyLocalService) {
149
150 _companyLocalService = companyLocalService;
151 }
152
153 public synchronized void unregister(String webId) {
154 CompanyStatistics companyStatistics = _companyStatisticsByWebId.remove(
155 webId);
156
157 if (companyStatistics != null) {
158 _companyStatisticsByCompanyId.remove(
159 companyStatistics.getCompanyId());
160 }
161 }
162
163 private CompanyLocalService _companyLocalService;
164 private Map<Long, CompanyStatistics> _companyStatisticsByCompanyId =
165 new TreeMap<Long, CompanyStatistics>();
166 private Map<String, CompanyStatistics> _companyStatisticsByWebId =
167 new TreeMap<String, CompanyStatistics>();
168
169 }