KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > server > EasyBeans


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.server;
27
28 import java.net.URL JavaDoc;
29
30 import org.objectweb.easybeans.log.JLog;
31 import org.objectweb.easybeans.log.JLogFactory;
32
33 /**
34  * Starts an embedded server in standalone mode.
35  * @author Florent Benoit
36  */

37 public final class EasyBeans {
38
39     /**
40      * Default XML file.
41      */

42     public static final String JavaDoc DEFAULT_XML_FILE = "org/objectweb/easybeans/server/easybeans-default.xml";
43
44     /**
45      * User XML file.
46      */

47     public static final String JavaDoc USER_XML_FILE = "easybeans.xml";
48
49     /**
50      * Logger.
51      */

52     private static JLog logger = JLogFactory.getLog(EasyBeans.class);
53
54     /**
55      * Utility class. No public constructor.
56      */

57     private EasyBeans() {
58
59     }
60
61     /**
62      * Main method called by default.
63      * @param args the arguments for the main method
64      * @throws Exception if failures
65      */

66     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
67
68         Embedded embedded = null;
69
70         // user configuration ?
71
URL JavaDoc xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(USER_XML_FILE);
72
73         if (xmlConfigurationURL == null) {
74             logger.warn("No user-defined configuration file named {0} found in classpath, use default settings", USER_XML_FILE);
75             xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XML_FILE);
76         }
77         try {
78             embedded = EmbeddedConfigurator.create(xmlConfigurationURL);
79         } catch (EmbeddedException e) {
80             throw new Exception JavaDoc("Cannot create the embedded server", e);
81         }
82
83         // Add hook for shutdown
84
Runtime.getRuntime().addShutdownHook(new ShutdownHook(embedded));
85
86         // Start EasyBeans
87
embedded.start();
88
89     }
90
91     /**
92      * Hook that is called when process is going to shutdown.
93      * @author Florent Benoit
94      */

95     static class ShutdownHook extends Thread JavaDoc {
96
97         /**
98          * Reference to the embedded object.
99          */

100         private Embedded embedded = null;
101
102         /**
103          * Build a new shutdown hook with the given embedded instance.
104          * @param embedded the instance to stop
105          */

106         public ShutdownHook(final Embedded embedded) {
107             this.embedded = embedded;
108         }
109
110         /**
111          * Stop the embedded server.
112          */

113         @Override JavaDoc
114         public void run() {
115             // stop embedded
116
try {
117                 embedded.stop();
118             } catch (EmbeddedException e) {
119                 System.err.println("Error while stopping embedded server");
120                 e.printStackTrace(System.err);
121             }
122         }
123     }
124 }
125
Popular Tags