1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util.logging;
9   
10  import java.util.Enumeration;
11  import java.util.List;
12  import java.util.ArrayList;
13  
14  /** 
15   * Logging is the implementation class of LoggingMXBean.
16   *
17   * The <tt>LoggingMXBean</tt> interface provides a standard
18   * method for management access to the individual
19   * java.util.Logger objects available at runtime.
20   * 
21   * @author Ron Mann
22   * @author Mandy Chung
23   * @version %I%, %G%
24   * @since 1.5
25   *
26   * @see javax.management
27   * @see java.util.Logger
28   * @see java.util.LogManager
29   */
30  class Logging implements LoggingMXBean {
31  
32      private static LogManager logManager = LogManager.getLogManager();
33  
34      /** Constructor of Logging which is the implementation class
35       *  of LoggingMXBean.
36       */
37      Logging() { 
38      }
39   
40      public List<String> getLoggerNames() {
41          Enumeration loggers = logManager.getLoggerNames();
42          ArrayList<String> array = new ArrayList<String>();
43  
44          for (; loggers.hasMoreElements();) {
45              array.add((String) loggers.nextElement());
46          }
47          return array;
48      }
49  
50      private static String EMPTY_STRING = "";
51      public String getLoggerLevel(String loggerName) {
52          Logger l = logManager.getLogger(loggerName);
53          if (l == null) {
54              return null;
55          }
56  
57          Level level = l.getLevel();
58          if (level == null) {
59              return EMPTY_STRING;
60          } else {
61              return level.getName();
62          }
63      }
64  
65      public void setLoggerLevel(String loggerName, String levelName) {
66          if (loggerName == null) {
67              throw new NullPointerException("loggerName is null");
68          }
69  
70          Logger logger = logManager.getLogger(loggerName);
71          
72          if (logger == null) {
73              throw new IllegalArgumentException("Logger " + loggerName +
74                  "does not exist");
75          }
76   
77          Level level = null; 
78          if (levelName != null) {
79              // parse will throw IAE if logLevel is invalid 
80              level = Level.parse(levelName);
81          }
82  
83          logger.setLevel(level);
84      }
85  
86      public String getParentLoggerName( String loggerName ) {
87          Logger l = logManager.getLogger( loggerName );
88          if (l == null) {
89              return null;
90          }
91  
92          Logger p = l.getParent();        
93          if (p == null) {
94              // root logger
95              return EMPTY_STRING;
96          } else {
97              return p.getName();
98          }
99      }
100 
101 }
102