1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.serviceregistry.impl.jmx;
23
24 import org.opencastproject.serviceregistry.api.HostRegistration;
25 import org.opencastproject.serviceregistry.api.ServiceStatistics;
26 import org.opencastproject.util.jmx.JmxUtil;
27
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.apache.commons.collections4.PredicateUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 import javax.management.MBeanNotificationInfo;
40 import javax.management.Notification;
41 import javax.management.NotificationBroadcasterSupport;
42
43 public class HostsStatistics extends NotificationBroadcasterSupport implements HostsStatisticsMXBean {
44
45 private static final Logger logger = LoggerFactory.getLogger(HostsStatistics.class);
46
47 private static final int ONLINE = 0;
48 private static final int MAINTENANCE = 1;
49 private static final int OFFLINE = 2;
50 private long sequenceNumber = 1;
51
52 private Map<String, Integer> hosts = new HashMap<String, Integer>();
53
54 public HostsStatistics(List<ServiceStatistics> statistics) {
55 for (ServiceStatistics stats : statistics) {
56 String host = stats.getServiceRegistration().getHost();
57 boolean online = stats.getServiceRegistration().isOnline();
58 boolean inMaintenanceMode = stats.getServiceRegistration().isInMaintenanceMode();
59 if (!stats.getServiceRegistration().isActive()) {
60 hosts.remove(host);
61 logger.trace("Removing inactive host '{}'", host);
62 continue;
63 }
64 if (online && !inMaintenanceMode) {
65 hosts.put(host, ONLINE);
66 } else if (online && inMaintenanceMode) {
67 hosts.put(host, MAINTENANCE);
68 } else {
69 hosts.put(host, OFFLINE);
70 }
71 }
72 }
73
74 public void updateHost(HostRegistration host) {
75 if (!host.isActive()) {
76 hosts.remove(host.toString());
77 logger.trace("Removing inactive host '{}'", host);
78 return;
79 }
80
81 if (host.isOnline() && !host.isMaintenanceMode()) {
82 hosts.put(host.toString(), ONLINE);
83 } else if (host.isOnline() && host.isMaintenanceMode()) {
84 hosts.put(host.toString(), MAINTENANCE);
85 } else {
86 hosts.put(host.toString(), OFFLINE);
87 }
88
89 sendNotification(JmxUtil.createUpdateNotification(this, sequenceNumber++, "Host updated"));
90 }
91
92 @Override
93 public MBeanNotificationInfo[] getNotificationInfo() {
94 String[] types = new String[] { JmxUtil.OPENCAST_UPDATE_NOTIFICATION };
95
96 String name = Notification.class.getName();
97 String description = "An update was executed";
98 MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description);
99 return new MBeanNotificationInfo[] { info };
100 }
101
102
103
104
105 @Override
106 public int getTotalCount() {
107 return hosts.size();
108 }
109
110
111
112
113 @Override
114 public int getOnlineCount() {
115 return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(ONLINE));
116 }
117
118
119
120
121 @Override
122 public int getOfflineCount() {
123 return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(OFFLINE));
124 }
125
126
127
128
129 @Override
130 public int getInMaintenanceCount() {
131 return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(MAINTENANCE));
132 }
133
134
135
136
137 @Override
138 public String[] getAll() {
139 return hosts.keySet().toArray(new String[hosts.size()]);
140 }
141
142
143
144
145 @Override
146 public String[] getOnline() {
147 List<String> onlineHosts = new ArrayList<String>();
148 for (Entry<String, Integer> entry : hosts.entrySet()) {
149 if (entry.getValue().equals(ONLINE)) {
150 onlineHosts.add(entry.getKey());
151 }
152 }
153 return onlineHosts.toArray(new String[onlineHosts.size()]);
154 }
155
156
157
158
159 @Override
160 public String[] getOffline() {
161 List<String> offlineHosts = new ArrayList<String>();
162 for (Entry<String, Integer> entry : hosts.entrySet()) {
163 if (entry.getValue().equals(OFFLINE)) {
164 offlineHosts.add(entry.getKey());
165 }
166 }
167 return offlineHosts.toArray(new String[offlineHosts.size()]);
168 }
169
170
171
172
173 @Override
174 public String[] getInMaintenance() {
175 List<String> maintenanceHosts = new ArrayList<String>();
176 for (Entry<String, Integer> entry : hosts.entrySet()) {
177 if (entry.getValue().equals(MAINTENANCE)) {
178 maintenanceHosts.add(entry.getKey());
179 }
180 }
181 return maintenanceHosts.toArray(new String[maintenanceHosts.size()]);
182 }
183
184 }