View Javadoc

1   /*
2    * #%L
3    * Bitrepository Integration
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.service;
26  
27  import java.util.UUID;
28  import org.bitrepository.bitrepositoryelements.Alarm;
29  import org.bitrepository.bitrepositorymessages.AlarmMessage;
30  import org.bitrepository.common.ArgumentValidator;
31  import org.bitrepository.common.settings.Settings;
32  import org.bitrepository.common.utils.CalendarUtils;
33  import org.bitrepository.protocol.messagebus.MessageSender;
34  import org.bitrepository.service.contributor.MessageDispatcher;
35  import org.bitrepository.settings.referencesettings.AlarmLevel;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * The class for dispatching alarms.
41   */
42  public class AlarmDispatcher extends MessageDispatcher {
43      private Logger log = LoggerFactory.getLogger(getClass());
44      private final AlarmLevel alarmLevel;
45      
46      /**
47       * @param sender Used for sending the alarms.
48       * @param settings The configuration.
49       */
50      public AlarmDispatcher(Settings settings, MessageSender sender, AlarmLevel alarmLevel) {
51          super(settings, sender);
52          if (alarmLevel == null) {
53              this.alarmLevel = AlarmLevel.ERROR;
54          }  else {
55              this.alarmLevel = alarmLevel;
56          }
57      }
58  
59      /**
60       * Delegates to #AlarmDispatcher(MessageSender, AlarmLevel) with a ERROR alarm level.
61       */
62      public AlarmDispatcher(Settings settings, MessageSender sender) {
63          this(settings, sender, AlarmLevel.ERROR);
64      }
65      
66      /**
67       * Send an alarm at warning-level. 
68       * If the settings does not have alarm level at 'warning', then a log is made instead.
69       * @param alarm The alarm to send.
70       */
71      public void warning(Alarm alarm) {
72          ArgumentValidator.checkNotNull(alarm, "alarm");
73          if(alarmLevel != AlarmLevel.WARNING) {
74              log.debug("Will send a '" + AlarmLevel.WARNING + "' alarm, when the alarm level is '"
75                      + alarmLevel + "'{}", alarm);
76          } else {
77              sendAlarm(alarm);
78          }
79      }
80      
81      /**
82       * Send an alarm at error-level. 
83       * If the settings does not have alarm level at 'error', then a log is made instead.
84       * @param alarm The alarm to send.
85       */
86      public void error(Alarm alarm) {
87          ArgumentValidator.checkNotNull(alarm, "alarm");
88          if(alarmLevel == AlarmLevel.EMERGENCY) {
89              log.debug("Cannot send a '" + AlarmLevel.ERROR + "' alarm, when the alarm level is '"
90                      + alarmLevel + "'{}", alarm);
91          } else {
92              sendAlarm(alarm);
93          }
94      }
95          
96      /**
97       * Send an alarm at emergency-level. At this level all alarms will be sent.
98       * @param alarm The alarm to send.
99       */
100     public void emergency(Alarm alarm) {
101         ArgumentValidator.checkNotNull(alarm, "alarm");
102         sendAlarm(alarm);
103     }
104         
105    /**
106      * Method for sending an Alarm when something bad happens.
107      * @param alarm The alarm to send to the destination for the alarm service.
108      */
109     protected void sendAlarm(Alarm alarm) {
110         AlarmMessage message = new AlarmMessage();
111         alarm.setAlarmRaiser(settings.getComponentID());
112         alarm.setOrigDateTime(CalendarUtils.getNow());
113 
114         message.setAlarm(alarm);
115         message.setCorrelationID(UUID.randomUUID().toString());
116         message.setReplyTo(settings.getContributorDestinationID());
117         message.setDestination(settings.getAlarmDestination());
118         message.setCollectionID(alarm.getCollectionID());
119         
120         log.warn("Sending alarm: " + alarm);
121         dispatchMessage(message);
122     }
123 }