View Javadoc

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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ConfigurationReader.java,v 1.1 2005/10/20 14:13:25 tanderson Exp $
44   */
45  package org.exolab.jms.config;
46  
47  import java.io.FileInputStream;
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.io.InputStreamReader;
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 ConfigurationReader reads a {@link Configuration} document and populates
59   * unset items with those provided by {@link DefaultConfiguration}.
60   *
61   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   * @version $Revision: 1.1 $ $Date: 2005/10/20 14:13:25 $
63   * @see Configuration
64   */
65  public final class ConfigurationReader {
66  
67      /***
68       * The default configuration path, as a resource.
69       */
70      private static final String DEFAULT_CONFIG = "openjms_defaults.xml";
71  
72  
73      /***
74       * Prevent construction of utility class.
75       */
76      private ConfigurationReader() {
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 static Configuration read(String path)
91              throws IOException, MarshalException, ValidationException {
92          InputStream stream = new FileInputStream(path);
93          return read(stream);
94      }
95  
96      /***
97       * Loads the configuration from a stream.
98       * Unpopulated configuration elements will be set to those provided by
99       * {@link  DefaultConfiguration}.
100      *
101      * @param stream the stream to read from
102      * @return the configuration
103      * @throws IOException         for any I/O error
104      * @throws MarshalException    if there is an error during unmarshalling
105      * @throws ValidationException if there is a validation error
106      */
107     public static Configuration read(InputStream stream)
108             throws IOException, MarshalException, ValidationException {
109         Configuration result = null;
110         Unmarshaller unmarshaller = new Unmarshaller(Configuration.class);
111         InputStreamReader reader = new InputStreamReader(stream);
112         AttributeExpander handler = new AttributeExpander(reader);
113         result = (Configuration) unmarshaller.unmarshal(handler);
114         return setDefaults(result);
115     }
116 
117     /***
118      * Sets unpopulated elements in the supplied configuration with default
119      * values. The default values are provided by {@link DefaultConfiguration}.
120      *
121      * @param config the configuration
122      * @return the configuration, with unpopulated elements set to the defaults
123      * @throws IOException         for any I/O error
124      * @throws MarshalException    if there is an error during unmarshalling
125      * @throws ValidationException if there is a validation error
126      */
127     public static Configuration setDefaults(Configuration config)
128             throws IOException, MarshalException, ValidationException {
129         DefaultConfiguration defaults = getDefaults();
130 
131         if (config.getServerConfiguration() == null) {
132             config.setServerConfiguration(defaults.getServerConfiguration());
133         }
134         if (config.getConnectors() == null) {
135             config.setConnectors(defaults.getConnectors());
136         }
137         if (config.getLoggerConfiguration() == null) {
138             config.setLoggerConfiguration(defaults.getLoggerConfiguration());
139         }
140         if (config.getTcpConfiguration() == null) {
141             config.setTcpConfiguration(defaults.getTcpConfiguration());
142         }
143         if (config.getTcpsConfiguration() == null) {
144             config.setTcpsConfiguration(defaults.getTcpsConfiguration());
145         }
146         if (config.getRmiConfiguration() == null) {
147             config.setRmiConfiguration(defaults.getRmiConfiguration());
148         }
149         if (config.getHttpConfiguration() == null) {
150             config.setHttpConfiguration(defaults.getHttpConfiguration());
151         }
152         if (config.getHttpsConfiguration() == null) {
153             config.setHttpsConfiguration(defaults.getHttpsConfiguration());
154         }
155         if (config.getMessageManagerConfiguration() == null) {
156             config.setMessageManagerConfiguration(
157                     defaults.getMessageManagerConfiguration());
158         }
159         if (config.getSchedulerConfiguration() == null) {
160             config.setSchedulerConfiguration(
161                     defaults.getSchedulerConfiguration());
162         }
163         if (config.getGarbageCollectionConfiguration() == null) {
164             config.setGarbageCollectionConfiguration(
165                     defaults.getGarbageCollectionConfiguration());
166         }
167         if (config.getSecurityConfiguration() == null) {
168             config.setSecurityConfiguration(
169                     defaults.getSecurityConfiguration());
170         }
171         if (config.getServerConfiguration().getEmbeddedJNDI()) {
172             // populate the JNDI configuration with the default values for
173             // the connector
174             config.setJndiConfiguration(
175                     JndiConfigurationFactory.create(config));
176         } else if (config.getJndiConfiguration() == null) {
177             throw new ValidationException(
178                     "JndiConfiguration must be provided when "
179                     + "ServerConfiguration/embeddedJNDI is false");
180         }
181         return config;
182     }
183 
184     /***
185      * Returns the default configuration, loaded from {@link #DEFAULT_CONFIG}.
186      *
187      * @return the default configuration
188      * @throws IOException         for any I/O error
189      * @throws MarshalException    if there is an error during unmarshalling
190      * @throws ValidationException if there is a validation error
191      */
192     private static DefaultConfiguration getDefaults()
193             throws IOException, MarshalException, ValidationException {
194 
195         DefaultConfiguration result = null;
196         InputStream source = Configuration.class.getResourceAsStream(
197                 DEFAULT_CONFIG);
198         if (source == null) {
199             throw new IOException(
200                     "Failed to find default configuration: " + DEFAULT_CONFIG);
201         }
202         try {
203             Unmarshaller stream = new Unmarshaller(DefaultConfiguration.class);
204             AttributeExpander handler = new AttributeExpander(
205                     new InputStreamReader(source));
206             result = (DefaultConfiguration) stream.unmarshal(handler);
207         } finally {
208             try {
209                 source.close();
210             } catch (IOException ignore) {
211             }
212         }
213         return result;
214     }
215 
216 }