|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConfigurationInitializer.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.configuration;
|
|
58 |
|
|
59 |
import java.io.FileInputStream;
|
|
60 |
import java.io.IOException;
|
|
61 |
|
|
62 |
import java.util.Enumeration;
|
|
63 |
import java.util.MissingResourceException;
|
|
64 |
import java.util.PropertyResourceBundle;
|
|
65 |
import java.util.ResourceBundle;
|
|
66 |
|
|
67 |
import org.apache.cactus.util.ChainedRuntimeException;
|
|
68 |
import org.apache.cactus.util.ClassLoaderUtils;
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Read Cactus configuration files and set the properties found as
|
|
72 |
* System properties.
|
|
73 |
*
|
|
74 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
75 |
*
|
|
76 |
* @version $Id: ConfigurationInitializer.java,v 1.1.2.1 2003/10/23 15:10:09 vmassol Exp $
|
|
77 |
*/
|
|
78 |
public class ConfigurationInitializer |
|
79 |
{ |
|
80 |
/**
|
|
81 |
* Name of the Cactus configuration file if cactus is to look for it in
|
|
82 |
* the classpath.
|
|
83 |
*/
|
|
84 |
private static final String DEFAULT_CONFIG_NAME = "cactus"; |
|
85 |
|
|
86 |
/**
|
|
87 |
* Name of the java property for specifying the location of the cactus
|
|
88 |
* configuration file. This overrides any cactus configuration file that is
|
|
89 |
* put in the classpath.
|
|
90 |
*/
|
|
91 |
private static final String CACTUS_CONFIG_PROPERTY = "cactus.config"; |
|
92 |
|
|
93 |
/**
|
|
94 |
* Name of the Cactus property that points to a properties file
|
|
95 |
* containing logging configuration.
|
|
96 |
*/
|
|
97 |
private static final String CACTUS_LOGGING_CONFIG_PROPERTY = |
|
98 |
"cactus.logging.config";
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Have the Cactus configuration files been initialized?
|
|
102 |
*/
|
|
103 |
private static boolean isInitialized; |
|
104 |
|
|
105 |
/**
|
|
106 |
* Read Cactus configuration files.
|
|
107 |
*/
|
|
108 | 0 |
public static final synchronized void initialize() |
109 |
{ |
|
110 | 0 |
if (!isInitialized)
|
111 |
{ |
|
112 | 0 |
initializeConfig(); |
113 | 0 |
initializeLoggingConfig(); |
114 | 0 |
isInitialized = true;
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Initialize general cactus configuration. Read the cactus configuration
|
|
120 |
* file from the java property defined on the command line
|
|
121 |
* (named CACTUS_CONFIG_PROPERTY) and if none has been defined tries to
|
|
122 |
* read the DEFAULT_CONFIG_NAME file from the classpath. All properties
|
|
123 |
* found are exported as java system properties.
|
|
124 |
*/
|
|
125 | 0 |
private static void initializeConfig() |
126 |
{ |
|
127 | 0 |
ResourceBundle config; |
128 |
|
|
129 |
// Has the user passed the location of the cactus configuration
|
|
130 |
// file as a java property
|
|
131 | 0 |
String configOverride = System.getProperty(CACTUS_CONFIG_PROPERTY); |
132 |
|
|
133 | 0 |
if (configOverride == null) |
134 |
{ |
|
135 |
// Try to read the default cactus configuration file from the
|
|
136 |
// classpath
|
|
137 | 0 |
try
|
138 |
{ |
|
139 | 0 |
config = ClassLoaderUtils.loadPropertyResourceBundle( |
140 |
DEFAULT_CONFIG_NAME, ConfigurationInitializer.class);
|
|
141 |
} |
|
142 |
catch (MissingResourceException e)
|
|
143 |
{ |
|
144 |
// Cannot find cactus properties file. Do nothing.
|
|
145 | 0 |
return;
|
146 |
} |
|
147 |
} |
|
148 |
else
|
|
149 |
{ |
|
150 |
// Try to read from specified properties file
|
|
151 | 0 |
try
|
152 |
{ |
|
153 | 0 |
config = new PropertyResourceBundle(
|
154 |
new FileInputStream(configOverride));
|
|
155 |
} |
|
156 |
catch (IOException e)
|
|
157 |
{ |
|
158 | 0 |
throw new ChainedRuntimeException( |
159 |
"Cannot read cactus configuration file ["
|
|
160 |
+ configOverride + "]", e);
|
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 | 0 |
addSystemProperties(config); |
165 |
} |
|
166 |
|
|
167 |
/**
|
|
168 |
* Initialize logging configuration.
|
|
169 |
*/
|
|
170 | 0 |
private static void initializeLoggingConfig() |
171 |
{ |
|
172 | 0 |
String logConfig = System.getProperty(CACTUS_LOGGING_CONFIG_PROPERTY); |
173 | 0 |
if (logConfig != null) |
174 |
{ |
|
175 | 0 |
ResourceBundle bundle; |
176 | 0 |
try
|
177 |
{ |
|
178 | 0 |
bundle = new PropertyResourceBundle(
|
179 |
new FileInputStream(logConfig));
|
|
180 |
} |
|
181 |
catch (IOException e)
|
|
182 |
{ |
|
183 | 0 |
throw new ChainedRuntimeException("Failed to load logging " |
184 |
+ "configuration file [" + logConfig + "]"); |
|
185 |
} |
|
186 | 0 |
addSystemProperties(bundle); |
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
/**
|
|
191 |
* Add all properties found in the resource bundle as system
|
|
192 |
* properties.
|
|
193 |
*
|
|
194 |
* @param theBundle the resource bundle containing the properties to
|
|
195 |
* set as system properties
|
|
196 |
*/
|
|
197 | 0 |
private static void addSystemProperties(ResourceBundle theBundle) |
198 |
{ |
|
199 | 0 |
Enumeration keys = theBundle.getKeys(); |
200 |
|
|
201 | 0 |
while (keys.hasMoreElements())
|
202 |
{ |
|
203 | 0 |
String key = (String) keys.nextElement(); |
204 |
// Only set the system property if it does not already exist.
|
|
205 |
// This allows to have a cactus properties file and override
|
|
206 |
// some values on the command line.
|
|
207 | 0 |
if (System.getProperty(key) == null) |
208 |
{ |
|
209 | 0 |
System.setProperty(key, theBundle.getString(key)); |
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
|
|