KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.easybeans.xmlconfig.XMLConfiguration;
33 import org.objectweb.easybeans.xmlconfig.XMLConfigurationException;
34
35
36 /**
37  * Allows to configure an embedded server with an XML configuration file.
38  * @author Florent Benoit
39  */

40 public final class EmbeddedConfigurator {
41
42     /**
43      * Name of the Default XML configuration file.
44      */

45     private static final String JavaDoc CONFIGURATION_FILE_NAME = "easybeans.xml";
46
47     /**
48      * Logger.
49      */

50     private static JLog logger = JLogFactory.getLog(EmbeddedConfigurator.class);
51
52
53     /**
54      * Utility class, no public constructor.
55      */

56     private EmbeddedConfigurator() {
57
58     }
59
60     /**
61      * Configure the given embedded server with the given XML configuration file
62      * URL.
63      * @param embedded the embedded server to configure.
64      * @param xmlConfigurationURL the URL to the xml configuration file.
65      * @return the configured embedded instance.
66      * @throws EmbeddedException if the embedded configuration fails.
67      */

68     public static Embedded init(final Embedded embedded, final URL JavaDoc xmlConfigurationURL) throws EmbeddedException {
69         configure(embedded, xmlConfigurationURL);
70         return embedded;
71     }
72
73     /**
74      * Create and configure an embedded server with the given XML configuration
75      * file URL.
76      * @param xmlConfigurationURL the URL to the xml configuration file.
77      * @return the configured embedded instance.
78      * @throws EmbeddedException if the embedded configuration fails.
79      */

80     public static Embedded create(final URL JavaDoc xmlConfigurationURL) throws EmbeddedException {
81         return init(new Embedded(), xmlConfigurationURL);
82     }
83
84     /**
85      * Create and configure an embedded server with the XML configuration file
86      * URL found in classpath.
87      * @return the configured embedded instance.
88      * @throws EmbeddedException if the embedded configuration fails.
89      */

90     public static Embedded create() throws EmbeddedException {
91         URL JavaDoc xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(CONFIGURATION_FILE_NAME);
92         if (xmlConfigurationURL == null) {
93             throw new EmbeddedException("No configuration file with name '" + CONFIGURATION_FILE_NAME
94                     + "' was found in classpath.");
95         }
96         return create(xmlConfigurationURL);
97     }
98
99     /**
100      * Configure the given embedded server with the xml configuration file.
101      * @param embedded the embedded server to configure.
102      * @param xmlConfigurationURL the URL to the xml configuration file.
103      * @throws EmbeddedException if the embedded configuration fails.
104      */

105     private static void configure(final Embedded embedded, final URL JavaDoc xmlConfigurationURL) throws EmbeddedException {
106         long tStart = System.currentTimeMillis();
107         logger.info("Starting configuration of EasyBeans server");
108         XMLConfiguration xmlConfiguration = new XMLConfiguration(xmlConfigurationURL);
109         try {
110             xmlConfiguration.configure(embedded);
111         } catch (XMLConfigurationException e) {
112             throw new EmbeddedException("Cannot configure the embedded server", e);
113         }
114         if (logger.isInfoEnabled()) {
115             logger.info("Configuration done in : " + (System.currentTimeMillis() - tStart) + " ms");
116         }
117     }
118
119 }
120
Popular Tags