KickJava   Java API By Example, From Geeks To Geeks.

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


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.1 2004/11/26 01:50:41 tanderson Exp $
44  */

45 package org.exolab.jms.config;
46
47 import java.io.File JavaDoc;
48
49 import org.exolab.castor.xml.MarshalException;
50 import org.exolab.castor.xml.Unmarshaller;
51 import org.exolab.castor.xml.ValidationException;
52 import org.exolab.jms.config.types.SchemeType;
53
54
55 /**
56  * The ConfigurationManager manages provides class methods for setting and
57  * getting the configuration file. It should be set by the application main
58  * line through {@link #setConfig} and subsequently accessed by other
59  * components through {@link #getConfig}.
60  *
61  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:41 $
62  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
63  */

64 public class ConfigurationManager {
65
66     /**
67      * The loaded configuration
68      */

69     private static Configuration _config = null;
70
71     /**
72      * Load the configuration file
73      *
74      * @param path xml config file conforming to openjms.xsd schema
75      * @throws FileDoesNotExistException if the file does not exist
76      * @throws ConfigurationFileException if the file is not well-formed
77      */

78     public static synchronized void setConfig(String JavaDoc path)
79         throws FileDoesNotExistException, ConfigurationFileException {
80         File JavaDoc config = new File JavaDoc(path);
81
82         if (config.exists()) {
83             ConfigurationLoader loader = new ConfigurationLoader();
84             try {
85                 _config = loader.load(path);
86             } catch (Exception JavaDoc exception) {
87                 throw new ConfigurationFileException(
88                     "Error occured in " + path + " " + exception);
89             }
90         } else {
91             throw new FileDoesNotExistException(
92                 "Configuration file " + path + " does not exist.");
93         }
94     }
95
96     /**
97      * Set the configuration
98      *
99      * @param config the configuration
100      */

101     public static synchronized void setConfig(Configuration config) {
102         if (config == null) {
103             throw new IllegalArgumentException JavaDoc("Argument 'config' is null");
104         }
105         _config = config;
106     }
107
108     /**
109      * Returns the configuration
110      *
111      * @throws IllegalStateException if the configuration has not been
112      * initialised
113      * @return the configuration
114      */

115     public static synchronized Configuration getConfig() {
116         if (_config == null) {
117             throw new IllegalStateException JavaDoc(
118                 "Configuration manager has not been initialised");
119         }
120         return _config;
121     }
122
123     /**
124      * Returns the connector configuration for the supplied scheme
125      *
126      * @param scheme the connector scheme
127      * @return the connector configuration for the supplied scheme, or null,
128      * if no configuration exists
129      * @throws IllegalArgumentException if scheme is null
130      * @throws IllegalStateException if the configuration is not initialised
131      * @deprecated
132      */

133     public static Connector getConnector(SchemeType scheme) {
134         if (scheme == null) {
135             throw new IllegalArgumentException JavaDoc("Argument scheme is null");
136         }
137         Connector result = null;
138         Configuration config = getConfig();
139         Connector[] connectors = config.getConnectors().getConnector();
140         for (int i = 0; i < connectors.length; ++i) {
141             if (connectors[i].getScheme().equals(scheme)) {
142                 result = connectors[i];
143                 break;
144             }
145         }
146         return result;
147     }
148
149     /**
150      * Returns the default connector. This is the first configured connector
151      * in the configuration.
152      *
153      * @return the default connector
154      * @throws IllegalStateException if the configuration is not initialised
155      * @deprecated This method relies on users knowing that the first connector
156      * is the one that will be used.
157      * @see #getConnector(SchemeType)
158      */

159     public static Connector getConnector() {
160         Configuration config = getConfig();
161         return config.getConnectors().getConnector(0);
162     }
163
164 }
165
Popular Tags