1 23 24 29 42 43 48 49 package com.sun.enterprise.util; 50 51 import java.util.Properties ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.util.logging.Logger ; 56 import java.util.logging.Level ; 57 import com.sun.logging.LogDomains; 58 60 public class PropertyWrapper extends java.lang.Object implements java.io.Serializable 61 { 62 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 64 66 protected boolean bTryDefaultFileOnEnvFailure = true; 67 68 private boolean bLoaded = false; 69 70 protected boolean bDebug=false; 71 72 protected String properties_file_name = null; 73 77 protected Properties props=null; 78 79 89 public PropertyWrapper(String propertiesFileName, String envPropName, boolean bTryDefaultFileOnEnvFailure) 90 { 91 try 93 { 94 if(null!=envPropName) 95 { 96 SecurityManager sm = System.getSecurityManager(); 97 if(null!=sm) 98 sm.checkPropertyAccess(envPropName); 99 String envPropFile = System.getProperty(envPropName); 100 if(null!=envPropFile) 105 { 106 properties_file_name = envPropFile; 107 try 108 { 109 loadProperties(); 110 }catch(IOException ioe) 111 { 112 if(com.sun.enterprise.util.logging.Debug.enabled) 113 _logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe); 116 } 118 } 119 } 120 } catch(SecurityException se) 121 { 122 _logger.log(Level.SEVERE,"iplanet_util.security_exception",new Object []{properties_file_name,se}); 125 } 127 if( !bLoaded && bTryDefaultFileOnEnvFailure && (null!=propertiesFileName) ) 129 { 130 try 131 { 132 properties_file_name = propertiesFileName; 133 loadProperties(); 134 }catch(IOException ioe) 135 { 136 if(com.sun.enterprise.util.logging.Debug.enabled) 137 _logger.log(Level.SEVERE,"enterprise_util.dbgcntl_ioexception",ioe); 140 } 142 } 143 144 if(bLoaded && bDebug) 145 _logger.log(Level.FINE,"PropertyWrapper using " + properties_file_name); 148 else if (!bLoaded && bDebug) 150 _logger.log(Level.FINE,"PropertyWrapper reports properties could not be loaded for env=" + envPropName + " or filename=" + propertiesFileName); 153 } 155 156 165 public PropertyWrapper(String propertiesFileName, String envPropName) 166 { 167 this(propertiesFileName, envPropName, true); 168 } 169 170 174 public PropertyWrapper(String propertiesFileName) 175 { 176 this(propertiesFileName, null, true); 177 } 178 179 183 public PropertyWrapper(Properties props) 184 { 185 this.props=props; 186 } 187 188 192 public String getPropertiesFile() 193 { 194 return properties_file_name; 195 } 196 197 204 protected void loadProperties() 205 throws IOException 206 { 207 props = new Properties (); 208 209 try 210 { 211 InputStream is = ClassLoader.getSystemResourceAsStream(properties_file_name); 212 props.load(is); 213 if(!props.isEmpty()) 214 bLoaded = true; 215 }catch(IOException e) 216 { 217 throw e; 218 } 219 } 220 221 226 protected String getProperty(String key) 227 { 228 return getProperty(key, null); 229 } 230 231 239 protected String getProperty(String key, String defaultVal) 240 { 241 return props.getProperty(key, defaultVal); 242 } 243 244 248 protected int getIntProperty(String key) 249 { 250 String str = getProperty(key); 251 if(null==str) 252 return -1; 253 try 254 { 255 return Integer.parseInt(str); 256 }catch(NumberFormatException nfe) 257 { 258 return -1; 259 } 260 } 261 262 267 protected int getIntProperty(String key, int defaultVal) 268 { 269 String str = getProperty(key); 270 if(null==str) 271 return defaultVal; 272 try 273 { 274 return Integer.parseInt(str); 275 }catch(NumberFormatException nfe) 276 { 277 return -1; 278 } 279 } 280 281 285 protected boolean getBooleanProperty(String key, boolean defaultVal) 286 { 287 String str = getProperty(key); 288 if(null==str) 289 return defaultVal; 290 if(str.toLowerCase().startsWith("true")) 291 return true; 292 if(str.toLowerCase().startsWith("false")) 293 return false; 294 return defaultVal; 295 } 296 297 301 protected long getLongProperty(String key) 302 { 303 String str = getProperty(key); 304 if (str == null) 305 return -1; 306 try { 307 return Long.parseLong(str); 308 } catch(NumberFormatException nfe) { 309 return -1; 310 } 311 } 312 313 318 protected long getLongProperty(String key, long defaultVal) 319 { 320 String str = getProperty(key); 321 if (str == null) 322 return defaultVal; 323 try { 324 return Long.parseLong(str); 325 } catch(NumberFormatException nfe) { 326 return -1; 327 } 328 } 329 330 } 331 | Popular Tags |