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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ConfigurationManager.java,v 1.16 2004/01/03 04:00:56 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 05/15/2000   jima    Created
47   */
48  package org.exolab.jms.config;
49  
50  import java.io.File;
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 ConfigurationManager manages provides class methods for setting and
60   * getting the configuration file. It should be set by the application main
61   * line through {@link #setConfig} and subsequently accessed by other
62   * components through {@link #getConfig}.
63   *
64   * @version     $Revision: 1.16 $ $Date: 2004/01/03 04:00:56 $
65   * @author      <a href="mailto:jima@intalio.com">Jim Alateras</a>
66   */
67  public class ConfigurationManager {
68  
69      /***
70       * The loaded configuration
71       */
72      private static Configuration _config = null;
73  
74      /***
75       * Load the configuration file
76       *
77       * @param path xml config file conforming to openjms.xsd schema
78       * @throws FileDoesNotExistException if the file does not exist
79       * @throws ConfigurationFileException if the file is not well-formed
80       */
81      public static synchronized void setConfig(String path)
82          throws FileDoesNotExistException, ConfigurationFileException {
83          File config = new File(path);
84  
85          if (config.exists()) {
86              ConfigurationLoader loader = new ConfigurationLoader();
87              try {
88                  _config = loader.load(path);
89              } catch (Exception exception) {
90                  throw new ConfigurationFileException(
91                      "Error occured in " + path + " " + exception);
92              }
93          } else {
94              throw new FileDoesNotExistException(
95                  "Configuration file " + path + " does not exist.");
96          }
97      }
98  
99      /***
100      * Set the configuration
101      *
102      * @param config the configuration
103      */
104     public static synchronized void setConfig(Configuration config) {
105         if (config == null) {
106             throw new IllegalArgumentException("Argument 'config' is null");
107         }
108         _config = config;
109     }
110 
111     /***
112      * Returns the configuration
113      *
114      * @throws IllegalStateException if the configuration has not been
115      * initialised
116      * @return the configuration
117      */
118     public static synchronized Configuration getConfig() {
119         if (_config == null) {
120             throw new IllegalStateException(
121                 "Configuration manager has not been initialised");
122         }
123         return _config;
124     }
125 
126     /***
127      * Returns the connector configuration for the supplied scheme
128      *
129      * @param scheme the connector scheme
130      * @return the connector configuration for the supplied scheme, or null,
131      * if no configuration exists
132      * @throws IllegalArgumentException if scheme is null
133      * @throws IllegalStateException if the configuration is not initialised
134      */
135     public static Connector getConnector(SchemeType scheme) {
136         if (scheme == null) {
137             throw new IllegalArgumentException("Argument scheme is null");
138         }
139         Connector result = null;
140         Configuration config = getConfig();
141         Connector[] connectors = config.getConnectors().getConnector();
142         for (int i = 0; i < connectors.length; ++i) {
143             if (connectors[i].getScheme().equals(scheme)) {
144                 result = connectors[i];
145                 break;
146             }
147         }
148         return result;
149     }
150 
151     /***
152      * Returns the default connector. This is the first configured connector
153      * in the configuration.
154      *
155      * @return the default connector
156      * @throws IllegalStateException if the configuration is not initialised
157      * @deprecated This method relies on users knowing that the first connector
158      * is the one that will be used.
159      * @see #getConnector(SchemeType)
160      */
161     public static Connector getConnector() {
162         Configuration config = getConfig();
163         return config.getConnectors().getConnector(0);
164     }
165 
166 } //-- ConfigurationManager