KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > CatalinaProperties


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

17
18
19 package org.apache.catalina.startup;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Properties JavaDoc;
27
28
29 /**
30  * Utility class to read the bootstrap Catalina configuration.
31  *
32  * @author Remy Maucherat
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class CatalinaProperties {
37
38
39     // ------------------------------------------------------- Static Variables
40

41     private static org.apache.commons.logging.Log log=
42         org.apache.commons.logging.LogFactory.getLog( CatalinaProperties.class );
43
44     private static Properties JavaDoc properties = null;
45
46
47     static {
48
49         loadProperties();
50
51     }
52
53
54     // --------------------------------------------------------- Public Methods
55

56
57     /**
58      * Return specified property value.
59      */

60     public static String JavaDoc getProperty(String JavaDoc name) {
61     
62         return properties.getProperty(name);
63
64     }
65
66
67     /**
68      * Return specified property value.
69      */

70     public static String JavaDoc getProperty(String JavaDoc name, String JavaDoc defaultValue) {
71
72         return properties.getProperty(name, defaultValue);
73
74     }
75
76
77     // --------------------------------------------------------- Public Methods
78

79
80     /**
81      * Load properties.
82      */

83     private static void loadProperties() {
84
85         InputStream JavaDoc is = null;
86         Throwable JavaDoc error = null;
87
88         try {
89             String JavaDoc configUrl = getConfigUrl();
90             if (configUrl != null) {
91                 is = (new URL JavaDoc(configUrl)).openStream();
92             }
93         } catch (Throwable JavaDoc t) {
94             // Ignore
95
}
96
97         if (is == null) {
98             try {
99                 File JavaDoc home = new File JavaDoc(getCatalinaBase());
100                 File JavaDoc conf = new File JavaDoc(home, "conf");
101                 File JavaDoc properties = new File JavaDoc(conf, "catalina.properties");
102                 is = new FileInputStream JavaDoc(properties);
103             } catch (Throwable JavaDoc t) {
104                 // Ignore
105
}
106         }
107
108         if (is == null) {
109             try {
110                 is = CatalinaProperties.class.getResourceAsStream
111                     ("/org/apache/catalina/startup/catalina.properties");
112             } catch (Throwable JavaDoc t) {
113                 // Ignore
114
}
115         }
116
117         if (is != null) {
118             try {
119                 properties = new Properties JavaDoc();
120                 properties.load(is);
121                 is.close();
122             } catch (Throwable JavaDoc t) {
123                 error = t;
124             }
125         }
126
127         if ((is == null) || (error != null)) {
128             // Do something
129
log.warn("Failed to load catalina.properties", error);
130             // That's fine - we have reasonable defaults.
131
properties=new Properties JavaDoc();
132         }
133
134         // Register the properties as system properties
135
Enumeration JavaDoc enumeration = properties.propertyNames();
136         while (enumeration.hasMoreElements()) {
137             String JavaDoc name = (String JavaDoc) enumeration.nextElement();
138             String JavaDoc value = properties.getProperty(name);
139             if (value != null) {
140                 System.setProperty(name, value);
141             }
142         }
143
144     }
145
146
147     /**
148      * Get the value of the catalina.home environment variable.
149      */

150     private static String JavaDoc getCatalinaHome() {
151         return System.getProperty("catalina.home",
152                                   System.getProperty("user.dir"));
153     }
154     
155     
156     /**
157      * Get the value of the catalina.base environment variable.
158      */

159     private static String JavaDoc getCatalinaBase() {
160         return System.getProperty("catalina.base", getCatalinaHome());
161     }
162
163
164     /**
165      * Get the value of the configuration URL.
166      */

167     private static String JavaDoc getConfigUrl() {
168         return System.getProperty("catalina.config");
169     }
170
171
172 }
173
Popular Tags