1 package org.apache.ojb.broker.util.configuration.impl; 2 3 17 18 import org.apache.ojb.broker.metadata.MetadataException; 19 import org.apache.ojb.broker.util.configuration.Configuration; 20 import org.apache.ojb.broker.util.logging.Logger; 21 import org.apache.ojb.broker.util.logging.LoggerFactory; 22 import org.apache.ojb.broker.util.ClassHelper; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 import java.util.Properties ; 30 import java.util.StringTokenizer ; 31 32 40 public abstract class ConfigurationAbstractImpl implements Configuration 41 { 42 43 private Logger logger = LoggerFactory.getBootLogger(); 44 45 46 protected String filename; 47 48 49 protected Properties properties; 50 51 55 private String [] trueValues = {"true", "yes", "1"}; 56 60 private String [] falseValues = {"false", "no", "0"}; 61 62 65 public ConfigurationAbstractImpl() 66 { 67 load(); 68 } 69 70 78 public String getString(String key, String defaultValue) 79 { 80 String ret = properties.getProperty(key); 81 if (ret == null) 82 { 83 if(defaultValue == null) 84 { 85 logger.info("No value for key '" + key + "'"); 86 } 87 else 88 { 89 logger.debug("No value for key \"" + key + "\", using default \"" 90 + defaultValue + "\"."); 91 properties.put(key, defaultValue); 92 } 93 94 ret = defaultValue; 95 } 96 97 return ret; 98 } 99 100 112 public String [] getStrings(String key, String defaultValue, String seperators) 113 { 114 StringTokenizer st = new StringTokenizer (getString(key, defaultValue), seperators); 115 String [] ret = new String [st.countTokens()]; 116 for (int i = 0; i < ret.length; i++) 117 { 118 ret[i] = st.nextToken(); 119 } 120 return ret; 121 } 122 123 132 public String [] getStrings(String key, String defaultValue) 133 { 134 return getStrings(key, defaultValue, ";"); 135 } 136 137 146 public int getInteger(String key, int defaultValue) 147 { 148 int ret; 149 try 150 { 151 String tmp = properties.getProperty(key); 152 if (tmp == null) 153 { 154 properties.put(key, String.valueOf(defaultValue)); 155 logger.debug("No value for key \"" + key + "\", using default " 156 + defaultValue + "."); 157 return defaultValue; 158 } 159 ret = Integer.parseInt(tmp); 160 } 161 catch (NumberFormatException e) 162 { 163 Object wrongValue = properties.put(key, String.valueOf(defaultValue)); 164 logger.warn( 165 "Value \"" 166 + wrongValue 167 + "\" is illegal for key \"" 168 + key 169 + "\" (should be an integer, using default value " 170 + defaultValue 171 + ")"); 172 ret = defaultValue; 173 } 174 return ret; 175 } 176 177 public long getLong(String key, long defaultValue) 178 { 179 long ret; 180 try 181 { 182 String tmp = properties.getProperty(key); 183 if (tmp == null) 184 { 185 properties.put(key, String.valueOf(defaultValue)); 186 logger.debug("No value for key \"" + key + "\", using default " 187 + defaultValue + "."); 188 return defaultValue; 189 } 190 ret = Long.parseLong(tmp); 191 } 192 catch (NumberFormatException e) 193 { 194 Object wrongValue = properties.put(key, String.valueOf(defaultValue)); 195 logger.warn( 196 "Value \"" 197 + wrongValue 198 + "\" is illegal for key \"" 199 + key 200 + "\" (should be an integer, using default value " 201 + defaultValue 202 + ")"); 203 ret = defaultValue; 204 } 205 return ret; 206 } 207 208 public byte getByte(String key, byte defaultValue) 209 { 210 byte ret; 211 try 212 { 213 String tmp = properties.getProperty(key); 214 if (tmp == null) 215 { 216 properties.put(key, String.valueOf(defaultValue)); 217 logger.debug("No value for key \"" + key + "\", using default " 218 + defaultValue + "."); 219 return defaultValue; 220 } 221 ret = Byte.parseByte(tmp); 222 } 223 catch (NumberFormatException e) 224 { 225 Object wrongValue = properties.put(key, String.valueOf(defaultValue)); 226 logger.warn( 227 "Value \"" 228 + wrongValue 229 + "\" is illegal for key \"" 230 + key 231 + "\" (should be an integer, using default value " 232 + defaultValue 233 + ")"); 234 ret = defaultValue; 235 } 236 return ret; 237 } 238 239 251 public boolean getBoolean(String key, boolean defaultValue) 252 { 253 String tmp = properties.getProperty(key); 254 255 if (tmp == null) 256 { 257 logger.debug("No value for key \"" + key + "\", using default " 258 + defaultValue + "."); 259 properties.put(key, String.valueOf(defaultValue)); 260 return defaultValue; 261 } 262 263 for (int i = 0; i < trueValues.length; i++) 264 { 265 if (tmp.equalsIgnoreCase(trueValues[i])) 266 { 267 return true; 268 } 269 } 270 271 for (int i = 0; i < falseValues.length; i++) 272 { 273 if (tmp.equalsIgnoreCase(falseValues[i])) 274 { 275 return false; 276 } 277 } 278 279 logger.warn( 280 "Value \"" 281 + tmp 282 + "\" is illegal for key \"" 283 + key 284 + "\" (should be a boolean, using default value " 285 + defaultValue 286 + ")"); 287 return defaultValue; 288 } 289 290 302 public Class getClass(String key, Class defaultValue, Class [] assignables) 303 { 304 String className = properties.getProperty(key); 305 306 if (className == null) 307 { 308 if (defaultValue == null) 309 { 310 logger.info("No value for key '" + key + "'"); 311 return null; 312 } 313 else 314 { 315 className = defaultValue.getName(); 316 properties.put(key, className); 317 logger.debug("No value for key \"" + key + "\", using default " 318 + className + "."); 319 return defaultValue; 320 } 321 } 322 323 Class clazz = null; 324 try 325 { 326 clazz = ClassHelper.getClass(className); 327 } 328 catch (ClassNotFoundException e) 329 { 330 clazz = defaultValue; 331 logger.warn( 332 "Value \"" 333 + className 334 + "\" is illegal for key \"" 335 + key 336 + "\" (should be a class, using default value " 337 + defaultValue 338 + ")", e); 339 } 340 341 for (int i = 0; i < assignables.length; i++) 342 { 343 Class assignable = assignables[i]; 344 if (!assignable.isAssignableFrom(clazz)) 345 { 346 String extendsOrImplements; 347 if (assignable.isInterface()) 348 { 349 extendsOrImplements = "implement the interface "; 350 } 351 else 352 { 353 extendsOrImplements = "extend the class "; 354 355 } 356 logger.error( 357 "The specified class \"" 358 + className 359 + "\" does not " 360 + extendsOrImplements 361 + assignables[i].getName() 362 + ", which is a requirement for the key \"" 363 + key 364 + "\". Using default class " 365 + defaultValue); 366 clazz = defaultValue; 367 } 368 369 } 370 371 return clazz; 372 } 373 374 386 public Class getClass(String key, Class defaultValue, Class assignable) 387 { 388 return getClass(key, defaultValue, new Class []{assignable}); 389 } 390 391 400 public Class getClass(String key, Class defaultValue) 401 { 402 return getClass(key, defaultValue, new Class [0]); 403 } 404 405 413 protected void load() 414 { 415 properties = new Properties (); 416 417 String filename = getFilename(); 418 419 try 420 { 421 URL url = ClassHelper.getResource(filename); 422 423 if (url == null) 424 { 425 url = (new File (filename)).toURL(); 426 } 427 428 logger.info("Loading OJB's properties: " + url); 429 430 URLConnection conn = url.openConnection(); 431 conn.setUseCaches(false); 432 conn.connect(); 433 InputStream strIn = conn.getInputStream(); 434 properties.load(strIn); 435 strIn.close(); 436 } 437 catch (FileNotFoundException ex) 438 { 439 if ((filename == null) || (filename.length() == 0)) 443 { 444 logger.info("Starting OJB without a properties file. OJB is using default settings instead."); 445 } 446 else 447 { 448 logger.warn("Could not load properties file '"+filename+"'. Using default settings!", ex); 449 } 450 } 453 catch (Exception ex) 454 { 455 throw new MetadataException("An error happend while loading the properties file '"+filename+"'", ex); 456 } 457 } 458 459 private String getFilename() 460 { 461 if (filename == null) 462 { 463 filename = this.getClass().getName() + ".properties"; 464 } 465 return filename; 466 } 467 468 protected void setFilename(String name) 469 { 470 filename = name; 471 } 472 473 476 public void setLogger(Logger loggerInstance) 477 { 478 logger = loggerInstance; 479 } 480 481 } 482 | Popular Tags |