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 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ConfigurationLoader.java,v 1.9 2003/10/29 04:58:32 tanderson Exp $
44   */
45  package org.exolab.jms.config;
46  
47  import java.io.FileReader;
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  import org.exolab.jms.config.types.SchemeType;
56  
57  
58  /***
59   * The ConfigurationLoader loads a {@link Configuration} document and populates
60   * unset items with those provided by <code>DefaultConfiguration</code>
61   *
62   * @version     $Revision: 1.9 $ $Date: 2003/10/29 04:58:32 $
63   * @author      <a href="mailto:tima@intalio.com">Tim Anderson</a>
64   * @see         Configuration
65   */
66  public class ConfigurationLoader {
67  
68      /***
69       * The default configuration path as a resource
70       */
71      private static final String DEFAULT_CONFIG =
72          "/org/exolab/jms/config/openjms_defaults.xml";
73  
74      public ConfigurationLoader() {
75      }
76  
77      /***
78       * Load the configuraiton information from the specified file and
79       * return it.
80       *
81       * @param path the path to the file
82       * @return the configuration file
83       * @throws IOException
84       * @throws MarshalException
85       * @throws ValidationException
86       */
87      public Configuration load(String path)
88          throws IOException, MarshalException, ValidationException {
89  
90          Configuration result = null;
91          FileReader reader = new FileReader(path);
92          try {
93              Unmarshaller stream = new Unmarshaller(Configuration.class);
94              AttributeExpander handler = new AttributeExpander(reader);
95              result = (Configuration) stream.unmarshal(handler);
96          } finally {
97              reader.close();
98          }
99          return load(result);
100     }
101 
102     public Configuration load(Configuration config)
103         throws IOException, MarshalException, ValidationException {
104         DefaultConfiguration defaults = getDefaults();
105 
106         if (config.getServerConfiguration() == null) {
107             config.setServerConfiguration(defaults.getServerConfiguration());
108         }
109         if (config.getConnectors() == null) {
110             config.setConnectors(defaults.getConnectors());
111         }
112         if (config.getLoggerConfiguration() == null) {
113             config.setLoggerConfiguration(defaults.getLoggerConfiguration());
114         }
115         if (config.getTcpConfiguration() == null) {
116             config.setTcpConfiguration(defaults.getTcpConfiguration());
117         }
118         if (config.getTcpsConfiguration() == null) {
119             config.setTcpsConfiguration(defaults.getTcpsConfiguration());
120         }
121         if (config.getRmiConfiguration() == null) {
122             config.setRmiConfiguration(defaults.getRmiConfiguration());
123         }
124         if (config.getHttpConfiguration() == null) {
125             config.setHttpConfiguration(defaults.getHttpConfiguration());
126         }
127         if (config.getHttpsConfiguration() == null) {
128             config.setHttpsConfiguration(defaults.getHttpsConfiguration());
129         }
130         if (config.getMessageManagerConfiguration() == null) {
131             config.setMessageManagerConfiguration(
132                 defaults.getMessageManagerConfiguration());
133         }
134         if (config.getSchedulerConfiguration() == null) {
135             config.setSchedulerConfiguration(
136                 defaults.getSchedulerConfiguration());
137         }
138         if (config.getGarbageCollectionConfiguration() == null) {
139             config.setGarbageCollectionConfiguration(
140                 defaults.getGarbageCollectionConfiguration());
141         }
142         if (config.getSecurityConfiguration() == null) {
143             config.setSecurityConfiguration(
144                 defaults.getSecurityConfiguration());
145         }
146         if (config.getServerConfiguration().getEmbeddedJNDI()) {
147             // populate the JNDI configuration with the default values for
148             // the connector
149             config.setJndiConfiguration(
150                 JndiConfigurationFactory.create(config));
151         } else if (config.getJndiConfiguration() == null) {
152             throw new ValidationException(
153                 "JndiConfiguration must be provided when " +
154                 "ServerConfiguation/embeddedJNDI is true");
155         }
156         return config;
157     }
158 
159     private DefaultConfiguration getDefaults()
160         throws IOException, MarshalException, ValidationException {
161 
162         DefaultConfiguration result = null;
163         InputStream source = getClass().getResourceAsStream(DEFAULT_CONFIG);
164         if (source == null) {
165             throw new IOException(
166                 "Failed to find default configuration: " + DEFAULT_CONFIG);
167         }
168         try {
169             Unmarshaller stream = new Unmarshaller(DefaultConfiguration.class);
170             AttributeExpander handler = new AttributeExpander(
171                 new InputStreamReader(source));
172             result = (DefaultConfiguration) stream.unmarshal(handler);
173         } finally {
174             try {
175                 source.close();
176             } catch (IOException ignore) {
177             }
178         }
179         return result;
180     }
181 
182 } //-- ConfigurationLoader