View Javadoc

1   /*
2    * #%L
3    * Bitrepository Monitoring Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.monitoringservice;
23  
24  import java.util.Map;
25  import javax.jms.JMSException;
26  import access.AccessComponentFactory;
27  import access.getstatus.GetStatusClient;
28  import org.bitrepository.common.settings.Settings;
29  import org.bitrepository.common.utils.SettingsUtils;
30  import org.bitrepository.monitoringservice.alarm.BasicMonitoringServiceAlerter;
31  import org.bitrepository.monitoringservice.alarm.MonitorAlerter;
32  import org.bitrepository.monitoringservice.collector.StatusCollector;
33  import org.bitrepository.monitoringservice.status.ComponentStatus;
34  import org.bitrepository.monitoringservice.status.ComponentStatusStore;
35  import org.bitrepository.monitoringservice.status.StatusStore;
36  import org.bitrepository.protocol.messagebus.MessageBus;
37  import org.bitrepository.protocol.messagebus.MessageBusManager;
38  import org.bitrepository.protocol.security.SecurityManager;
39  import org.bitrepository.service.LifeCycledService;
40  import org.bitrepository.settings.referencesettings.AlarmLevel;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * The monitoring service.
46   */
47  public class MonitoringService implements LifeCycledService {
48      private final Logger log = LoggerFactory.getLogger(getClass());
49      /** The settings. */
50      private final Settings settings;
51      /** The store of collected statuses */
52      private final StatusStore statusStore;
53      /** The client for getting statuses. */
54      private final GetStatusClient getStatusClient;
55      /** The alerter for sending alarms */
56      private final MonitorAlerter alerter;
57      /** The status collector */
58      private final StatusCollector collector;
59      
60      /**
61       * Constructor.
62       * @param settings The settings.
63       * @param securityManager The security manager.
64       */
65      public MonitoringService(Settings settings, SecurityManager securityManager) {
66          this.settings = settings;
67          SettingsUtils.initialize(settings);
68          MessageBus messageBus = MessageBusManager.getMessageBus(settings, securityManager);
69          statusStore = new ComponentStatusStore(SettingsUtils.getStatusContributorsForCollection());
70          alerter = new BasicMonitoringServiceAlerter(settings, messageBus, AlarmLevel.ERROR, statusStore);
71          getStatusClient = AccessComponentFactory.getInstance().createGetStatusClient(settings, securityManager,
72                  settings.getReferenceSettings().getMonitoringServiceSettings().getID());
73          collector = new StatusCollector(getStatusClient, settings, statusStore, alerter);
74          collector.start();
75      }
76      
77      /**
78       * @return The map of the status for the components.
79       */
80      public Map<String, ComponentStatus> getStatus() {
81          return statusStore.getStatusMap();
82      }
83      
84      @Override
85      public void start() {}
86      
87      /**
88       * @return The maximum number of attempts to retrieve a status from a component before dispatching an alarm.
89       */
90      public int getMaxRetries() {
91          return settings.getReferenceSettings().getMonitoringServiceSettings().getMaxRetries().intValue();
92      } 
93      
94      /**
95       * @return The interval between collecting status from the components.
96       */
97      public long getCollectionInterval() {
98          return settings.getReferenceSettings().getMonitoringServiceSettings().getCollectionInterval();
99      }
100     
101     @Override
102     public void shutdown() {
103         collector.stop();
104         MessageBus messageBus = MessageBusManager.getMessageBus();
105         if ( messageBus != null) {
106             try {
107                 messageBus.close();
108             } catch (JMSException e) {
109                 log.warn("Failed to close message bus cleanly, " + e.getMessage());
110             }
111         }
112     }
113 }