001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.startup;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.Enumeration;
024: import java.util.Properties;
025:
026: /**
027: * Utility class to read the bootstrap Catalina configuration.
028: *
029: * @author Remy Maucherat
030: * @version $Revision: 1.6 $ $Date: 2004/05/18 16:33:24 $
031: */
032:
033: public class CatalinaProperties {
034:
035: // ------------------------------------------------------- Static Variables
036:
037: private static Properties properties = null;
038:
039: static {
040:
041: loadProperties();
042:
043: }
044:
045: // --------------------------------------------------------- Public Methods
046:
047: /**
048: * Return specified property value.
049: */
050: public static String getProperty(String name) {
051:
052: return properties.getProperty(name);
053:
054: }
055:
056: /**
057: * Return specified property value.
058: */
059: public static String getProperty(String name, String defaultValue) {
060:
061: return properties.getProperty(name, defaultValue);
062:
063: }
064:
065: // --------------------------------------------------------- Public Methods
066:
067: /**
068: * Load properties.
069: */
070: private static void loadProperties() {
071:
072: InputStream is = null;
073: Throwable error = null;
074:
075: try {
076: String configUrl = getConfigUrl();
077: if (configUrl != null) {
078: is = (new URL(configUrl)).openStream();
079: }
080: } catch (Throwable t) {
081: // Ignore
082: }
083:
084: if (is == null) {
085: try {
086: File home = new File(getCatalinaBase());
087: File conf = new File(home, "conf");
088: File properties = new File(conf, "catalina.properties");
089: is = new FileInputStream(properties);
090: } catch (Throwable t) {
091: // Ignore
092: }
093: }
094:
095: if (is == null) {
096: try {
097: is = CatalinaProperties.class.getResourceAsStream
098: ("/org/apache/catalina/startup/catalina.properties");
099: } catch (Throwable t) {
100: // Ignore
101: }
102: }
103:
104: if (is != null) {
105: try {
106: properties = new Properties();
107: properties.load(is);
108: is.close();
109: } catch (Throwable t) {
110: error = t;
111: }
112: }
113:
114: if ((is == null) || (error != null)) {
115: // Do something
116: System.out.println("Error");
117: }
118:
119: // Register the properties as system properties
120: Enumeration enum = properties.propertyNames();
121: while (enum.hasMoreElements()) {
122: String name = (String) enum.nextElement();
123: String value = properties.getProperty(name);
124: if (value != null) {
125: System.setProperty(name, value);
126: }
127: }
128:
129: }
130:
131: /**
132: * Get the value of the catalina.home environment variable.
133: */
134: private static String getCatalinaHome() {
135: return System.getProperty("catalina.home", System
136: .getProperty("user.dir"));
137: }
138:
139: /**
140: * Get the value of the catalina.base environment variable.
141: */
142: private static String getCatalinaBase() {
143: return System.getProperty("catalina.base", getCatalinaHome());
144: }
145:
146: /**
147: * Get the value of the configuration URL.
148: */
149: private static String getConfigUrl() {
150: return System.getProperty("catalina.config");
151: }
152:
153: }
|