KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > config > ConfigurationLoader


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ConfigurationLoader.java,v 1.2 2005/03/19 13:11:19 tanderson Exp $
44  */

45 package org.exolab.jms.config;
46
47 import java.io.FileReader JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.InputStreamReader JavaDoc;
51
52 import org.exolab.castor.xml.MarshalException;
53 import org.exolab.castor.xml.Unmarshaller;
54 import org.exolab.castor.xml.ValidationException;
55
56
57 /**
58  * The ConfigurationLoader loads a {@link Configuration} document and populates
59  * unset items with those provided by {@link DefaultConfiguration}.
60  *
61  * @author <a HREF="mailto:tima@intalio.com">Tim Anderson</a>
62  * @version $Revision: 1.2 $ $Date: 2005/03/19 13:11:19 $
63  * @see Configuration
64  */

65 public class ConfigurationLoader {
66
67     /**
68      * The default configuration path, as a resource.
69      */

70     private static final String JavaDoc DEFAULT_CONFIG = "openjms_defaults.xml";
71
72
73     /**
74      * Construct a new <code>ConfigurationLoader</code>.
75      */

76     public ConfigurationLoader() {
77     }
78
79     /**
80      * Loads the configuration information from the specified file. Unpopulated
81      * configuration elements will be set to those provided by {@link
82      * DefaultConfiguration}.
83      *
84      * @param path the path to the file
85      * @return the configuration
86      * @throws IOException for any I/O error
87      * @throws MarshalException if there is an error during unmarshalling
88      * @throws ValidationException if there is a validation error
89      */

90     public Configuration load(String JavaDoc path)
91             throws IOException JavaDoc, MarshalException, ValidationException {
92
93         Configuration result = null;
94         FileReader JavaDoc reader = new FileReader JavaDoc(path);
95         try {
96             Unmarshaller stream = new Unmarshaller(Configuration.class);
97             AttributeExpander handler = new AttributeExpander(reader);
98             result = (Configuration) stream.unmarshal(handler);
99         } finally {
100             reader.close();
101         }
102         return load(result);
103     }
104
105     /**
106      * Loads unpopulated elements in the supplied configuration with default
107      * values. The default values are provided by {@link DefaultConfiguration}.
108      *
109      * @param config the configuration
110      * @return the configuration, with unpopulated elements set to the defaults
111      * @throws IOException for any I/O error
112      * @throws MarshalException if there is an error during unmarshalling
113      * @throws ValidationException if there is a validation error
114      */

115     public Configuration load(Configuration config)
116             throws IOException JavaDoc, MarshalException, ValidationException {
117         DefaultConfiguration defaults = getDefaults();
118
119         if (config.getServerConfiguration() == null) {
120             config.setServerConfiguration(defaults.getServerConfiguration());
121         }
122         if (config.getConnectors() == null) {
123             config.setConnectors(defaults.getConnectors());
124         }
125         if (config.getLoggerConfiguration() == null) {
126             config.setLoggerConfiguration(defaults.getLoggerConfiguration());
127         }
128         if (config.getTcpConfiguration() == null) {
129             config.setTcpConfiguration(defaults.getTcpConfiguration());
130         }
131         if (config.getTcpsConfiguration() == null) {
132             config.setTcpsConfiguration(defaults.getTcpsConfiguration());
133         }
134         if (config.getRmiConfiguration() == null) {
135             config.setRmiConfiguration(defaults.getRmiConfiguration());
136         }
137         if (config.getHttpConfiguration() == null) {
138             config.setHttpConfiguration(defaults.getHttpConfiguration());
139         }
140         if (config.getHttpsConfiguration() == null) {
141             config.setHttpsConfiguration(defaults.getHttpsConfiguration());
142         }
143         if (config.getMessageManagerConfiguration() == null) {
144             config.setMessageManagerConfiguration(
145                     defaults.getMessageManagerConfiguration());
146         }
147         if (config.getSchedulerConfiguration() == null) {
148             config.setSchedulerConfiguration(
149                     defaults.getSchedulerConfiguration());
150         }
151         if (config.getGarbageCollectionConfiguration() == null) {
152             config.setGarbageCollectionConfiguration(
153                     defaults.getGarbageCollectionConfiguration());
154         }
155         if (config.getSecurityConfiguration() == null) {
156             config.setSecurityConfiguration(
157                     defaults.getSecurityConfiguration());
158         }
159         if (config.getServerConfiguration().getEmbeddedJNDI()) {
160             // populate the JNDI configuration with the default values for
161
// the connector
162
config.setJndiConfiguration(
163                     JndiConfigurationFactory.create(config));
164         } else if (config.getJndiConfiguration() == null) {
165             throw new ValidationException(
166                     "JndiConfiguration must be provided when "
167                     + "ServerConfiguration/embeddedJNDI is false");
168         }
169         return config;
170     }
171
172     /**
173      * Returns the default configuration, loaded from {@link #DEFAULT_CONFIG}.
174      *
175      * @return the default configuration
176      * @throws IOException for any I/O error
177      * @throws MarshalException if there is an error during unmarshalling
178      * @throws ValidationException if there is a validation error
179      */

180     private DefaultConfiguration getDefaults()
181             throws IOException JavaDoc, MarshalException, ValidationException {
182
183         DefaultConfiguration result = null;
184         InputStream JavaDoc source = getClass().getResourceAsStream(DEFAULT_CONFIG);
185         if (source == null) {
186             throw new IOException JavaDoc(
187                     "Failed to find default configuration: " + DEFAULT_CONFIG);
188         }
189         try {
190             Unmarshaller stream = new Unmarshaller(DefaultConfiguration.class);
191             AttributeExpander handler = new AttributeExpander(
192                     new InputStreamReader JavaDoc(source));
193             result = (DefaultConfiguration) stream.unmarshal(handler);
194         } finally {
195             try {
196                 source.close();
197             } catch (IOException JavaDoc ignore) {
198             }
199         }
200         return result;
201     }
202
203 }
204
Popular Tags