View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getTotalCount()
104    */
105   @Override
106   public int getTotalCount() {
107     return hosts.size();
108   }
109 
110   /**
111    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getOnlineCount()
112    */
113   @Override
114   public int getOnlineCount() {
115     return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(ONLINE));
116   }
117 
118   /**
119    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getOfflineCount()
120    */
121   @Override
122   public int getOfflineCount() {
123     return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(OFFLINE));
124   }
125 
126   /**
127    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getInMaintenanceCount()
128    */
129   @Override
130   public int getInMaintenanceCount() {
131     return CollectionUtils.countMatches(hosts.values(), PredicateUtils.equalPredicate(MAINTENANCE));
132   }
133 
134   /**
135    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getAll()
136    */
137   @Override
138   public String[] getAll() {
139     return hosts.keySet().toArray(new String[hosts.size()]);
140   }
141 
142   /**
143    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getOnline()
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     return onlineHosts.toArray(new String[onlineHosts.size()]);
153   }
154 
155   /**
156    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getOffline()
157    */
158   @Override
159   public String[] getOffline() {
160     List<String> offlineHosts = new ArrayList<String>();
161     for (Entry<String, Integer> entry : hosts.entrySet()) {
162       if (entry.getValue().equals(OFFLINE))
163         offlineHosts.add(entry.getKey());
164     }
165     return offlineHosts.toArray(new String[offlineHosts.size()]);
166   }
167 
168   /**
169    * @see org.opencastproject.serviceregistry.impl.jmx.HostsStatisticsMXBean#getInMaintenance()
170    */
171   @Override
172   public String[] getInMaintenance() {
173     List<String> maintenanceHosts = new ArrayList<String>();
174     for (Entry<String, Integer> entry : hosts.entrySet()) {
175       if (entry.getValue().equals(MAINTENANCE))
176         maintenanceHosts.add(entry.getKey());
177     }
178     return maintenanceHosts.toArray(new String[maintenanceHosts.size()]);
179   }
180 
181 }