KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > loader > LoaderProperties


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

16
17
18 package org.apache.tomcat.util.loader;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26
27
28 /**
29  * Load a properties file describing the modules and startup sequence.
30  *
31  * The properties file will be named "loader.properties" or
32  * "catalina.properties" ( for backwad compatibility ) and
33  * will be searched in:
34  * - TODO
35  *
36  * Properties used:
37  * - TODO
38  *
39  * loader.* and *.loader properties are used internally by the loader (
40  * *.loader is for backward compat with catalina ).
41  * All other properties in the config file are set as System properties.
42  *
43  *
44  * Based on o.a.catalina.bootstrap.CatalinaProperties - utility class to read
45  * the bootstrap Catalina configuration.
46  *
47  * @author Remy Maucherat
48  * @author Costin Manolache
49  */

50 public class LoaderProperties {
51
52     private static Properties JavaDoc properties = null;
53
54     static {
55
56         loadProperties();
57
58     }
59
60     private static boolean DEBUG= getProperty("debug.LoaderProperties")!=null;
61     private static String JavaDoc propFile;
62
63     // --------------------------------------------------------- Public Methods
64

65
66     /**
67      * Return specified property value.
68      */

69     public static String JavaDoc getProperty(String JavaDoc name) {
70         return properties.getProperty(name);
71     }
72
73
74     /**
75      * Return specified property value.
76      */

77     public static String JavaDoc getProperty(String JavaDoc name, String JavaDoc defaultValue) {
78         return properties.getProperty(name, defaultValue);
79     }
80
81
82     // --------------------------------------------------------- Public Methods
83

84
85     /**
86      * Load properties.
87      * Will try:
88      * - "catalina.config" system property ( a URL )
89      * - "catalina.base", "catalina.home", "user.dir" system properties +
90      * "/conf/" "../conf" "/" + "loader.properties" or "catalina.properties"
91      * - /org/apache/catalina/startup/catalina.properties
92      *
93      * Properties will be loaded as system properties.
94      *
95      * loader.properties was added to allow coexistence with bootstrap.jar ( the
96      * current scheme ), since the classpaths are slightly different.
97      */

98     private static void loadProperties() {
99
100         InputStream JavaDoc is = null;
101         Throwable JavaDoc error = null;
102
103         // TODO: paste the code to do ${} substitution
104
// TODO: add the code to detect where tomcat-properties is loaded from
105
if( propFile != null ) {
106             try {
107                 File JavaDoc properties = new File JavaDoc(propFile);
108                 is = new FileInputStream JavaDoc(properties);
109                 if( is!=null && DEBUG ) {
110                     log("Loaded from loader.properties " + properties );
111                 }
112             } catch( Throwable JavaDoc t) {
113                 System.err.println("Can't find " + propFile);
114                 return;
115             }
116         }
117         
118         if( is == null ) {
119             try {
120                 // "catalina.config" system property
121
String JavaDoc configUrl = System.getProperty("catalina.config");
122                 if (configUrl != null) {
123                     is = (new URL JavaDoc(configUrl)).openStream();
124                     if( is!=null && DEBUG ) {
125                         log("Loaded from catalina.config " + configUrl );
126                     }
127                 }
128             } catch (Throwable JavaDoc t) {
129                 // Ignore
130
}
131         }
132
133         if (is == null) {
134             try {
135                 File JavaDoc home = new File JavaDoc(getCatalinaBase());
136                 File JavaDoc conf = new File JavaDoc(home, "conf");
137                 // use conf if exists, or the base directory otherwise
138
if( ! conf.exists() ) conf = new File JavaDoc(home, "../conf");
139                 if( ! conf.exists() ) conf=home;
140                 File JavaDoc properties = new File JavaDoc(conf, "loader.properties");
141                 is = new FileInputStream JavaDoc(properties);
142                 if( is!=null && DEBUG ) {
143                     log("Loaded from loader.properties " + properties );
144                 }
145             } catch (Throwable JavaDoc t) {
146                 // Ignore
147
}
148         }
149
150         if (is == null) {
151             try {
152                 File JavaDoc home = new File JavaDoc(getCatalinaBase());
153                 File JavaDoc conf = new File JavaDoc(home, "conf");
154                 File JavaDoc properties = new File JavaDoc(conf, "catalina.properties");
155                 is = new FileInputStream JavaDoc(properties);
156                 if( is!=null && DEBUG ) {
157                     log("Loaded from catalina.properties " + properties );
158                 }
159             } catch (Throwable JavaDoc t) {
160                 // Ignore
161
}
162         }
163
164         if (is == null) {
165             try {
166                 is = LoaderProperties.class.getResourceAsStream
167                     ("/org/apache/catalina/startup/catalina.properties");
168                 if( is!=null && DEBUG ) {
169                     log("Loaded from o/a/c/startup/catalina.properties " );
170                 }
171
172             } catch (Throwable JavaDoc t) {
173                 // Ignore
174
}
175         }
176
177         if (is == null) {
178             try {
179                 is = LoaderProperties.class.getResourceAsStream
180                     ("/org/apache/tomcat/util/loader/loader.properties");
181                 if( is!=null && DEBUG ) {
182                     log("Loaded from o/a/t/u/loader/loader.properties " );
183                 }
184             } catch (Throwable JavaDoc t) {
185                 // Ignore
186
}
187         }
188         
189         properties = new Properties JavaDoc();
190         
191         if (is != null) {
192             try {
193                 properties.load(is);
194                 is.close();
195             } catch (Throwable JavaDoc t) {
196                 error = t;
197             }
198         }
199
200         if ((is == null) || (error != null)) {
201             // Do something
202
log("Error: no properties found !!!");
203         }
204
205         // Register the _unused_ properties as system properties
206
if( properties != null ) {
207             Enumeration JavaDoc enumeration = properties.propertyNames();
208             while (enumeration.hasMoreElements()) {
209                 String JavaDoc name = (String JavaDoc) enumeration.nextElement();
210                 String JavaDoc value = properties.getProperty(name);
211                 if( "security.preload".equals( name )) continue;
212                 if( "package.access".equals( name )) continue;
213                 if( "package.definition".equals( name )) continue;
214                 if( name.endsWith(".loader")) continue;
215                 if( name.startsWith("loader.")) continue;
216                 if (value != null) {
217                     System.setProperty(name, value);
218                 }
219             }
220         }
221
222     }
223
224
225     /**
226      * Get the value of the catalina.home environment variable.
227      */

228     static String JavaDoc getCatalinaHome() {
229         return System.getProperty("catalina.home",
230                                   System.getProperty("user.dir"));
231     }
232     
233     
234     /**
235      * Get the value of the catalina.base environment variable.
236      */

237     static String JavaDoc getCatalinaBase() {
238         return System.getProperty("catalina.base", getCatalinaHome());
239     }
240
241     
242     /**
243      * Set the <code>catalina.base</code> System property to the current
244      * working directory if it has not been set.
245      */

246     static void setCatalinaBase() {
247
248         if (System.getProperty("catalina.base") != null)
249             return;
250         if (System.getProperty("catalina.home") != null)
251             System.setProperty("catalina.base",
252                                System.getProperty("catalina.home"));
253         else
254             System.setProperty("catalina.base",
255                                System.getProperty("user.dir"));
256
257     }
258
259
260     /**
261      * Set the <code>catalina.home</code> System property to the current
262      * working directory if it has not been set.
263      */

264     static void setCatalinaHome() {
265
266         if (System.getProperty("catalina.home") != null)
267             return;
268         File JavaDoc bootstrapJar =
269             new File JavaDoc(System.getProperty("user.dir"), "bootstrap.jar");
270         File JavaDoc tloaderJar =
271             new File JavaDoc(System.getProperty("user.dir"), "tomcat-loader.jar");
272         if (bootstrapJar.exists() || tloaderJar.exists()) {
273             try {
274                 System.setProperty
275                     ("catalina.home",
276                      (new File JavaDoc(System.getProperty("user.dir"), ".."))
277                      .getCanonicalPath());
278             } catch (Exception JavaDoc e) {
279                 // Ignore
280
System.setProperty("catalina.home",
281                                    System.getProperty("user.dir"));
282             }
283         } else {
284             System.setProperty("catalina.home",
285                                System.getProperty("user.dir"));
286         }
287
288     }
289
290
291     private static void log(String JavaDoc s ) {
292         System.err.println("LoaderProperties: "+ s);
293     }
294
295
296     /**
297      * @param props
298      */

299     public static void setPropertiesFile(String JavaDoc props) {
300         propFile=props;
301         loadProperties();
302     }
303 }
304
Popular Tags