|
| 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 | +package org.apache.catalina.startup; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileInputStream; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.net.URL; |
| 23 | +import java.util.Enumeration; |
| 24 | +import java.util.Properties; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility class to read the bootstrap Catalina configuration. |
| 29 | + * |
| 30 | + * @author Remy Maucherat |
| 31 | + */ |
| 32 | +public class CatalinaProperties { |
| 33 | + |
| 34 | + private static final org.apache.juli.logging.Log log= |
| 35 | + org.apache.juli.logging.LogFactory.getLog( CatalinaProperties.class ); |
| 36 | + |
| 37 | + private static Properties properties = null; |
| 38 | + |
| 39 | + |
| 40 | + static { |
| 41 | + loadProperties(); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Return specified property value. |
| 47 | + */ |
| 48 | + public static String getProperty(String name) { |
| 49 | + return properties.getProperty(name); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Load properties. |
| 55 | + */ |
| 56 | + private static void loadProperties() { |
| 57 | + |
| 58 | + InputStream is = null; |
| 59 | + Throwable error = null; |
| 60 | + |
| 61 | + try { |
| 62 | + String configUrl = getConfigUrl(); |
| 63 | + if (configUrl != null) { |
| 64 | + is = (new URL(configUrl)).openStream(); |
| 65 | + } |
| 66 | + } catch (Throwable t) { |
| 67 | + handleThrowable(t); |
| 68 | + } |
| 69 | + |
| 70 | + if (is == null) { |
| 71 | + try { |
| 72 | + File home = new File(Bootstrap.getCatalinaBase()); |
| 73 | + File conf = new File(home, "conf"); |
| 74 | + File propsFile = new File(conf, "catalina.properties"); |
| 75 | + is = new FileInputStream(propsFile); |
| 76 | + } catch (Throwable t) { |
| 77 | + handleThrowable(t); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (is == null) { |
| 82 | + try { |
| 83 | + is = CatalinaProperties.class.getResourceAsStream |
| 84 | + ("/org/apache/catalina/startup/catalina.properties"); |
| 85 | + } catch (Throwable t) { |
| 86 | + handleThrowable(t); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (is != null) { |
| 91 | + try { |
| 92 | + properties = new Properties(); |
| 93 | + properties.load(is); |
| 94 | + is.close(); |
| 95 | + } catch (Throwable t) { |
| 96 | + handleThrowable(t); |
| 97 | + error = t; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if ((is == null) || (error != null)) { |
| 102 | + // Do something |
| 103 | + log.warn("Failed to load catalina.properties", error); |
| 104 | + // That's fine - we have reasonable defaults. |
| 105 | + properties=new Properties(); |
| 106 | + } |
| 107 | + |
| 108 | + // Register the properties as system properties |
| 109 | + Enumeration<?> enumeration = properties.propertyNames(); |
| 110 | + while (enumeration.hasMoreElements()) { |
| 111 | + String name = (String) enumeration.nextElement(); |
| 112 | + String value = properties.getProperty(name); |
| 113 | + if (value != null) { |
| 114 | + System.setProperty(name, value); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the value of the configuration URL. |
| 122 | + */ |
| 123 | + private static String getConfigUrl() { |
| 124 | + return System.getProperty("catalina.config"); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + // Copied from ExceptionUtils since that class is not visible during start |
| 129 | + private static void handleThrowable(Throwable t) { |
| 130 | + if (t instanceof ThreadDeath) { |
| 131 | + throw (ThreadDeath) t; |
| 132 | + } |
| 133 | + if (t instanceof VirtualMachineError) { |
| 134 | + throw (VirtualMachineError) t; |
| 135 | + } |
| 136 | + // All other instances of Throwable will be silently swallowed |
| 137 | + } |
| 138 | +} |
0 commit comments