1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
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
42
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
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
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 }