KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > configuration > ConfigurationInitializer


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.configuration;
21
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.MissingResourceException JavaDoc;
27 import java.util.PropertyResourceBundle JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import org.apache.cactus.internal.util.ClassLoaderUtils;
31 import org.apache.cactus.util.ChainedRuntimeException;
32
33 /**
34  * Read Cactus configuration files and set the properties found as
35  * System properties.
36  *
37  * @version $Id: ConfigurationInitializer.java,v 1.4 2004/06/27 15:23:30 vmassol Exp $
38  */

39 public class ConfigurationInitializer
40 {
41     /**
42      * Name of the Cactus configuration file if cactus is to look for it in
43      * the classpath.
44      */

45     private static final String JavaDoc DEFAULT_CONFIG_NAME = "cactus";
46
47     /**
48      * Name of the java property for specifying the location of the cactus
49      * configuration file. This overrides any cactus configuration file that is
50      * put in the classpath.
51      */

52     private static final String JavaDoc CACTUS_CONFIG_PROPERTY = "cactus.config";
53
54     /**
55      * Name of the Cactus property that points to a properties file
56      * containing logging configuration.
57      */

58     private static final String JavaDoc CACTUS_LOGGING_CONFIG_PROPERTY =
59         "cactus.logging.config";
60
61     /**
62      * Have the Cactus configuration files been initialized?
63      */

64     private static boolean isInitialized;
65     
66     /**
67      * Read Cactus configuration files.
68      *
69      * @param isReinitialization if true then force a re-read of the Cactus
70      * configuration files
71      */

72     public static synchronized void initialize(boolean isReinitialization)
73     {
74         if (!isInitialized)
75         {
76             initializeConfig(isReinitialization);
77             initializeLoggingConfig(isReinitialization);
78             isInitialized = true;
79         }
80     }
81
82     /**
83      * Read Cactus configuration files.
84      */

85     public static synchronized void initialize()
86     {
87         initialize(false);
88     }
89     
90     /**
91      * Initialize general cactus configuration. Read the cactus configuration
92      * file from the java property defined on the command line
93      * (named CACTUS_CONFIG_PROPERTY) and if none has been defined tries to
94      * read the DEFAULT_CONFIG_NAME file from the classpath. All properties
95      * found are exported as java system properties.
96      *
97      * @param isReinitialization if true then force a re-read of the Cactus
98      * configuration files
99      */

100     private static void initializeConfig(boolean isReinitialization)
101     {
102         ResourceBundle JavaDoc config;
103
104         // Has the user passed the location of the cactus configuration
105
// file as a java property
106
String JavaDoc configOverride = System.getProperty(CACTUS_CONFIG_PROPERTY);
107
108         if (configOverride == null)
109         {
110             // Try to read the default cactus configuration file from the
111
// classpath
112
try
113             {
114                 config = ClassLoaderUtils.loadPropertyResourceBundle(
115                     DEFAULT_CONFIG_NAME, ConfigurationInitializer.class);
116             }
117             catch (MissingResourceException JavaDoc e)
118             {
119                 // Cannot find cactus properties file. Do nothing.
120
return;
121             }
122         }
123         else
124         {
125             // Try to read from specified properties file
126
try
127             {
128                 config = new PropertyResourceBundle JavaDoc(
129                     new FileInputStream JavaDoc(configOverride));
130             }
131             catch (IOException JavaDoc e)
132             {
133                 throw new ChainedRuntimeException(
134                     "Cannot read cactus configuration file ["
135                     + configOverride + "]", e);
136             }
137         }
138
139         addSystemProperties(config, isReinitialization);
140     }
141
142     /**
143      * Initialize logging configuration.
144      *
145      * @param isReinitialization if true then force a re-read of the Cactus
146      * configuration files
147      */

148     private static void initializeLoggingConfig(boolean isReinitialization)
149     {
150         String JavaDoc logConfig = System.getProperty(CACTUS_LOGGING_CONFIG_PROPERTY);
151         if (logConfig != null)
152         {
153             ResourceBundle JavaDoc bundle;
154             try
155             {
156                 bundle = new PropertyResourceBundle JavaDoc(
157                     new FileInputStream JavaDoc(logConfig));
158             }
159             catch (IOException JavaDoc e)
160             {
161                 throw new ChainedRuntimeException("Failed to load logging "
162                     + "configuration file [" + logConfig + "]");
163             }
164             addSystemProperties(bundle, isReinitialization);
165         }
166     }
167
168     /**
169      * Add all properties found in the resource bundle as system
170      * properties.
171      *
172      * @param theBundle the resource bundle containing the properties to
173      * set as system properties
174      * @param isReinitialization if true then force a re-read of the Cactus
175      * configuration files
176      */

177     private static void addSystemProperties(ResourceBundle JavaDoc theBundle,
178             boolean isReinitialization)
179     {
180         Enumeration JavaDoc keys = theBundle.getKeys();
181
182         while (keys.hasMoreElements())
183         {
184             String JavaDoc key = (String JavaDoc) keys.nextElement();
185
186             // Only set the system property if it does not already exist.
187
// This allows system properties defined on the command line
188
// override Cactus properties located in the Cactus configuration
189
// files.
190
if ((System.getProperty(key) == null) || isReinitialization)
191             {
192                 System.setProperty(key, theBundle.getString(key));
193             }
194         }
195     }
196 }
197
Popular Tags