001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.lang.management;
019:
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.LogManager;
025: import java.util.logging.Logger;
026: import java.util.logging.LoggingMXBean;
027:
028: /**
029: * Runtime type for {@link java.util.logging.LoggingMXBean}.
030: *
031: * @since 1.5
032: */
033: public class LoggingMXBeanImpl extends DynamicMXBeanImpl implements
034: LoggingMXBean {
035:
036: private static LoggingMXBeanImpl instance = new LoggingMXBeanImpl();
037:
038: /**
039: * Constructor intentionally private to prevent instantiation by others.
040: * Sets the metadata for this bean.
041: */
042: private LoggingMXBeanImpl() {
043: setMBeanInfo(ManagementUtils.getMBeanInfo(LoggingMXBean.class
044: .getName()));
045: }
046:
047: /**
048: * Singleton accessor method.
049: *
050: * @return the <code>LoggingMXBeanImpl</code> singleton.
051: */
052: static LoggingMXBeanImpl getInstance() {
053: return instance;
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see java.util.logging.LoggingMXBean#getLoggerLevel(java.lang.String)
060: */
061: public String getLoggerLevel(String loggerName) {
062: String result = null;
063:
064: Logger logger = LogManager.getLogManager()
065: .getLogger(loggerName);
066: if (logger != null) {
067: // The named Logger exists. Now attempt to obtain its log level.
068: Level level = logger.getLevel();
069: if (level != null) {
070: result = level.getName();
071: } else {
072: // A null return from getLevel() means that the Logger
073: // is inheriting its log level from an ancestor. Return an
074: // empty string to the caller.
075: result = "";
076: }
077: }
078: return result;
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see java.util.logging.LoggingMXBean#getLoggerNames()
085: */
086: public List<String> getLoggerNames() {
087: // By default, return an empty list to caller
088: List<String> result = new ArrayList<String>();
089:
090: Enumeration<String> enumeration = LogManager.getLogManager()
091: .getLoggerNames();
092: if (enumeration != null) {
093: while (enumeration.hasMoreElements()) {
094: result.add(enumeration.nextElement());
095: }
096: }
097: return result;
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see java.util.logging.LoggingMXBean#getParentLoggerName(java.lang.String)
104: */
105: public String getParentLoggerName(String loggerName) {
106: String result = null;
107:
108: Logger logger = LogManager.getLogManager()
109: .getLogger(loggerName);
110: if (logger != null) {
111: // The named Logger exists. Now attempt to obtain its parent.
112: Logger parent = logger.getParent();
113: if (parent != null) {
114: // There is a parent
115: result = parent.getName();
116: } else {
117: // logger must be the root Logger
118: result = "";
119: }
120: }
121: return result;
122: }
123:
124: /*
125: * (non-Javadoc)
126: *
127: * @see java.util.logging.LoggingMXBean#setLoggerLevel(java.lang.String,
128: * java.lang.String)
129: */
130: public void setLoggerLevel(String loggerName, String levelName) {
131: final Logger logger = LogManager.getLogManager().getLogger(
132: loggerName);
133: if (logger != null) {
134: // The named Logger exists. Now attempt to set its level. The
135: // below attempt to parse a Level from the supplied levelName
136: // will throw an IllegalArgumentException if levelName is not
137: // a valid level name.
138: if (levelName != null) {
139: Level newLevel = Level.parse(levelName);
140: logger.setLevel(newLevel);
141: } else {
142: logger.setLevel(null);
143: }
144: } else {
145: // Named Logger does not exist.
146: throw new IllegalArgumentException(
147: "Unable to find Logger with name " + loggerName);
148: }
149: }
150: }
|