1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.configuration;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25
26 import java.util.Enumeration;
27 import java.util.MissingResourceException;
28 import java.util.PropertyResourceBundle;
29 import java.util.ResourceBundle;
30
31 import org.apache.cactus.internal.util.ClassLoaderUtils;
32 import org.apache.cactus.util.ChainedRuntimeException;
33
34 /**
35 * Read Cactus configuration files and set the properties found as
36 * System properties.
37 *
38 * @version $Id: ConfigurationInitializer.java 239016 2004-06-27 15:23:30Z vmassol $
39 */
40 public class ConfigurationInitializer
41 {
42 /**
43 * Name of the Cactus configuration file if cactus is to look for it in
44 * the classpath.
45 */
46 private static final String DEFAULT_CONFIG_NAME = "cactus";
47
48 /**
49 * Name of the java property for specifying the location of the cactus
50 * configuration file. This overrides any cactus configuration file that is
51 * put in the classpath.
52 */
53 private static final String CACTUS_CONFIG_PROPERTY = "cactus.config";
54
55 /**
56 * Name of the Cactus property that points to a properties file
57 * containing logging configuration.
58 */
59 private static final String CACTUS_LOGGING_CONFIG_PROPERTY =
60 "cactus.logging.config";
61
62 /**
63 * Have the Cactus configuration files been initialized?
64 */
65 private static boolean isInitialized;
66
67 /**
68 * Read Cactus configuration files.
69 *
70 * @param isReinitialization if true then force a re-read of the Cactus
71 * configuration files
72 */
73 public static synchronized void initialize(boolean isReinitialization)
74 {
75 if (!isInitialized)
76 {
77 initializeConfig(isReinitialization);
78 initializeLoggingConfig(isReinitialization);
79 isInitialized = true;
80 }
81 }
82
83 /**
84 * Read Cactus configuration files.
85 */
86 public static synchronized void initialize()
87 {
88 initialize(false);
89 }
90
91 /**
92 * Initialize general cactus configuration. Read the cactus configuration
93 * file from the java property defined on the command line
94 * (named CACTUS_CONFIG_PROPERTY) and if none has been defined tries to
95 * read the DEFAULT_CONFIG_NAME file from the classpath. All properties
96 * found are exported as java system properties.
97 *
98 * @param isReinitialization if true then force a re-read of the Cactus
99 * configuration files
100 */
101 private static void initializeConfig(boolean isReinitialization)
102 {
103 ResourceBundle config;
104
105 // Has the user passed the location of the cactus configuration
106 // file as a java property
107 String configOverride = System.getProperty(CACTUS_CONFIG_PROPERTY);
108
109 if (configOverride == null)
110 {
111 // Try to read the default cactus configuration file from the
112 // classpath
113 try
114 {
115 config = ClassLoaderUtils.loadPropertyResourceBundle(
116 DEFAULT_CONFIG_NAME, ConfigurationInitializer.class);
117 }
118 catch (MissingResourceException e)
119 {
120 // Cannot find cactus properties file. Do nothing.
121 return;
122 }
123 }
124 else
125 {
126 // Try to read from specified properties file
127 try
128 {
129 config = new PropertyResourceBundle(
130 new FileInputStream(configOverride));
131 }
132 catch (IOException e)
133 {
134 throw new ChainedRuntimeException(
135 "Cannot read cactus configuration file ["
136 + configOverride + "]", e);
137 }
138 }
139
140 addSystemProperties(config, isReinitialization);
141 }
142
143 /**
144 * Initialize logging configuration.
145 *
146 * @param isReinitialization if true then force a re-read of the Cactus
147 * configuration files
148 */
149 private static void initializeLoggingConfig(boolean isReinitialization)
150 {
151 String logConfig = System.getProperty(CACTUS_LOGGING_CONFIG_PROPERTY);
152 if (logConfig != null)
153 {
154 ResourceBundle bundle;
155 try
156 {
157 bundle = new PropertyResourceBundle(
158 new FileInputStream(logConfig));
159 }
160 catch (IOException e)
161 {
162 throw new ChainedRuntimeException("Failed to load logging "
163 + "configuration file [" + logConfig + "]");
164 }
165 addSystemProperties(bundle, isReinitialization);
166 }
167 }
168
169 /**
170 * Add all properties found in the resource bundle as system
171 * properties.
172 *
173 * @param theBundle the resource bundle containing the properties to
174 * set as system properties
175 * @param isReinitialization if true then force a re-read of the Cactus
176 * configuration files
177 */
178 private static void addSystemProperties(ResourceBundle theBundle,
179 boolean isReinitialization)
180 {
181 Enumeration keys = theBundle.getKeys();
182
183 while (keys.hasMoreElements())
184 {
185 String key = (String) keys.nextElement();
186
187 // Only set the system property if it does not already exist.
188 // This allows system properties defined on the command line
189 // override Cactus properties located in the Cactus configuration
190 // files.
191 if ((System.getProperty(key) == null) || isReinitialization)
192 {
193 System.setProperty(key, theBundle.getString(key));
194 }
195 }
196 }
197 }