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.io.PrintStream;
19  import java.util.List;
20  
21  import javax.annotation.PreDestroy;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.beans.factory.annotation.Required;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.ApplicationContextAware;
28  import org.springframework.context.support.AbstractApplicationContext;
29  
30  import ch.syabru.nagios.broker.MessageListener;
31  import ch.syabru.nagios.broker.MessageListenerException;
32  import ch.syabru.nagios.broker.NagiosMessageBroker;
33  
34  /**
35   * Nagios message broker main class.
36   *
37   * @author Felix Roethenbacher
38   *
39   */
40  public class NagiosMessageBrokerImpl
41  implements Runnable, NagiosMessageBroker, ApplicationContextAware
42  {
43      private final Logger logger =
44          LoggerFactory.getLogger(NagiosMessageBrokerImpl.class);
45  
46      private List<MessageListener> messageListeners;
47      private AbstractApplicationContext applicationContext;
48  
49      private boolean isDaemon = false;
50      private boolean stopping = false;
51  
52      @Override
53      public void run() {
54          if (!isDaemon)
55              outputApplicationInfo();
56          startMessageListeners();
57          while (!stopping) {
58              try {
59                  Thread.sleep(1000);
60              } catch (InterruptedException e) {
61                  logger.error("Thread interrupted", e);
62              }
63          }
64          logger.info("Shutting down ...");
65          applicationContext.destroy();
66          logger.info("Shutdown completed.");
67      }
68  
69      /**
70       * Start message listeners.
71       */
72      private void startMessageListeners() {
73          for (MessageListener listener : messageListeners) {
74              listener.registerListener();
75          }
76      }
77  
78      /**
79       * Stop message listeners.
80       */
81      private void stopMessageListeners() {
82          for (MessageListener listener : messageListeners) {
83              try {
84                  listener.unregisterListener();
85              } catch (MessageListenerException e) {
86                  logger.error("Error unregistering message listener", e);
87              }
88          }
89      }
90  
91      /**
92       * Shut-down method.
93       */
94      @PreDestroy
95      public void destroy() {
96          stopMessageListeners();
97      }
98  
99      @Override
100     public void start() {
101         Thread t = new Thread(this);
102         t.start();
103     }
104 
105     @Override
106     public void stop() {
107         stopping = true;
108     }
109 
110     /**
111      * Output application info.
112      */
113     private void outputApplicationInfo() {
114         PrintStream out = System.out;
115         out.println("Syabru Nagios Message Broker");
116         out.println();
117         out.println("Hit 'Enter' to terminate application.");
118     }
119 
120     @Required
121     public void setMessageListeners(List<MessageListener> messageListeners) {
122         this.messageListeners = messageListeners;
123     }
124 
125     public void setIsDaemon(boolean isDaemon) {
126         this.isDaemon = isDaemon;
127     }
128 
129     @Override
130     public void setApplicationContext(ApplicationContext applicationContext)
131     {
132         this.applicationContext =
133             (AbstractApplicationContext) applicationContext;
134     }
135 }