001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EasyBeans.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.server;
025:
026: import java.io.File;
027: import java.net.URL;
028:
029: import org.ow2.util.log.Log;
030: import org.ow2.util.log.LogFactory;
031:
032: /**
033: * Starts an embedded server in standalone mode.
034: * @author Florent Benoit
035: */
036: public final class EasyBeans {
037:
038: /**
039: * Default XML file.
040: */
041: public static final String DEFAULT_XML_FILE = "org/ow2/easybeans/server/easybeans-default.xml";
042:
043: /**
044: * User XML file.
045: */
046: public static final String USER_XML_FILE = "easybeans.xml";
047:
048: /**
049: * Logger.
050: */
051: private static Log logger = LogFactory.getLog(EasyBeans.class);
052:
053: /**
054: * Utility class. No public constructor.
055: */
056: private EasyBeans() {
057:
058: }
059:
060: /**
061: * Main method called by default.
062: * @param args the arguments for the main method
063: * @throws Exception if failures
064: */
065: public static void main(final String[] args) throws Exception {
066:
067: Embedded embedded = null;
068:
069: // user configuration ?
070: URL xmlConfigurationURL = Thread.currentThread()
071: .getContextClassLoader().getResource(USER_XML_FILE);
072:
073: if (xmlConfigurationURL == null) {
074: logger
075: .debug(
076: "No user-defined configuration file named {0} found in classpath, use default settings",
077: USER_XML_FILE);
078: xmlConfigurationURL = Thread.currentThread()
079: .getContextClassLoader().getResource(
080: DEFAULT_XML_FILE);
081: } else {
082: logger
083: .info(
084: "Using user-defined configuration file ''{0}'' found in classpath",
085: xmlConfigurationURL);
086: }
087: try {
088: embedded = EmbeddedConfigurator.create(xmlConfigurationURL);
089: } catch (EmbeddedException e) {
090: throw new Exception("Cannot create the embedded server", e);
091: }
092:
093: // Add hook for shutdown
094: Runtime.getRuntime()
095: .addShutdownHook(new ShutdownHook(embedded));
096:
097: // Add deploy directory
098: embedded.getServerConfig().addDeployDirectory(
099: new File(Embedded.DEFAULT_DEPLOY_DIRECTORY));
100:
101: // Start EasyBeans
102: embedded.start();
103:
104: }
105:
106: /**
107: * Hook that is called when process is going to shutdown.
108: * @author Florent Benoit
109: */
110: static class ShutdownHook extends Thread {
111:
112: /**
113: * Reference to the embedded object.
114: */
115: private Embedded embedded = null;
116:
117: /**
118: * Build a new shutdown hook with the given embedded instance.
119: * @param embedded the instance to stop
120: */
121: public ShutdownHook(final Embedded embedded) {
122: this .embedded = embedded;
123: }
124:
125: /**
126: * Stop the embedded server.
127: */
128: @Override
129: public void run() {
130: // stop embedded
131: try {
132: embedded.stop();
133: } catch (EmbeddedException e) {
134: System.err
135: .println("Error while stopping embedded server");
136: e.printStackTrace(System.err);
137: }
138: }
139: }
140: }
|