001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 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: EmbeddedConfigurator.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.server;
025:
026: import java.net.URL;
027:
028: import org.ow2.easybeans.xmlconfig.XMLConfiguration;
029: import org.ow2.easybeans.xmlconfig.XMLConfigurationException;
030: import org.ow2.util.log.Log;
031: import org.ow2.util.log.LogFactory;
032:
033: /**
034: * Allows to configure an embedded server with an XML configuration file.
035: * @author Florent Benoit
036: */
037: public final class EmbeddedConfigurator {
038:
039: /**
040: * Name of the Default XML configuration file.
041: */
042: private static final String CONFIGURATION_FILE_NAME = "easybeans.xml";
043:
044: /**
045: * Logger.
046: */
047: private static Log logger = LogFactory
048: .getLog(EmbeddedConfigurator.class);
049:
050: /**
051: * Utility class, no public constructor.
052: */
053: private EmbeddedConfigurator() {
054:
055: }
056:
057: /**
058: * Configure the given embedded server with the given XML configuration file
059: * URL.
060: * @param embedded the embedded server to configure.
061: * @param xmlConfigurationURL the URL to the xml configuration file.
062: * @return the configured embedded instance.
063: * @throws EmbeddedException if the embedded configuration fails.
064: */
065: public static Embedded init(final Embedded embedded,
066: final URL xmlConfigurationURL) throws EmbeddedException {
067: configure(embedded, xmlConfigurationURL);
068: return embedded;
069: }
070:
071: /**
072: * Create and configure an embedded server with the given XML configuration
073: * file URL.
074: * @param xmlConfigurationURL the URL to the xml configuration file.
075: * @return the configured embedded instance.
076: * @throws EmbeddedException if the embedded configuration fails.
077: */
078: public static Embedded create(final URL xmlConfigurationURL)
079: throws EmbeddedException {
080: return init(new Embedded(), xmlConfigurationURL);
081: }
082:
083: /**
084: * Create and configure an embedded server with the XML configuration file
085: * URL found in classpath.
086: * @return the configured embedded instance.
087: * @throws EmbeddedException if the embedded configuration fails.
088: */
089: public static Embedded create() throws EmbeddedException {
090: URL xmlConfigurationURL = Thread.currentThread()
091: .getContextClassLoader().getResource(
092: CONFIGURATION_FILE_NAME);
093: if (xmlConfigurationURL == null) {
094: throw new EmbeddedException(
095: "No configuration file with name '"
096: + CONFIGURATION_FILE_NAME
097: + "' was found in classpath.");
098: }
099: return create(xmlConfigurationURL);
100: }
101:
102: /**
103: * Configure the given embedded server with the xml configuration file.
104: * @param embedded the embedded server to configure.
105: * @param xmlConfigurationURL the URL to the xml configuration file.
106: * @throws EmbeddedException if the embedded configuration fails.
107: */
108: private static void configure(final Embedded embedded,
109: final URL xmlConfigurationURL) throws EmbeddedException {
110: long tStart = System.currentTimeMillis();
111: logger.debug("Starting configuration of EasyBeans server");
112: XMLConfiguration xmlConfiguration = new XMLConfiguration(
113: xmlConfigurationURL);
114: try {
115: xmlConfiguration.configure(embedded);
116: } catch (XMLConfigurationException e) {
117: throw new EmbeddedException(
118: "Cannot configure the embedded server", e);
119: }
120: if (logger.isInfoEnabled()) {
121: logger.debug("Configuration done in : "
122: + (System.currentTimeMillis() - tStart) + " ms");
123: }
124: }
125:
126: }
|