1 16 17 18 package org.apache.tomcat.util.loader; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 27 28 50 public class LoaderProperties { 51 52 private static Properties properties = null; 53 54 static { 55 56 loadProperties(); 57 58 } 59 60 private static boolean DEBUG= getProperty("debug.LoaderProperties")!=null; 61 private static String propFile; 62 63 65 66 69 public static String getProperty(String name) { 70 return properties.getProperty(name); 71 } 72 73 74 77 public static String getProperty(String name, String defaultValue) { 78 return properties.getProperty(name, defaultValue); 79 } 80 81 82 84 85 98 private static void loadProperties() { 99 100 InputStream is = null; 101 Throwable error = null; 102 103 if( propFile != null ) { 106 try { 107 File properties = new File (propFile); 108 is = new FileInputStream (properties); 109 if( is!=null && DEBUG ) { 110 log("Loaded from loader.properties " + properties ); 111 } 112 } catch( Throwable t) { 113 System.err.println("Can't find " + propFile); 114 return; 115 } 116 } 117 118 if( is == null ) { 119 try { 120 String configUrl = System.getProperty("catalina.config"); 122 if (configUrl != null) { 123 is = (new URL (configUrl)).openStream(); 124 if( is!=null && DEBUG ) { 125 log("Loaded from catalina.config " + configUrl ); 126 } 127 } 128 } catch (Throwable t) { 129 } 131 } 132 133 if (is == null) { 134 try { 135 File home = new File (getCatalinaBase()); 136 File conf = new File (home, "conf"); 137 if( ! conf.exists() ) conf = new File (home, "../conf"); 139 if( ! conf.exists() ) conf=home; 140 File properties = new File (conf, "loader.properties"); 141 is = new FileInputStream (properties); 142 if( is!=null && DEBUG ) { 143 log("Loaded from loader.properties " + properties ); 144 } 145 } catch (Throwable t) { 146 } 148 } 149 150 if (is == null) { 151 try { 152 File home = new File (getCatalinaBase()); 153 File conf = new File (home, "conf"); 154 File properties = new File (conf, "catalina.properties"); 155 is = new FileInputStream (properties); 156 if( is!=null && DEBUG ) { 157 log("Loaded from catalina.properties " + properties ); 158 } 159 } catch (Throwable t) { 160 } 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 t) { 173 } 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 t) { 185 } 187 } 188 189 properties = new Properties (); 190 191 if (is != null) { 192 try { 193 properties.load(is); 194 is.close(); 195 } catch (Throwable t) { 196 error = t; 197 } 198 } 199 200 if ((is == null) || (error != null)) { 201 log("Error: no properties found !!!"); 203 } 204 205 if( properties != null ) { 207 Enumeration enumeration = properties.propertyNames(); 208 while (enumeration.hasMoreElements()) { 209 String name = (String ) enumeration.nextElement(); 210 String 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 228 static String getCatalinaHome() { 229 return System.getProperty("catalina.home", 230 System.getProperty("user.dir")); 231 } 232 233 234 237 static String getCatalinaBase() { 238 return System.getProperty("catalina.base", getCatalinaHome()); 239 } 240 241 242 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 264 static void setCatalinaHome() { 265 266 if (System.getProperty("catalina.home") != null) 267 return; 268 File bootstrapJar = 269 new File (System.getProperty("user.dir"), "bootstrap.jar"); 270 File tloaderJar = 271 new File (System.getProperty("user.dir"), "tomcat-loader.jar"); 272 if (bootstrapJar.exists() || tloaderJar.exists()) { 273 try { 274 System.setProperty 275 ("catalina.home", 276 (new File (System.getProperty("user.dir"), "..")) 277 .getCanonicalPath()); 278 } catch (Exception e) { 279 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 s ) { 292 System.err.println("LoaderProperties: "+ s); 293 } 294 295 296 299 public static void setPropertiesFile(String props) { 300 propFile=props; 301 loadProperties(); 302 } 303 } 304 | Popular Tags |