GNU Classpath (0.92) | |
Frames | No Frames |
1: /* LogManager.java -- a class for maintaining Loggers and managing 2: configuration properties 3: Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath. 6: 7: GNU Classpath is free software; you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation; either version 2, or (at your option) 10: any later version. 11: 12: GNU Classpath is distributed in the hope that it will be useful, but 13: WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15: General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with GNU Classpath; see the file COPYING. If not, write to the 19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20: 02110-1301 USA. 21: 22: Linking this library statically or dynamically with other modules is 23: making a combined work based on this library. Thus, the terms and 24: conditions of the GNU General Public License cover the whole 25: combination. 26: 27: As a special exception, the copyright holders of this library give you 28: permission to link this library with independent modules to produce an 29: executable, regardless of the license terms of these independent 30: modules, and to copy and distribute the resulting executable under 31: terms of your choice, provided that you also meet, for each linked 32: independent module, the terms and conditions of the license of that 33: module. An independent module is a module which is not derived from 34: or based on this library. If you modify this library, you may extend 35: this exception to your version of the library, but you are not 36: obligated to do so. If you do not wish to do so, delete this 37: exception statement from your version. */ 38: 39: 40: package java.util.logging; 41: 42: import gnu.classpath.SystemProperties; 43: 44: import java.beans.PropertyChangeListener; 45: import java.beans.PropertyChangeSupport; 46: import java.io.ByteArrayInputStream; 47: import java.io.IOException; 48: import java.io.InputStream; 49: import java.lang.ref.WeakReference; 50: import java.net.URL; 51: import java.util.Collections; 52: import java.util.Enumeration; 53: import java.util.HashMap; 54: import java.util.Iterator; 55: import java.util.List; 56: import java.util.Map; 57: import java.util.Properties; 58: import java.util.StringTokenizer; 59: 60: /** 61: * The <code>LogManager</code> maintains a hierarchical namespace 62: * of Logger objects and manages properties for configuring the logging 63: * framework. There exists only one single <code>LogManager</code> 64: * per virtual machine. This instance can be retrieved using the 65: * static method {@link #getLogManager()}. 66: * 67: * <p><strong>Configuration Process:</strong> The global LogManager 68: * object is created and configured when the class 69: * <code>java.util.logging.LogManager</code> is initialized. 70: * The configuration process includes the subsequent steps: 71: * 72: * <ul> 73: * <li>If the system property <code>java.util.logging.manager</code> 74: * is set to the name of a subclass of 75: * <code>java.util.logging.LogManager</code>, an instance of 76: * that subclass is created and becomes the global LogManager. 77: * Otherwise, a new instance of LogManager is created.</li> 78: * <li>The <code>LogManager</code> constructor tries to create 79: * a new instance of the class specified by the system 80: * property <code>java.util.logging.config.class</code>. 81: * Typically, the constructor of this class will call 82: * <code>LogManager.getLogManager().readConfiguration(java.io.InputStream)</code> 83: * for configuring the logging framework. 84: * The configuration process stops at this point if 85: * the system property <code>java.util.logging.config.class</code> 86: * is set (irrespective of whether the class constructor 87: * could be called or an exception was thrown).</li> 88: * 89: * <li>If the system property <code>java.util.logging.config.class</code> 90: * is <em>not</em> set, the configuration parameters are read in from 91: * a file and passed to 92: * {@link #readConfiguration(java.io.InputStream)}. 93: * The name and location of this file are specified by the system 94: * property <code>java.util.logging.config.file</code>.</li> 95: * <li>If the system property <code>java.util.logging.config.file</code> 96: * is not set, however, the contents of the URL 97: * "{gnu.classpath.home.url}/logging.properties" are passed to 98: * {@link #readConfiguration(java.io.InputStream)}. 99: * Here, "{gnu.classpath.home.url}" stands for the value of 100: * the system property <code>gnu.classpath.home.url</code>.</li> 101: * </ul> 102: * 103: * <p>The <code>LogManager</code> has a level of <code>INFO</code> by 104: * default, and this will be inherited by <code>Logger</code>s unless they 105: * override it either by properties or programmatically. 106: * 107: * @author Sascha Brawer (brawer@acm.org) 108: */ 109: public class LogManager 110: { 111: /** 112: * The object name for the logging management bean. 113: * @since 1.5 114: */ 115: public static final String LOGGING_MXBEAN_NAME 116: = "java.util.logging:type=Logging"; 117: 118: /** 119: * The singleton LogManager instance. 120: */ 121: private static LogManager logManager; 122: 123: /** 124: * The singleton logging bean. 125: */ 126: private static LoggingMXBean loggingBean; 127: 128: /** 129: * The registered named loggers; maps the name of a Logger to 130: * a WeakReference to it. 131: */ 132: private Map loggers; 133: 134: /** 135: * The properties for the logging framework which have been 136: * read in last. 137: */ 138: private Properties properties; 139: 140: /** 141: * A delegate object that provides support for handling 142: * PropertyChangeEvents. The API specification does not 143: * mention which bean should be the source in the distributed 144: * PropertyChangeEvents, but Mauve test code has determined that 145: * the Sun J2SE 1.4 reference implementation uses the LogManager 146: * class object. This is somewhat strange, as the class object 147: * is not the bean with which listeners have to register, but 148: * there is no reason for the GNU Classpath implementation to 149: * behave differently from the reference implementation in 150: * this case. 151: */ 152: private final PropertyChangeSupport pcs = new PropertyChangeSupport( /* source bean */ 153: LogManager.class); 154: 155: protected LogManager() 156: { 157: loggers = new HashMap(); 158: } 159: 160: /** 161: * Returns the globally shared LogManager instance. 162: */ 163: public static synchronized LogManager getLogManager() 164: { 165: if (logManager == null) 166: { 167: logManager = makeLogManager(); 168: initLogManager(); 169: } 170: return logManager; 171: } 172: 173: private static final String MANAGER_PROPERTY = "java.util.logging.manager"; 174: 175: private static LogManager makeLogManager() 176: { 177: String managerClassName = SystemProperties.getProperty(MANAGER_PROPERTY); 178: LogManager manager = (LogManager) createInstance 179: (managerClassName, LogManager.class, MANAGER_PROPERTY); 180: if (manager == null) 181: manager = new LogManager(); 182: return manager; 183: } 184: 185: private static final String CONFIG_PROPERTY = "java.util.logging.config.class"; 186: 187: private static void initLogManager() 188: { 189: LogManager manager = getLogManager(); 190: Logger.root.setLevel(Level.INFO); 191: manager.addLogger(Logger.root); 192: 193: /* The Javadoc description of the class explains 194: * what is going on here. 195: */ 196: Object configurator = createInstance(System.getProperty(CONFIG_PROPERTY), 197: /* must be instance of */ Object.class, 198: CONFIG_PROPERTY); 199: 200: try 201: { 202: if (configurator == null) 203: manager.readConfiguration(); 204: } 205: catch (IOException ex) 206: { 207: /* FIXME: Is it ok to ignore exceptions here? */ 208: } 209: } 210: 211: /** 212: * Registers a listener which will be notified when the 213: * logging properties are re-read. 214: */ 215: public synchronized void addPropertyChangeListener(PropertyChangeListener listener) 216: { 217: /* do not register null. */ 218: listener.getClass(); 219: 220: pcs.addPropertyChangeListener(listener); 221: } 222: 223: /** 224: * Unregisters a listener. 225: * 226: * If <code>listener</code> has not been registered previously, 227: * nothing happens. Also, no exception is thrown if 228: * <code>listener</code> is <code>null</code>. 229: */ 230: public synchronized void removePropertyChangeListener(PropertyChangeListener listener) 231: { 232: if (listener != null) 233: pcs.removePropertyChangeListener(listener); 234: } 235: 236: /** 237: * Adds a named logger. If a logger with the same name has 238: * already been registered, the method returns <code>false</code> 239: * without adding the logger. 240: * 241: * <p>The <code>LogManager</code> only keeps weak references 242: * to registered loggers. Therefore, names can become available 243: * after automatic garbage collection. 244: * 245: * @param logger the logger to be added. 246: * 247: * @return <code>true</code>if <code>logger</code> was added, 248: * <code>false</code> otherwise. 249: * 250: * @throws NullPointerException if <code>name</code> is 251: * <code>null</code>. 252: */ 253: public synchronized boolean addLogger(Logger logger) 254: { 255: /* To developers thinking about to remove the 'synchronized' 256: * declaration from this method: Please read the comment 257: * in java.util.logging.Logger.getLogger(String, String) 258: * and make sure that whatever you change wrt. synchronization 259: * does not endanger thread-safety of Logger.getLogger. 260: * The current implementation of Logger.getLogger assumes 261: * that LogManager does its synchronization on the globally 262: * shared instance of LogManager. 263: */ 264: String name; 265: WeakReference ref; 266: 267: /* This will throw a NullPointerException if logger is null, 268: * as required by the API specification. 269: */ 270: name = logger.getName(); 271: 272: ref = (WeakReference) loggers.get(name); 273: if (ref != null) 274: { 275: if (ref.get() != null) 276: return false; 277: 278: /* There has been a logger under this name in the past, 279: * but it has been garbage collected. 280: */ 281: loggers.remove(ref); 282: } 283: 284: /* Adding a named logger requires a security permission. */ 285: if ((name != null) && ! name.equals("")) 286: checkAccess(); 287: 288: Logger parent = findAncestor(logger); 289: loggers.put(name, new WeakReference(logger)); 290: if (parent != logger.getParent()) 291: logger.setParent(parent); 292: 293: // The level of the newly added logger must be specified. 294: // The easiest case is if there is a level for exactly this logger 295: // in the properties. If no such level exists the level needs to be 296: // searched along the hirachy. So if there is a new logger 'foo.blah.blub' 297: // and an existing parent logger 'foo' the properties 'foo.blah.blub.level' 298: // and 'foo.blah.level' need to be checked. If both do not exist in the 299: // properties the level of the new logger is set to 'null' (i.e. it uses the 300: // level of its parent 'foo'). 301: Level logLevel = logger.getLevel(); 302: String searchName = name; 303: String parentName = parent != null ? parent.getName() : ""; 304: while (logLevel == null && ! searchName.equals(parentName)) 305: { 306: logLevel = getLevelProperty(searchName + ".level", logLevel); 307: int index = searchName.lastIndexOf('.'); 308: if(index > -1) 309: searchName = searchName.substring(0,index); 310: else 311: searchName = ""; 312: } 313: logger.setLevel(logLevel); 314: 315: /* It can happen that existing loggers should be children of 316: * the newly added logger. For example, assume that there 317: * already exist loggers under the names "", "foo", and "foo.bar.baz". 318: * When adding "foo.bar", the logger "foo.bar.baz" should change 319: * its parent to "foo.bar". 320: */ 321: if (parent != Logger.root) 322: { 323: for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();) 324: { 325: Logger possChild = (Logger) ((WeakReference) loggers.get(iter.next())) 326: .get(); 327: if ((possChild == null) || (possChild == logger) 328: || (possChild.getParent() != parent)) 329: continue; 330: 331: if (! possChild.getName().startsWith(name)) 332: continue; 333: 334: if (possChild.getName().charAt(name.length()) != '.') 335: continue; 336: 337: possChild.setParent(logger); 338: } 339: } 340: 341: return true; 342: } 343: 344: /** 345: * Finds the closest ancestor for a logger among the currently 346: * registered ones. For example, if the currently registered 347: * loggers have the names "", "foo", and "foo.bar", the result for 348: * "foo.bar.baz" will be the logger whose name is "foo.bar". 349: * 350: * @param child a logger for whose name no logger has been 351: * registered. 352: * 353: * @return the closest ancestor for <code>child</code>, 354: * or <code>null</code> if <code>child</code> 355: * is the root logger. 356: * 357: * @throws NullPointerException if <code>child</code> 358: * is <code>null</code>. 359: */ 360: private synchronized Logger findAncestor(Logger child) 361: { 362: String childName = child.getName(); 363: int childNameLength = childName.length(); 364: Logger best = Logger.root; 365: int bestNameLength = 0; 366: 367: Logger cand; 368: String candName; 369: int candNameLength; 370: 371: if (child == Logger.root) 372: return null; 373: 374: for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();) 375: { 376: candName = (String) iter.next(); 377: candNameLength = candName.length(); 378: 379: if (candNameLength > bestNameLength 380: && childNameLength > candNameLength 381: && childName.startsWith(candName) 382: && childName.charAt(candNameLength) == '.') 383: { 384: cand = (Logger) ((WeakReference) loggers.get(candName)).get(); 385: if ((cand == null) || (cand == child)) 386: continue; 387: 388: bestNameLength = candName.length(); 389: best = cand; 390: } 391: } 392: 393: return best; 394: } 395: 396: /** 397: * Returns a Logger given its name. 398: * 399: * @param name the name of the logger. 400: * 401: * @return a named Logger, or <code>null</code> if there is no 402: * logger with that name. 403: * 404: * @throw java.lang.NullPointerException if <code>name</code> 405: * is <code>null</code>. 406: */ 407: public synchronized Logger getLogger(String name) 408: { 409: WeakReference ref; 410: 411: /* Throw a NullPointerException if name is null. */ 412: name.getClass(); 413: 414: ref = (WeakReference) loggers.get(name); 415: if (ref != null) 416: return (Logger) ref.get(); 417: else 418: return null; 419: } 420: 421: /** 422: * Returns an Enumeration of currently registered Logger names. 423: * Since other threads can register loggers at any time, the 424: * result could be different any time this method is called. 425: * 426: * @return an Enumeration with the names of the currently 427: * registered Loggers. 428: */ 429: public synchronized Enumeration getLoggerNames() 430: { 431: return Collections.enumeration(loggers.keySet()); 432: } 433: 434: /** 435: * Resets the logging configuration by removing all handlers for 436: * registered named loggers and setting their level to <code>null</code>. 437: * The level of the root logger will be set to <code>Level.INFO</code>. 438: * 439: * @throws SecurityException if a security manager exists and 440: * the caller is not granted the permission to control 441: * the logging infrastructure. 442: */ 443: public synchronized void reset() throws SecurityException 444: { 445: /* Throw a SecurityException if the caller does not have the 446: * permission to control the logging infrastructure. 447: */ 448: checkAccess(); 449: 450: properties = new Properties(); 451: 452: Iterator iter = loggers.values().iterator(); 453: while (iter.hasNext()) 454: { 455: WeakReference ref; 456: Logger logger; 457: 458: ref = (WeakReference) iter.next(); 459: if (ref != null) 460: { 461: logger = (Logger) ref.get(); 462: 463: if (logger == null) 464: iter.remove(); 465: else if (logger != Logger.root) 466: { 467: logger.resetLogger(); 468: logger.setLevel(null); 469: } 470: } 471: } 472: 473: Logger.root.setLevel(Level.INFO); 474: Logger.root.resetLogger(); 475: } 476: 477: /** 478: * Configures the logging framework by reading a configuration file. 479: * The name and location of this file are specified by the system 480: * property <code>java.util.logging.config.file</code>. If this 481: * property is not set, the URL 482: * "{gnu.classpath.home.url}/logging.properties" is taken, where 483: * "{gnu.classpath.home.url}" stands for the value of the system 484: * property <code>gnu.classpath.home.url</code>. 485: * 486: * <p>The task of configuring the framework is then delegated to 487: * {@link #readConfiguration(java.io.InputStream)}, which will 488: * notify registered listeners after having read the properties. 489: * 490: * @throws SecurityException if a security manager exists and 491: * the caller is not granted the permission to control 492: * the logging infrastructure, or if the caller is 493: * not granted the permission to read the configuration 494: * file. 495: * 496: * @throws IOException if there is a problem reading in the 497: * configuration file. 498: */ 499: public synchronized void readConfiguration() 500: throws IOException, SecurityException 501: { 502: String path; 503: InputStream inputStream; 504: 505: path = System.getProperty("java.util.logging.config.file"); 506: if ((path == null) || (path.length() == 0)) 507: { 508: String url = (System.getProperty("gnu.classpath.home.url") 509: + "/logging.properties"); 510: try 511: { 512: inputStream = new URL(url).openStream(); 513: } 514: catch (Exception e) 515: { 516: inputStream=null; 517: } 518: 519: // If no config file could be found use a default configuration. 520: if(inputStream == null) 521: { 522: String defaultConfig = "handlers = java.util.logging.ConsoleHandler \n" 523: + ".level=INFO \n"; 524: inputStream = new ByteArrayInputStream(defaultConfig.getBytes()); 525: } 526: } 527: else 528: inputStream = new java.io.FileInputStream(path); 529: 530: try 531: { 532: readConfiguration(inputStream); 533: } 534: finally 535: { 536: // Close the stream in order to save 537: // resources such as file descriptors. 538: inputStream.close(); 539: } 540: } 541: 542: public synchronized void readConfiguration(InputStream inputStream) 543: throws IOException, SecurityException 544: { 545: Properties newProperties; 546: Enumeration keys; 547: 548: checkAccess(); 549: newProperties = new Properties(); 550: newProperties.load(inputStream); 551: reset(); 552: this.properties = newProperties; 553: keys = newProperties.propertyNames(); 554: 555: while (keys.hasMoreElements()) 556: { 557: String key = ((String) keys.nextElement()).trim(); 558: String value = newProperties.getProperty(key); 559: 560: if (value == null) 561: continue; 562: 563: value = value.trim(); 564: 565: if ("handlers".equals(key)) 566: { 567: StringTokenizer tokenizer = new StringTokenizer(value); 568: while (tokenizer.hasMoreTokens()) 569: { 570: String handlerName = tokenizer.nextToken(); 571: Handler handler = (Handler) 572: createInstance(handlerName, Handler.class, key); 573: Logger.root.addHandler(handler); 574: } 575: } 576: 577: if (key.endsWith(".level")) 578: { 579: String loggerName = key.substring(0, key.length() - 6); 580: Logger logger = getLogger(loggerName); 581: 582: if (logger == null) 583: { 584: logger = Logger.getLogger(loggerName); 585: addLogger(logger); 586: } 587: Level level = null; 588: try 589: { 590: level = Level.parse(value); 591: } 592: catch (IllegalArgumentException e) 593: { 594: warn("bad level \'" + value + "\'", e); 595: } 596: if (level != null) 597: { 598: logger.setLevel(level); 599: } 600: continue; 601: } 602: } 603: 604: /* The API specification does not talk about the 605: * property name that is distributed with the 606: * PropertyChangeEvent. With test code, it could 607: * be determined that the Sun J2SE 1.4 reference 608: * implementation uses null for the property name. 609: */ 610: pcs.firePropertyChange(null, null, null); 611: } 612: 613: /** 614: * Returns the value of a configuration property as a String. 615: */ 616: public synchronized String getProperty(String name) 617: { 618: if (properties != null) 619: return properties.getProperty(name); 620: else 621: return null; 622: } 623: 624: /** 625: * Returns the value of a configuration property as an integer. 626: * This function is a helper used by the Classpath implementation 627: * of java.util.logging, it is <em>not</em> specified in the 628: * logging API. 629: * 630: * @param name the name of the configuration property. 631: * 632: * @param defaultValue the value that will be returned if the 633: * property is not defined, or if its value is not an integer 634: * number. 635: */ 636: static int getIntProperty(String name, int defaultValue) 637: { 638: try 639: { 640: return Integer.parseInt(getLogManager().getProperty(name)); 641: } 642: catch (Exception ex) 643: { 644: return defaultValue; 645: } 646: } 647: 648: /** 649: * Returns the value of a configuration property as an integer, 650: * provided it is inside the acceptable range. 651: * This function is a helper used by the Classpath implementation 652: * of java.util.logging, it is <em>not</em> specified in the 653: * logging API. 654: * 655: * @param name the name of the configuration property. 656: * 657: * @param minValue the lowest acceptable value. 658: * 659: * @param maxValue the highest acceptable value. 660: * 661: * @param defaultValue the value that will be returned if the 662: * property is not defined, or if its value is not an integer 663: * number, or if it is less than the minimum value, 664: * or if it is greater than the maximum value. 665: */ 666: static int getIntPropertyClamped(String name, int defaultValue, 667: int minValue, int maxValue) 668: { 669: int val = getIntProperty(name, defaultValue); 670: if ((val < minValue) || (val > maxValue)) 671: val = defaultValue; 672: return val; 673: } 674: 675: /** 676: * Returns the value of a configuration property as a boolean. 677: * This function is a helper used by the Classpath implementation 678: * of java.util.logging, it is <em>not</em> specified in the 679: * logging API. 680: * 681: * @param name the name of the configuration property. 682: * 683: * @param defaultValue the value that will be returned if the 684: * property is not defined, or if its value is neither 685: * <code>"true"</code> nor <code>"false"</code>. 686: */ 687: static boolean getBooleanProperty(String name, boolean defaultValue) 688: { 689: try 690: { 691: return (Boolean.valueOf(getLogManager().getProperty(name))).booleanValue(); 692: } 693: catch (Exception ex) 694: { 695: return defaultValue; 696: } 697: } 698: 699: /** 700: * Returns the value of a configuration property as a Level. 701: * This function is a helper used by the Classpath implementation 702: * of java.util.logging, it is <em>not</em> specified in the 703: * logging API. 704: * 705: * @param propertyName the name of the configuration property. 706: * 707: * @param defaultValue the value that will be returned if the 708: * property is not defined, or if 709: * {@link Level#parse(java.lang.String)} does not like 710: * the property value. 711: */ 712: static Level getLevelProperty(String propertyName, Level defaultValue) 713: { 714: try 715: { 716: return Level.parse(getLogManager().getProperty(propertyName)); 717: } 718: catch (Exception ex) 719: { 720: return defaultValue; 721: } 722: } 723: 724: /** 725: * Returns the value of a configuration property as a Class. 726: * This function is a helper used by the Classpath implementation 727: * of java.util.logging, it is <em>not</em> specified in the 728: * logging API. 729: * 730: * @param propertyName the name of the configuration property. 731: * 732: * @param defaultValue the value that will be returned if the 733: * property is not defined, or if it does not specify 734: * the name of a loadable class. 735: */ 736: static final Class getClassProperty(String propertyName, Class defaultValue) 737: { 738: String propertyValue = logManager.getProperty(propertyName); 739: 740: if (propertyValue != null) 741: try 742: { 743: return locateClass(propertyValue); 744: } 745: catch (ClassNotFoundException e) 746: { 747: warn(propertyName + " = " + propertyValue, e); 748: } 749: 750: return defaultValue; 751: } 752: 753: static final Object getInstanceProperty(String propertyName, Class ofClass, 754: Class defaultClass) 755: { 756: Class klass = getClassProperty(propertyName, defaultClass); 757: if (klass == null) 758: return null; 759: 760: try 761: { 762: Object obj = klass.newInstance(); 763: if (ofClass.isInstance(obj)) 764: return obj; 765: } 766: catch (InstantiationException e) 767: { 768: warn(propertyName + " = " + klass.getName(), e); 769: } 770: catch (IllegalAccessException e) 771: { 772: warn(propertyName + " = " + klass.getName(), e); 773: } 774: 775: if (defaultClass == null) 776: return null; 777: 778: try 779: { 780: return defaultClass.newInstance(); 781: } 782: catch (java.lang.InstantiationException ex) 783: { 784: throw new RuntimeException(ex.getMessage()); 785: } 786: catch (java.lang.IllegalAccessException ex) 787: { 788: throw new RuntimeException(ex.getMessage()); 789: } 790: } 791: 792: /** 793: * An instance of <code>LoggingPermission("control")</code> 794: * that is shared between calls to <code>checkAccess()</code>. 795: */ 796: private static final LoggingPermission controlPermission = new LoggingPermission("control", 797: null); 798: 799: /** 800: * Checks whether the current security context allows changing 801: * the configuration of the logging framework. For the security 802: * context to be trusted, it has to be granted 803: * a LoggingPermission("control"). 804: * 805: * @throws SecurityException if a security manager exists and 806: * the caller is not granted the permission to control 807: * the logging infrastructure. 808: */ 809: public void checkAccess() throws SecurityException 810: { 811: SecurityManager sm = System.getSecurityManager(); 812: if (sm != null) 813: sm.checkPermission(controlPermission); 814: } 815: 816: /** 817: * Creates a new instance of a class specified by name and verifies 818: * that it is an instance (or subclass of) a given type. 819: * 820: * @param className the name of the class of which a new instance 821: * should be created. 822: * 823: * @param type the object created must be an instance of 824: * <code>type</code> or any subclass of <code>type</code> 825: * 826: * @param property the system property to reference in error 827: * messages 828: * 829: * @return the new instance, or <code>null</code> if 830: * <code>className</code> is <code>null</code>, if no class 831: * with that name could be found, if there was an error 832: * loading that class, or if the constructor of the class 833: * has thrown an exception. 834: */ 835: private static final Object createInstance(String className, Class type, 836: String property) 837: { 838: Class klass = null; 839: 840: if ((className == null) || (className.length() == 0)) 841: return null; 842: 843: try 844: { 845: klass = locateClass(className); 846: if (type.isAssignableFrom(klass)) 847: return klass.newInstance(); 848: warn(property, className, "not an instance of " + type.getName()); 849: } 850: catch (ClassNotFoundException e) 851: { 852: warn(property, className, "class not found", e); 853: } 854: catch (IllegalAccessException e) 855: { 856: warn(property, className, "illegal access", e); 857: } 858: catch (InstantiationException e) 859: { 860: warn(property, className, e); 861: } 862: catch (java.lang.LinkageError e) 863: { 864: warn(property, className, "linkage error", e); 865: } 866: 867: return null; 868: } 869: 870: private static final void warn(String property, String klass, Throwable t) 871: { 872: warn(property, klass, null, t); 873: } 874: 875: private static final void warn(String property, String klass, String msg) 876: { 877: warn(property, klass, msg, null); 878: } 879: 880: private static final void warn(String property, String klass, String msg, 881: Throwable t) 882: { 883: warn("error instantiating '" + klass + "' referenced by " + property + 884: (msg == null ? "" : ", " + msg), t); 885: } 886: 887: /** 888: * All debug warnings go through this method. 889: */ 890: 891: private static final void warn(String msg, Throwable t) 892: { 893: System.err.println("WARNING: " + msg); 894: if (t != null) 895: t.printStackTrace(System.err); 896: } 897: 898: /** 899: * Locates a class by first checking the system class loader and 900: * then checking the context class loader. 901: * 902: * @param name the fully qualified name of the Class to locate 903: * @return Class the located Class 904: */ 905: 906: private static Class locateClass(String name) throws ClassNotFoundException 907: { 908: ClassLoader loader = Thread.currentThread().getContextClassLoader(); 909: try 910: { 911: return Class.forName(name, true, loader); 912: } 913: catch (ClassNotFoundException e) 914: { 915: loader = ClassLoader.getSystemClassLoader(); 916: return Class.forName(name, true, loader); 917: } 918: } 919: 920: /** 921: * Return the logging bean. There is a single logging bean per 922: * VM instance. 923: * @since 1.5 924: */ 925: public static synchronized LoggingMXBean getLoggingMXBean() 926: { 927: if (loggingBean == null) 928: { 929: loggingBean = new LoggingMXBean() 930: { 931: public String getLoggerLevel(String logger) 932: { 933: LogManager mgr = getLogManager(); 934: Logger l = mgr.getLogger(logger); 935: if (l == null) 936: return null; 937: Level lev = l.getLevel(); 938: if (lev == null) 939: return ""; 940: return lev.getName(); 941: } 942: 943: public List getLoggerNames() 944: { 945: LogManager mgr = getLogManager(); 946: // This is inefficient, but perhaps better for maintenance. 947: return Collections.list(mgr.getLoggerNames()); 948: } 949: 950: public String getParentLoggerName(String logger) 951: { 952: LogManager mgr = getLogManager(); 953: Logger l = mgr.getLogger(logger); 954: if (l == null) 955: return null; 956: l = l.getParent(); 957: if (l == null) 958: return ""; 959: return l.getName(); 960: } 961: 962: public void setLoggerLevel(String logger, String level) 963: { 964: LogManager mgr = getLogManager(); 965: Logger l = mgr.getLogger(logger); 966: if (l == null) 967: throw new IllegalArgumentException("no logger named " + logger); 968: Level newLevel; 969: if (level == null) 970: newLevel = null; 971: else 972: newLevel = Level.parse(level); 973: l.setLevel(newLevel); 974: } 975: }; 976: } 977: return loggingBean; 978: } 979: }
GNU Classpath (0.92) |