Source for java.util.logging.LogManager

   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:     for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();)
 322:       {
 323:     Logger possChild = (Logger) ((WeakReference) loggers.get(iter.next()))
 324:       .get();
 325:     if ((possChild == null) || (possChild == logger)
 326:         || (possChild.getParent() != parent))
 327:       continue;
 328:     
 329:     if (! possChild.getName().startsWith(name))
 330:       continue;
 331:     
 332:     if (possChild.getName().charAt(name.length()) != '.')
 333:       continue;
 334:     
 335:     possChild.setParent(logger);
 336:       }
 337: 
 338:     return true;
 339:   }
 340: 
 341:   /**
 342:    * Finds the closest ancestor for a logger among the currently
 343:    * registered ones.  For example, if the currently registered
 344:    * loggers have the names "", "foo", and "foo.bar", the result for
 345:    * "foo.bar.baz" will be the logger whose name is "foo.bar".
 346:    *
 347:    * @param child a logger for whose name no logger has been
 348:    *        registered.
 349:    *
 350:    * @return the closest ancestor for <code>child</code>,
 351:    *         or <code>null</code> if <code>child</code>
 352:    *         is the root logger.
 353:    *
 354:    * @throws NullPointerException if <code>child</code>
 355:    *         is <code>null</code>.
 356:    */
 357:   private synchronized Logger findAncestor(Logger child)
 358:   {
 359:     String childName = child.getName();
 360:     int childNameLength = childName.length();
 361:     Logger best = Logger.root;
 362:     int bestNameLength = 0;
 363: 
 364:     Logger cand;
 365:     String candName;
 366:     int candNameLength;
 367: 
 368:     if (child == Logger.root)
 369:       return null;
 370: 
 371:     for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();)
 372:       {
 373:     candName = (String) iter.next();
 374:     candNameLength = candName.length();
 375: 
 376:     if (candNameLength > bestNameLength
 377:         && childNameLength > candNameLength
 378:         && childName.startsWith(candName)
 379:         && childName.charAt(candNameLength) == '.')
 380:       {
 381:         cand = (Logger) ((WeakReference) loggers.get(candName)).get();
 382:         if ((cand == null) || (cand == child))
 383:           continue;
 384: 
 385:         bestNameLength = candName.length();
 386:         best = cand;
 387:       }
 388:       }
 389: 
 390:     return best;
 391:   }
 392: 
 393:   /**
 394:    * Returns a Logger given its name.
 395:    *
 396:    * @param name the name of the logger.
 397:    *
 398:    * @return a named Logger, or <code>null</code> if there is no
 399:    *     logger with that name.
 400:    *
 401:    * @throw java.lang.NullPointerException if <code>name</code>
 402:    *     is <code>null</code>.
 403:    */
 404:   public synchronized Logger getLogger(String name)
 405:   {
 406:     WeakReference ref;
 407: 
 408:     /* Throw a NullPointerException if name is null. */
 409:     name.getClass();
 410: 
 411:     ref = (WeakReference) loggers.get(name);
 412:     if (ref != null)
 413:       return (Logger) ref.get();
 414:     else
 415:       return null;
 416:   }
 417: 
 418:   /**
 419:    * Returns an Enumeration of currently registered Logger names.
 420:    * Since other threads can register loggers at any time, the
 421:    * result could be different any time this method is called.
 422:    *
 423:    * @return an Enumeration with the names of the currently
 424:    *    registered Loggers.
 425:    */
 426:   public synchronized Enumeration getLoggerNames()
 427:   {
 428:     return Collections.enumeration(loggers.keySet());
 429:   }
 430: 
 431:   /**
 432:    * Resets the logging configuration by removing all handlers for
 433:    * registered named loggers and setting their level to <code>null</code>.
 434:    * The level of the root logger will be set to <code>Level.INFO</code>.
 435:    *
 436:    * @throws SecurityException if a security manager exists and
 437:    *         the caller is not granted the permission to control
 438:    *         the logging infrastructure.
 439:    */
 440:   public synchronized void reset() throws SecurityException
 441:   {
 442:     /* Throw a SecurityException if the caller does not have the
 443:      * permission to control the logging infrastructure.
 444:      */
 445:     checkAccess();
 446: 
 447:     properties = new Properties();
 448: 
 449:     Iterator iter = loggers.values().iterator();
 450:     while (iter.hasNext())
 451:       {
 452:     WeakReference ref;
 453:     Logger logger;
 454: 
 455:     ref = (WeakReference) iter.next();
 456:     if (ref != null)
 457:       {
 458:         logger = (Logger) ref.get();
 459: 
 460:         if (logger == null)
 461:           iter.remove();
 462:         else if (logger != Logger.root)
 463:           {
 464:             logger.resetLogger();
 465:             logger.setLevel(null);
 466:           }
 467:       }
 468:       }
 469: 
 470:     Logger.root.setLevel(Level.INFO);
 471:     Logger.root.resetLogger();
 472:   }
 473: 
 474:   /**
 475:    * Configures the logging framework by reading a configuration file.
 476:    * The name and location of this file are specified by the system
 477:    * property <code>java.util.logging.config.file</code>.  If this
 478:    * property is not set, the URL
 479:    * "{gnu.classpath.home.url}/logging.properties" is taken, where
 480:    * "{gnu.classpath.home.url}" stands for the value of the system
 481:    * property <code>gnu.classpath.home.url</code>.
 482:    *
 483:    * <p>The task of configuring the framework is then delegated to
 484:    * {@link #readConfiguration(java.io.InputStream)}, which will
 485:    * notify registered listeners after having read the properties.
 486:    *
 487:    * @throws SecurityException if a security manager exists and
 488:    *         the caller is not granted the permission to control
 489:    *         the logging infrastructure, or if the caller is
 490:    *         not granted the permission to read the configuration
 491:    *         file.
 492:    *
 493:    * @throws IOException if there is a problem reading in the
 494:    *         configuration file.
 495:    */
 496:   public synchronized void readConfiguration()
 497:     throws IOException, SecurityException
 498:   {
 499:     String path;
 500:     InputStream inputStream;
 501: 
 502:     path = System.getProperty("java.util.logging.config.file");
 503:     if ((path == null) || (path.length() == 0))
 504:       {
 505:         String url = (System.getProperty("gnu.classpath.home.url")
 506:                       + "/logging.properties");
 507:         try
 508:           {
 509:             inputStream = new URL(url).openStream();
 510:           } 
 511:         catch (Exception e)
 512:           {
 513:             inputStream=null;
 514:           }
 515: 
 516:         // If no config file could be found use a default configuration.
 517:         if(inputStream == null)
 518:           {
 519:             String defaultConfig = "handlers = java.util.logging.ConsoleHandler   \n"
 520:               + ".level=INFO \n";
 521:             inputStream = new ByteArrayInputStream(defaultConfig.getBytes());
 522:           }
 523:       }
 524:     else
 525:       inputStream = new java.io.FileInputStream(path);
 526: 
 527:     try
 528:       {
 529:         readConfiguration(inputStream);
 530:       }
 531:     finally
 532:       {
 533:         // Close the stream in order to save
 534:         // resources such as file descriptors.
 535:         inputStream.close();
 536:       }
 537:   }
 538: 
 539:   public synchronized void readConfiguration(InputStream inputStream)
 540:     throws IOException, SecurityException
 541:   {
 542:     Properties newProperties;
 543:     Enumeration keys;
 544: 
 545:     checkAccess();
 546:     newProperties = new Properties();
 547:     newProperties.load(inputStream);
 548:     reset();
 549:     this.properties = newProperties;
 550:     keys = newProperties.propertyNames();
 551: 
 552:     while (keys.hasMoreElements())
 553:       {
 554:     String key = ((String) keys.nextElement()).trim();
 555:     String value = newProperties.getProperty(key);
 556: 
 557:     if (value == null)
 558:       continue;
 559: 
 560:     value = value.trim();
 561: 
 562:     if ("handlers".equals(key))
 563:       {
 564:         StringTokenizer tokenizer = new StringTokenizer(value);
 565:         while (tokenizer.hasMoreTokens())
 566:           {
 567:         String handlerName = tokenizer.nextToken();
 568:                 Handler handler = (Handler)
 569:                   createInstance(handlerName, Handler.class, key);
 570:                 Logger.root.addHandler(handler);
 571:           }
 572:       }
 573: 
 574:     if (key.endsWith(".level"))
 575:       {
 576:         String loggerName = key.substring(0, key.length() - 6);
 577:         Logger logger = getLogger(loggerName);
 578: 
 579:         if (logger == null)
 580:           {
 581:         logger = Logger.getLogger(loggerName);
 582:         addLogger(logger);
 583:           }
 584:             Level level = null;
 585:         try
 586:               {
 587:                 level = Level.parse(value);
 588:               }
 589:             catch (IllegalArgumentException e)
 590:               {
 591:                 warn("bad level \'" + value + "\'", e);
 592:               }
 593:             if (level != null)
 594:               {
 595:                 logger.setLevel(level);
 596:               }
 597:         continue;
 598:       }
 599:       }
 600: 
 601:     /* The API specification does not talk about the
 602:      * property name that is distributed with the
 603:      * PropertyChangeEvent.  With test code, it could
 604:      * be determined that the Sun J2SE 1.4 reference
 605:      * implementation uses null for the property name.
 606:      */
 607:     pcs.firePropertyChange(null, null, null);
 608:   }
 609: 
 610:   /**
 611:    * Returns the value of a configuration property as a String.
 612:    */
 613:   public synchronized String getProperty(String name)
 614:   {
 615:     if (properties != null)
 616:       return properties.getProperty(name);
 617:     else
 618:       return null;
 619:   }
 620: 
 621:   /**
 622:    * Returns the value of a configuration property as an integer.
 623:    * This function is a helper used by the Classpath implementation
 624:    * of java.util.logging, it is <em>not</em> specified in the
 625:    * logging API.
 626:    *
 627:    * @param name the name of the configuration property.
 628:    *
 629:    * @param defaultValue the value that will be returned if the
 630:    *        property is not defined, or if its value is not an integer
 631:    *        number.
 632:    */
 633:   static int getIntProperty(String name, int defaultValue)
 634:   {
 635:     try
 636:       {
 637:     return Integer.parseInt(getLogManager().getProperty(name));
 638:       }
 639:     catch (Exception ex)
 640:       {
 641:     return defaultValue;
 642:       }
 643:   }
 644: 
 645:   /**
 646:    * Returns the value of a configuration property as an integer,
 647:    * provided it is inside the acceptable range.
 648:    * This function is a helper used by the Classpath implementation
 649:    * of java.util.logging, it is <em>not</em> specified in the
 650:    * logging API.
 651:    *
 652:    * @param name the name of the configuration property.
 653:    *
 654:    * @param minValue the lowest acceptable value.
 655:    *
 656:    * @param maxValue the highest acceptable value.
 657:    *
 658:    * @param defaultValue the value that will be returned if the
 659:    *        property is not defined, or if its value is not an integer
 660:    *        number, or if it is less than the minimum value,
 661:    *        or if it is greater than the maximum value.
 662:    */
 663:   static int getIntPropertyClamped(String name, int defaultValue,
 664:                                    int minValue, int maxValue)
 665:   {
 666:     int val = getIntProperty(name, defaultValue);
 667:     if ((val < minValue) || (val > maxValue))
 668:       val = defaultValue;
 669:     return val;
 670:   }
 671: 
 672:   /**
 673:    * Returns the value of a configuration property as a boolean.
 674:    * This function is a helper used by the Classpath implementation
 675:    * of java.util.logging, it is <em>not</em> specified in the
 676:    * logging API.
 677:    *
 678:    * @param name the name of the configuration property.
 679:    *
 680:    * @param defaultValue the value that will be returned if the
 681:    *        property is not defined, or if its value is neither
 682:    *        <code>"true"</code> nor <code>"false"</code>.
 683:    */
 684:   static boolean getBooleanProperty(String name, boolean defaultValue)
 685:   {
 686:     try
 687:       {
 688:     return (Boolean.valueOf(getLogManager().getProperty(name))).booleanValue();
 689:       }
 690:     catch (Exception ex)
 691:       {
 692:     return defaultValue;
 693:       }
 694:   }
 695: 
 696:   /**
 697:    * Returns the value of a configuration property as a Level.
 698:    * This function is a helper used by the Classpath implementation
 699:    * of java.util.logging, it is <em>not</em> specified in the
 700:    * logging API.
 701:    *
 702:    * @param propertyName the name of the configuration property.
 703:    *
 704:    * @param defaultValue the value that will be returned if the
 705:    *        property is not defined, or if
 706:    *        {@link Level#parse(java.lang.String)} does not like
 707:    *        the property value.
 708:    */
 709:   static Level getLevelProperty(String propertyName, Level defaultValue)
 710:   {
 711:     try
 712:       {
 713:     return Level.parse(getLogManager().getProperty(propertyName));
 714:       }
 715:     catch (Exception ex)
 716:       {
 717:     return defaultValue;
 718:       }
 719:   }
 720: 
 721:   /**
 722:    * Returns the value of a configuration property as a Class.
 723:    * This function is a helper used by the Classpath implementation
 724:    * of java.util.logging, it is <em>not</em> specified in the
 725:    * logging API.
 726:    *
 727:    * @param propertyName the name of the configuration property.
 728:    *
 729:    * @param defaultValue the value that will be returned if the
 730:    *        property is not defined, or if it does not specify
 731:    *        the name of a loadable class.
 732:    */
 733:   static final Class getClassProperty(String propertyName, Class defaultValue)
 734:   {
 735:     String propertyValue = logManager.getProperty(propertyName);
 736: 
 737:     if (propertyValue != null)
 738:       try
 739:         {
 740:           return locateClass(propertyValue);
 741:         }
 742:       catch (ClassNotFoundException e)
 743:         {
 744:           warn(propertyName + " = " + propertyValue, e);
 745:         }
 746: 
 747:     return defaultValue;
 748:   }
 749: 
 750:   static final Object getInstanceProperty(String propertyName, Class ofClass,
 751:                                           Class defaultClass)
 752:   {
 753:     Class klass = getClassProperty(propertyName, defaultClass);
 754:     if (klass == null)
 755:       return null;
 756: 
 757:     try
 758:       {
 759:         Object obj = klass.newInstance();
 760:         if (ofClass.isInstance(obj))
 761:           return obj;
 762:       }
 763:     catch (InstantiationException e)
 764:       {
 765:         warn(propertyName + " = " + klass.getName(), e);
 766:       }
 767:     catch (IllegalAccessException e)
 768:       {
 769:         warn(propertyName + " = " + klass.getName(), e);
 770:       }
 771: 
 772:     if (defaultClass == null)
 773:       return null;
 774: 
 775:     try
 776:       {
 777:     return defaultClass.newInstance();
 778:       }
 779:     catch (java.lang.InstantiationException ex)
 780:       {
 781:     throw new RuntimeException(ex.getMessage());
 782:       }
 783:     catch (java.lang.IllegalAccessException ex)
 784:       {
 785:     throw new RuntimeException(ex.getMessage());
 786:       }
 787:   }
 788: 
 789:   /**
 790:    * An instance of <code>LoggingPermission("control")</code>
 791:    * that is shared between calls to <code>checkAccess()</code>.
 792:    */
 793:   private static final LoggingPermission controlPermission = new LoggingPermission("control",
 794:                                                                                    null);
 795: 
 796:   /**
 797:    * Checks whether the current security context allows changing
 798:    * the configuration of the logging framework.  For the security
 799:    * context to be trusted, it has to be granted
 800:    * a LoggingPermission("control").
 801:    *
 802:    * @throws SecurityException if a security manager exists and
 803:    *         the caller is not granted the permission to control
 804:    *         the logging infrastructure.
 805:    */
 806:   public void checkAccess() throws SecurityException
 807:   {
 808:     SecurityManager sm = System.getSecurityManager();
 809:     if (sm != null)
 810:       sm.checkPermission(controlPermission);
 811:   }
 812: 
 813:   /**
 814:    * Creates a new instance of a class specified by name and verifies
 815:    * that it is an instance (or subclass of) a given type.
 816:    *
 817:    * @param className the name of the class of which a new instance
 818:    *        should be created.
 819:    *
 820:    * @param type the object created must be an instance of
 821:    * <code>type</code> or any subclass of <code>type</code>
 822:    *
 823:    * @param property the system property to reference in error
 824:    * messages
 825:    *
 826:    * @return the new instance, or <code>null</code> if
 827:    *         <code>className</code> is <code>null</code>, if no class
 828:    *         with that name could be found, if there was an error
 829:    *         loading that class, or if the constructor of the class
 830:    *         has thrown an exception.
 831:    */
 832:   private static final Object createInstance(String className, Class type,
 833:                                              String property)
 834:   {
 835:     Class klass = null;
 836: 
 837:     if ((className == null) || (className.length() == 0))
 838:       return null;
 839: 
 840:     try
 841:       {
 842:         klass = locateClass(className);
 843:         if (type.isAssignableFrom(klass))
 844:           return klass.newInstance();
 845:         warn(property, className, "not an instance of " + type.getName());
 846:       }
 847:     catch (ClassNotFoundException e)
 848:       {
 849:         warn(property, className, "class not found", e);
 850:       }
 851:     catch (IllegalAccessException e)
 852:       {
 853:         warn(property, className, "illegal access", e);
 854:       }
 855:     catch (InstantiationException e)
 856:       {
 857:         warn(property, className, e);
 858:       }
 859:     catch (java.lang.LinkageError e)
 860:       {
 861:         warn(property, className, "linkage error", e);
 862:       }
 863: 
 864:     return null;
 865:   }
 866: 
 867:   private static final void warn(String property, String klass, Throwable t)
 868:   {
 869:     warn(property, klass, null, t);
 870:   }
 871: 
 872:   private static final void warn(String property, String klass, String msg)
 873:   {
 874:     warn(property, klass, msg, null);
 875:   }
 876: 
 877:   private static final void warn(String property, String klass, String msg,
 878:                                  Throwable t)
 879:   {
 880:     warn("error instantiating '" + klass + "' referenced by " + property +
 881:          (msg == null ? "" : ", " + msg), t);
 882:   }
 883: 
 884:   /**
 885:    * All debug warnings go through this method.
 886:    */
 887: 
 888:   private static final void warn(String msg, Throwable t)
 889:   {
 890:     System.err.println("WARNING: " + msg);
 891:     if (t != null)
 892:       t.printStackTrace(System.err);
 893:   }
 894: 
 895:   /**
 896:    * Locates a class by first checking the system class loader and
 897:    * then checking the context class loader.
 898:    *
 899:    * @param name the fully qualified name of the Class to locate
 900:    * @return Class the located Class
 901:    */
 902: 
 903:   private static Class locateClass(String name) throws ClassNotFoundException
 904:   {
 905:     // GCJ LOCAL
 906:     // Unfortunately this can be called during bootstrap when
 907:     // Thread.currentThread() will return null.
 908:     // See bug #27658
 909:     Thread t = Thread.currentThread();
 910:     ClassLoader loader = (t == null) ? null : t.getContextClassLoader();
 911:     try
 912:       {
 913:         return Class.forName(name, true, loader);
 914:       }
 915:     catch (ClassNotFoundException e)
 916:       {
 917:         loader = ClassLoader.getSystemClassLoader();
 918:         return Class.forName(name, true, loader);
 919:       }
 920:   }
 921: 
 922:   /**
 923:    * Return the logging bean.  There is a single logging bean per
 924:    * VM instance.
 925:    * @since 1.5
 926:    */
 927:   public static synchronized LoggingMXBean getLoggingMXBean()
 928:   {
 929:     if (loggingBean == null)
 930:       {
 931:         loggingBean = new LoggingMXBean()
 932:         {
 933:           public String getLoggerLevel(String logger)
 934:           {
 935:             LogManager mgr = getLogManager();
 936:             Logger l = mgr.getLogger(logger);
 937:             if (l == null)
 938:               return null;
 939:             Level lev = l.getLevel();
 940:             if (lev == null)
 941:               return "";
 942:             return lev.getName();
 943:           }
 944: 
 945:           public List getLoggerNames()
 946:           {
 947:             LogManager mgr = getLogManager();
 948:             // This is inefficient, but perhaps better for maintenance.
 949:             return Collections.list(mgr.getLoggerNames());
 950:           }
 951: 
 952:           public String getParentLoggerName(String logger)
 953:           {
 954:             LogManager mgr = getLogManager();
 955:             Logger l = mgr.getLogger(logger);
 956:             if (l == null)
 957:               return null;
 958:             l = l.getParent();
 959:             if (l == null)
 960:               return "";
 961:             return l.getName();
 962:           }
 963: 
 964:           public void setLoggerLevel(String logger, String level)
 965:           {
 966:             LogManager mgr = getLogManager();
 967:             Logger l = mgr.getLogger(logger);
 968:             if (l == null)
 969:               throw new IllegalArgumentException("no logger named " + logger);
 970:             Level newLevel;
 971:             if (level == null)
 972:               newLevel = null;
 973:             else
 974:               newLevel = Level.parse(level);
 975:             l.setLevel(newLevel);
 976:           }
 977:         };
 978:       }
 979:     return loggingBean;
 980:   }
 981: }