View Javadoc

1   /*
2    *  Copyright 2010 Felix Roethenbacher
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package ch.syabru.nagios.broker.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.beans.factory.annotation.Required;
24  
25  import ch.syabru.nagios.broker.ExternalCommand;
26  import ch.syabru.nagios.broker.ExternalCommandHandler;
27  import ch.syabru.nagios.broker.ExternalCommandHandlerException;
28  import ch.syabru.nagios.broker.Matcher;
29  import ch.syabru.nagios.broker.Message;
30  import ch.syabru.nagios.broker.MessageHandler;
31  
32  /**
33   * Message handler implementation.
34   *
35   * @author Felix Roethenbacher
36   *
37   */
38  public class MessageHandlerImpl implements MessageHandler {
39      private final Logger logger =
40          LoggerFactory.getLogger(MessageHandlerImpl.class);
41  
42      private ExternalCommandHandler commandHandler;
43      private List<Matcher> matchers;
44  
45      @Override
46      public void messageReceived(Message message) {
47          logger.debug("Message received [{}]", message);
48          for (ExternalCommand command : matches(message)) {
49              try {
50                  commandHandler.submitExternalCommand(command);
51              } catch (ExternalCommandHandlerException e) {
52                  logger.error("Error submitting external command", e);
53              }
54          }
55      }
56  
57      /**
58       * Get list of matching external commands.
59       * @param message Message.
60       * @return List of matching external commands.
61       */
62      private List<ExternalCommand> matches(Message message) {
63          ArrayList<ExternalCommand> commands = new ArrayList<ExternalCommand>();
64          for (Matcher matcher : matchers) {
65              ExternalCommand matchingCommand = matcher.matches(message);
66              if (matchingCommand != null)
67                  commands.add(matchingCommand);
68          }
69          logger.debug("Matching actions found: {}", commands.size());
70          return commands;
71      }
72  
73      @Required
74      public void setMatchers(List<Matcher> matchers) {
75          this.matchers = matchers;
76      }
77  
78      @Required
79      public void setCommandHandler(ExternalCommandHandler commandHandler) {
80          this.commandHandler = commandHandler;
81      }
82  }