001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.configuration;
021:
022: import java.io.FileInputStream;
023: import java.io.IOException;
024:
025: import java.util.Enumeration;
026: import java.util.MissingResourceException;
027: import java.util.PropertyResourceBundle;
028: import java.util.ResourceBundle;
029:
030: import org.apache.cactus.internal.util.ClassLoaderUtils;
031: import org.apache.cactus.util.ChainedRuntimeException;
032:
033: /**
034: * Read Cactus configuration files and set the properties found as
035: * System properties.
036: *
037: * @version $Id: ConfigurationInitializer.java 239016 2004-06-27 15:23:30Z vmassol $
038: */
039: public class ConfigurationInitializer {
040: /**
041: * Name of the Cactus configuration file if cactus is to look for it in
042: * the classpath.
043: */
044: private static final String DEFAULT_CONFIG_NAME = "cactus";
045:
046: /**
047: * Name of the java property for specifying the location of the cactus
048: * configuration file. This overrides any cactus configuration file that is
049: * put in the classpath.
050: */
051: private static final String CACTUS_CONFIG_PROPERTY = "cactus.config";
052:
053: /**
054: * Name of the Cactus property that points to a properties file
055: * containing logging configuration.
056: */
057: private static final String CACTUS_LOGGING_CONFIG_PROPERTY = "cactus.logging.config";
058:
059: /**
060: * Have the Cactus configuration files been initialized?
061: */
062: private static boolean isInitialized;
063:
064: /**
065: * Read Cactus configuration files.
066: *
067: * @param isReinitialization if true then force a re-read of the Cactus
068: * configuration files
069: */
070: public static synchronized void initialize(
071: boolean isReinitialization) {
072: if (!isInitialized) {
073: initializeConfig(isReinitialization);
074: initializeLoggingConfig(isReinitialization);
075: isInitialized = true;
076: }
077: }
078:
079: /**
080: * Read Cactus configuration files.
081: */
082: public static synchronized void initialize() {
083: initialize(false);
084: }
085:
086: /**
087: * Initialize general cactus configuration. Read the cactus configuration
088: * file from the java property defined on the command line
089: * (named CACTUS_CONFIG_PROPERTY) and if none has been defined tries to
090: * read the DEFAULT_CONFIG_NAME file from the classpath. All properties
091: * found are exported as java system properties.
092: *
093: * @param isReinitialization if true then force a re-read of the Cactus
094: * configuration files
095: */
096: private static void initializeConfig(boolean isReinitialization) {
097: ResourceBundle config;
098:
099: // Has the user passed the location of the cactus configuration
100: // file as a java property
101: String configOverride = System
102: .getProperty(CACTUS_CONFIG_PROPERTY);
103:
104: if (configOverride == null) {
105: // Try to read the default cactus configuration file from the
106: // classpath
107: try {
108: config = ClassLoaderUtils.loadPropertyResourceBundle(
109: DEFAULT_CONFIG_NAME,
110: ConfigurationInitializer.class);
111: } catch (MissingResourceException e) {
112: // Cannot find cactus properties file. Do nothing.
113: return;
114: }
115: } else {
116: // Try to read from specified properties file
117: try {
118: config = new PropertyResourceBundle(
119: new FileInputStream(configOverride));
120: } catch (IOException e) {
121: throw new ChainedRuntimeException(
122: "Cannot read cactus configuration file ["
123: + configOverride + "]", e);
124: }
125: }
126:
127: addSystemProperties(config, isReinitialization);
128: }
129:
130: /**
131: * Initialize logging configuration.
132: *
133: * @param isReinitialization if true then force a re-read of the Cactus
134: * configuration files
135: */
136: private static void initializeLoggingConfig(
137: boolean isReinitialization) {
138: String logConfig = System
139: .getProperty(CACTUS_LOGGING_CONFIG_PROPERTY);
140: if (logConfig != null) {
141: ResourceBundle bundle;
142: try {
143: bundle = new PropertyResourceBundle(
144: new FileInputStream(logConfig));
145: } catch (IOException e) {
146: throw new ChainedRuntimeException(
147: "Failed to load logging "
148: + "configuration file [" + logConfig
149: + "]");
150: }
151: addSystemProperties(bundle, isReinitialization);
152: }
153: }
154:
155: /**
156: * Add all properties found in the resource bundle as system
157: * properties.
158: *
159: * @param theBundle the resource bundle containing the properties to
160: * set as system properties
161: * @param isReinitialization if true then force a re-read of the Cactus
162: * configuration files
163: */
164: private static void addSystemProperties(ResourceBundle theBundle,
165: boolean isReinitialization) {
166: Enumeration keys = theBundle.getKeys();
167:
168: while (keys.hasMoreElements()) {
169: String key = (String) keys.nextElement();
170:
171: // Only set the system property if it does not already exist.
172: // This allows system properties defined on the command line
173: // override Cactus properties located in the Cactus configuration
174: // files.
175: if ((System.getProperty(key) == null) || isReinitialization) {
176: System.setProperty(key, theBundle.getString(key));
177: }
178: }
179: }
180: }
|