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;
17  
18  import org.apache.commons.daemon.Daemon;
19  import org.apache.commons.daemon.DaemonContext;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.beans.BeansException;
23  import org.springframework.context.support.AbstractApplicationContext;
24  import org.springframework.context.support.ClassPathXmlApplicationContext;
25  
26  import ch.syabru.nagios.broker.impl.NagiosMessageBrokerImpl;
27  
28  /**
29   * Nagios message broker bootstrap implementation.
30   *
31   * @author Felix Roethenbacher
32   *
33   */
34  public class Bootstrap implements Daemon {
35      private static final Logger logger =
36          LoggerFactory.getLogger(Bootstrap.class);
37  
38      private NagiosMessageBrokerImpl msgBroker;
39  
40      /**
41       * Main method.
42       * @param args Command line arguments.
43       */
44      public static void main(String[] args) {
45          final Bootstrap bootstrap = new Bootstrap();
46          Thread terminator = new Thread() {
47              public void run() {
48                  try {
49                      // Return will terminate application.
50                      if (System.in.read() != 10) {
51                          Thread.sleep(100);
52                      } else {
53                          bootstrap.stop();
54                      }
55                  } catch (Exception e) {
56                      logger.error("Error in termination loop", e);
57                  }
58              };
59          };
60          terminator.start();
61          try {
62              bootstrap.createApplicationContext();
63              bootstrap.start();
64          } catch (Exception e) {
65              e.printStackTrace();
66              System.exit(-1);
67          }
68      }
69  
70      @Override
71      public void start() throws Exception {
72          msgBroker.start();
73      }
74  
75      @Override
76      public void stop() throws Exception {
77          msgBroker.stop();
78      }
79  
80      @Override
81      public void destroy() {
82      }
83  
84      @Override
85      public void init(DaemonContext ctx) throws Exception {
86          createApplicationContext();
87      }
88  
89      /**
90       * Create Spring application context.
91       */
92      private void createApplicationContext() {
93          try {
94              AbstractApplicationContext context =
95                  new ClassPathXmlApplicationContext(new String[] {
96                          "META-INF/spring/applicationContext*.xml"});
97              context.registerShutdownHook();
98              msgBroker = (NagiosMessageBrokerImpl) context.getBean(
99                      "nagiosMessageBroker");
100             msgBroker.setIsDaemon(true);
101         } catch (BeansException e) {
102             logger.error("Error starting application", e);
103             System.exit(1);
104         }
105     }
106 }