too many changes to list
[opensuse:webpin2.git] / api / webpin.api / src / main / java / org / opensuse / webpin / api / Server.java
1 /*
2  * Created on Aug 1, 2010
3  */
4 package org.opensuse.webpin.api;
5
6 import java.util.concurrent.Semaphore;
7
8 import org.apache.commons.daemon.Daemon;
9 import org.apache.commons.daemon.DaemonContext;
10 import org.restlet.Component;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.BeansException;
13 import org.springframework.context.ApplicationContext;
14 import org.springframework.context.ConfigurableApplicationContext;
15 import org.springframework.context.support.ClassPathXmlApplicationContext;
16
17 public final class Server implements Daemon {
18
19         public static void main(String[] args) throws Exception {
20                 final Server server = new Server();
21                 server.init(null);
22                 server.start();
23                 try {
24                         new Semaphore(0).acquire();
25                 } finally {
26                         server.stop();
27                         server.destroy();
28                 }
29         }
30
31         private ConfigurableApplicationContext applicationContext = null;
32         private DaemonContext daemon = null;
33         
34         @Override
35         public void init(final DaemonContext d) throws Exception {
36             LoggerFactory.getLogger("query");
37             
38                 this.daemon = d;
39                 try {
40                         applicationContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
41                 } catch (final BeansException be) {
42                         final String message = String.format("failed to initialize the %s: %s", ApplicationContext.class.getSimpleName(), be.getMessage());
43                         if (d != null) {
44                                 d.getController().fail(message, be);
45                                 return;
46                         } else {
47                                 throw be;
48                         }
49                 }
50         }
51
52         @Override
53         public void start() throws Exception {
54                 if (applicationContext == null) {
55                         final String message = String.format("failed to start as the %s is not initialized", ApplicationContext.class.getSimpleName());
56                         if (daemon != null) {
57                                 daemon.getController().fail(message);
58                                 return;
59                         } else {
60                                 throw new IllegalStateException(message);
61                         }
62                 }
63                 applicationContext.getBean("top", Component.class).start();             
64         }
65
66         @Override
67         public void stop() throws Exception {
68                 if (applicationContext == null) {
69                         final String message = String.format("failed to stop as the %s is not initialized", ApplicationContext.class.getSimpleName());
70                         if (daemon != null) {
71                                 daemon.getController().fail(message);
72                                 return;
73                         } else {
74                                 throw new IllegalStateException(message);
75                         }
76                 }
77                 applicationContext.getBean("top", Component.class).stop();              
78         }
79
80         @Override
81         public void destroy() {
82                 if (applicationContext == null) {
83                         final String message = String.format("failed to destroy as the %s is not initialized", ApplicationContext.class.getSimpleName());
84                         if (daemon != null) {
85                                 daemon.getController().fail(message);
86                                 return;
87                         } else {
88                                 throw new IllegalStateException(message);
89                         }
90                 }
91                 applicationContext.close();
92                 applicationContext = null;
93         }
94         
95 }