1 package org.jacorb.config; 2 3 22 23 import org.apache.avalon.framework.configuration.DefaultConfiguration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.*; 26 27 import java.io.*; 28 import java.util.*; 29 30 import org.jacorb.orb.ORB; 31 import org.jacorb.util.ObjectUtil; 32 33 65 66 public class Configuration 67 extends org.apache.avalon.framework.configuration.DefaultConfiguration 68 { 69 private static final String fileSuffix = ".properties"; 70 private static final String COMMON_PROPS = "orb" + fileSuffix; 71 72 private static final String TRUE = "true"; 73 private static final String ON = "on"; 74 private static final String EMPTY_STR = ""; 75 76 private static final int DEFAULT_LOG_LEVEL = 0; 77 78 private Configuration config; 79 private String configName; 80 private ORB orb = null; 81 82 83 private Logger logger = null; 84 85 86 private LoggerFactory loggerFactory = null; 87 88 89 private final String loggerFactoryClzName = 90 "org.jacorb.config.LogKitLoggerFactory"; 91 92 93 97 98 106 109 110 public static Configuration getConfiguration(Properties props, 111 ORB orb, 112 boolean isApplet) 113 throws ConfigurationException 114 { 115 String orbID = "jacorb"; String myOrbID = isApplet ? null : System.getProperty("ORBid"); 119 120 if( props != null ) 121 { 122 String tmp = (String )props.get("ORBid"); 124 if( tmp != null ) 125 myOrbID = tmp; 126 } 127 128 if (myOrbID != null ) 129 { 130 if (myOrbID.equals("orb") || myOrbID.equals("jacorb")) 132 throw new ConfigurationException("Illegal orbID, <" + 133 myOrbID + "> is reserved"); 134 else 135 orbID = myOrbID; 136 } 137 138 return new Configuration(orbID, props, orb, isApplet); 139 } 140 141 142 146 147 private Configuration(String name, 148 Properties orbProperties, 149 ORB orb, 150 boolean isApplet) 151 throws ConfigurationException 152 { 153 super(name); 154 this.orb = orb; 155 156 if (isApplet) 157 { 158 initApplet(name, orbProperties); 159 } 160 else 161 { 162 init(name, orbProperties); 163 } 164 165 initLogging(); 166 } 167 168 183 184 private void init(String name, Properties orbProperties) 185 throws ConfigurationException 186 { 187 if( name == null ) 188 throw new ConfigurationException("Illegal null value for ORB name!"); 189 String separator = System.getProperty("file.separator"); 190 String home = System.getProperty("user.home"); 191 String lib = System.getProperty("java.home"); 192 boolean loaded = false; 193 194 setAttributes(System.getProperties()); 197 198 int logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 199 200 Properties commonProps = 203 loadPropertiesFromFile( lib + separator + "lib" + separator + COMMON_PROPS); 204 205 if (commonProps!= null) 206 { 207 setAttributes(commonProps); 208 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 211 loaded = true; 212 213 if (logLevel > 2) 214 System.out.println("[ base configuration loaded from file " + 215 lib + separator + "lib" + separator + COMMON_PROPS + " ]"); 216 } 217 218 commonProps = 220 loadPropertiesFromFile( home + separator + COMMON_PROPS ); 221 222 if (commonProps!= null) 223 { 224 setAttributes(commonProps); 225 loaded = true; 226 227 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 228 if (logLevel > 2) 229 System.out.println("[ base configuration loaded from file " + 230 home + separator + COMMON_PROPS + " ]"); 231 } 232 233 commonProps = 235 loadPropertiesFromClassPath( COMMON_PROPS ); 236 237 if (commonProps!= null) 238 { 239 loaded = true; 240 setAttributes(commonProps); 241 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 242 if (logLevel > 2) 243 System.out.println("[ base configuration loaded from classpath " + 244 COMMON_PROPS + " ]"); 245 } 246 247 248 String configDir = 250 getAttribute("jacorb.config.dir", ""); 251 252 if (configDir.length() == 0) 253 configDir = getAttribute ("jacorb.home", ""); 254 255 if (configDir.length() != 0 ) 256 configDir += separator + "etc"; 257 else 258 { 259 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 260 if (logLevel > 0) 261 System.err.println("[ jacorb.home unset! Will use '.' ]"); 262 configDir = "."; 263 } 264 265 String propFileName = configDir + separator + name + fileSuffix; 266 267 Properties orbConfig = loadPropertiesFromFile(propFileName ); 269 270 if (orbConfig!= null) 271 { 272 setAttributes(orbConfig); 273 loaded = true; 274 275 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 276 if (logLevel > 2) 277 System.out.println("[ configuration " + name + 278 " loaded from file " + propFileName + " ]"); 279 } 280 else 281 { 282 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 283 if (logLevel > 0) 284 System.err.println("[ File " + propFileName + " for configuration " + name + 285 " not found ]"); 286 } 287 288 List customPropFileNames = getAttributeList("custom.props"); 290 291 if (!customPropFileNames.isEmpty()) 292 { 293 for (Iterator iter = customPropFileNames.iterator(); iter.hasNext();) 294 { 295 String fileName = ((String )iter.next()); 296 Properties customProps = loadPropertiesFromFile(fileName); 297 if (customProps!= null) 298 { 299 setAttributes(customProps); 300 loaded = true; 301 302 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 303 if (logLevel > 2) 304 System.out.println("[ custom properties loaded from file " + 305 fileName + " ]"); 306 } 307 else 308 { 309 if (logLevel > 0) 310 System.err.println("[ custom properties not found in " + 311 fileName + " ]"); 312 } 313 } 314 } 315 316 orbConfig = loadPropertiesFromClassPath( name + fileSuffix ); 318 if (orbConfig!= null) 319 { 320 setAttributes(orbConfig); 321 loaded = true; 322 323 logLevel = 324 getAttributeAsInteger("jacorb.config.log.verbosity",DEFAULT_LOG_LEVEL); 325 if (logLevel > 2) 326 System.out.println("[ configuration " + name + " loaded from classpath]"); 327 } 328 329 setAttributes( System.getProperties() ); 332 333 if (orbProperties != null) 336 { 337 loaded = true; 338 setAttributes(orbProperties); 339 } 340 341 if (!loaded) 342 { 343 System.out.println("[ No configuration properties found for configuration " + name + " ]"); 345 } 346 347 } 348 349 361 362 private void initApplet(String name, Properties orbProperties) 363 throws ConfigurationException 364 { 365 if( name == null ) 366 throw new ConfigurationException("Illegal null value for ORB name!"); 367 boolean loaded = false; 368 369 setAttributes(orbProperties); 372 373 int logLevel = getAttributeAsInteger("jacorb.config.log.verbosity", 374 DEFAULT_LOG_LEVEL); 375 376 Properties commonProps = 379 loadPropertiesFromClassPath( COMMON_PROPS ); 380 381 if (commonProps!= null) 382 { 383 loaded = true; 384 setAttributes(commonProps); 385 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity", 386 DEFAULT_LOG_LEVEL); 387 if (logLevel > 2) 388 System.out.println("[ base configuration loaded from classpath " + 389 COMMON_PROPS + " ]"); 390 } 391 392 393 String propFileName = name + fileSuffix; 395 Properties orbConfig = loadPropertiesFromClassPath(propFileName ); 396 397 if (orbConfig!= null) 398 { 399 setAttributes(orbConfig); 400 loaded = true; 401 402 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity", 403 DEFAULT_LOG_LEVEL); 404 if (logLevel > 2) 405 System.out.println("[ configuration " + name + 406 " loaded from classpath " + propFileName + 407 " ]"); 408 } 409 else 410 { 411 logLevel = getAttributeAsInteger("jacorb.config.log.verbosity", 412 DEFAULT_LOG_LEVEL); 413 if (logLevel > 0) 414 System.err.println("[ File " + propFileName + 415 " for configuration " + name + 416 " not found in classpath]"); 417 } 418 419 List customPropFileNames = getAttributeList("custom.props"); 421 422 if (!customPropFileNames.isEmpty()) 423 { 424 for (Iterator iter = customPropFileNames.iterator(); iter.hasNext();) 425 { 426 String fileName = ((String )iter.next()); 427 Properties customProps = loadPropertiesFromClassPath(fileName); 428 if (customProps!= null) 429 { 430 setAttributes(customProps); 431 loaded = true; 432 433 logLevel = 434 getAttributeAsInteger("jacorb.config.log.verbosity", 435 DEFAULT_LOG_LEVEL); 436 if (logLevel > 2) 437 System.out.println( 438 "[ custom properties loaded from classpath " + 439 fileName + " ]"); 440 } 441 else 442 { 443 if (logLevel > 0) 444 System.err.println( 445 "[ custom properties " + fileName + 446 "not found in classpath ]"); 447 } 448 } 449 } 450 451 if (orbProperties != null) 454 { 455 loaded = true; 456 setAttributes(orbProperties); 457 } 458 459 if (!loaded) 460 { 461 System.out.println( 463 "[ No configuration properties found for configuration " + 464 name + " ]"); 465 } 466 } 467 468 469 472 473 void setAttributes(Properties properties) 474 { 475 for (Iterator iter=properties.keySet().iterator(); iter.hasNext();) 476 { 477 Object k = iter.next(); 478 if (!(k instanceof String )) continue; 481 String key = (String )k; 482 Object value = properties.get(key); 483 if (value instanceof String || value == null) 484 { 485 setAttribute(key, (String )value); 486 } 487 } 488 } 489 490 491 496 497 private static Properties loadPropertiesFromFile(String fileName) 498 { 499 try 500 { 501 BufferedInputStream bin = 502 new BufferedInputStream( new FileInputStream(fileName)); 503 Properties result = new Properties(); 504 result.load(bin); 505 return result; 506 } 507 catch( java.io.IOException io ) 508 { 509 return null; 511 } 512 } 513 514 515 520 521 private static Properties loadPropertiesFromClassPath(String name) 522 { 523 Properties result = null; 524 try 525 { 526 java.net.URL url = 527 Thread.currentThread().getContextClassLoader().getResource(name); 528 if (url!=null) 529 { 530 result = new Properties(); 531 result.load( url.openStream() ); 532 } 533 } 534 catch (java.io.IOException ioe) 535 { 536 ioe.printStackTrace(); } 538 return result; 539 } 540 541 542 550 551 private void initLogging() 552 throws ConfigurationException 553 { 554 String logFileName = 555 getAttribute("jacorb.logfile", ""); 556 557 int maxLogSize = 558 getAttributeAsInteger( "jacorb.logfile.maxLogSize", 0 ); 559 560 if ( !logFileName.equals("")) 561 { 562 if (logFileName.endsWith("$implname")) 564 { 565 logFileName = logFileName.substring (0, logFileName.length () - 9); 566 567 if ( !getAttribute("jacorb.implname","").equals("")) 568 { 569 logFileName += getAttribute("jacorb.implname",""); 570 } 571 else 572 { 573 logFileName += "log"; 575 } 576 } 577 } 578 579 String clzName = getAttribute("jacorb.log.loggerFactory",""); 580 Class loggerFactoryClz = null; 581 582 try 583 { 584 if ( !clzName.equals("")) 585 { 586 loggerFactoryClz = org.jacorb.util.ObjectUtil.classForName(clzName); 587 } 588 else 589 { 590 loggerFactoryClz = org.jacorb.util.ObjectUtil.classForName(loggerFactoryClzName); 591 } 592 loggerFactory = (LoggerFactory)loggerFactoryClz.newInstance(); 593 loggerFactory.configure( this ); 594 } 595 catch (Exception e) 596 { 597 e.printStackTrace(); 598 } 599 600 if (loggerFactory == null) 601 { 602 System.err.println("Configuration Error, could not create logger!"); 603 } 604 605 if (!logFileName.equals("")) 606 { 607 try 608 { 609 loggerFactory.setDefaultLogFile(logFileName, maxLogSize); 610 logger = 612 loggerFactory.getNamedLogger("jacorb",logFileName, maxLogSize); 613 } 614 catch (IOException e) 615 { 616 logger = loggerFactory.getNamedRootLogger("jacorb"); 617 if( logger.isErrorEnabled()) 618 { 619 logger.error("Could not create logger with file target: " + logFileName + 620 ", falling back to console log!"); 621 } 622 } 623 } 624 else 625 { 626 logger = loggerFactory.getNamedRootLogger("jacorb" ); 627 } 628 } 629 630 633 634 public ORB getORB() 635 { 636 return orb; 637 } 638 639 640 645 646 public Logger getNamedLogger(String name) 647 { 648 return loggerFactory.getNamedLogger(name); 649 } 650 651 public static final String getLoggerName(Class clz) 652 { 653 String packageName = clz.getPackage().getName(); 654 if (packageName != null && packageName.startsWith("org.jacorb")) 655 { 656 return packageName.substring(4); 657 } 658 return packageName; 659 } 660 661 666 667 public List getAttributeList(String key) 668 { 669 List result = new ArrayList(); 670 String value = null; 671 672 try 673 { 674 value = getAttribute(key); 675 } 676 catch( ConfigurationException ce) 677 { 678 } 680 681 if (value != null) 682 { 683 StringTokenizer tok = new StringTokenizer(value, ","); 684 while (tok.hasMoreTokens()) 685 result.add(tok.nextToken().trim()); 686 } 687 return result; 688 } 689 690 698 699 public Object getAttributeAsObject( String key ) 700 throws ConfigurationException 701 { 702 String className = null; 703 try 704 { 705 className = getAttribute( key ); 706 } 707 catch( Exception e ) 708 { 709 } 711 712 if( className != null && className.length() > 0 ) 713 { 714 try 715 { 716 Class c = ObjectUtil.classForName(className); 717 return c.newInstance(); 718 } 719 catch( Exception e ) 720 { 721 throw new ConfigurationException( "Unable to build class from key >" + 722 key +"<: " + e ); 723 } 724 } 725 else 726 { 727 return null; 728 } 729 } 730 731 public boolean getAttributeAsBoolean(String key) 732 throws ConfigurationException 733 { 734 String s = getAttribute(key); 735 736 if (s != null && s.length() > 0) 737 { 738 s = s.trim().toLowerCase(); 739 return ON.equals(s) || TRUE.equals(s); 740 } 741 else 742 { 743 return false; 744 } 745 } 746 747 public boolean getAttributeAsBoolean(String key, boolean defaultValue) 748 { 749 String s = getAttribute(key, EMPTY_STR); 750 751 if (s.length() > 0) 752 { 753 s = s.trim().toLowerCase(); 754 return ON.equals(s) || TRUE.equals(s); 755 } 756 else 757 { 758 return defaultValue; 759 } 760 } 761 } 762 | Popular Tags |