1/*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package java.util.logging;
27
28import java.util.Enumeration;
29import java.util.List;
30import java.util.ArrayList;
31
32/**
33 * Logging is the implementation class of LoggingMXBean.
34 *
35 * The {@code LoggingMXBean} interface provides a standard
36 * method for management access to the individual
37 * {@code Logger} objects available at runtime.
38 *
39 * @author Ron Mann
40 * @author Mandy Chung
41 * @since 1.5
42 *
43 * @see javax.management
44 * @see Logger
45 * @see LogManager
46 */
47@SuppressWarnings("deprecation") // implements LoggingMXBean
48final class Logging implements LoggingMXBean {
49
50    private static LogManager logManager = LogManager.getLogManager();
51
52    /** Constructor of Logging which is the implementation class
53     *  of LoggingMXBean.
54     */
55    private Logging() {
56    }
57
58    @Override
59    public List<String> getLoggerNames() {
60        Enumeration<String> loggers = logManager.getLoggerNames();
61        ArrayList<String> array = new ArrayList<>();
62
63        for (; loggers.hasMoreElements();) {
64            array.add(loggers.nextElement());
65        }
66        return array;
67    }
68
69    private static String EMPTY_STRING = "";
70    @Override
71    public String getLoggerLevel(String loggerName) {
72        Logger l = logManager.getLogger(loggerName);
73        if (l == null) {
74            return null;
75        }
76
77        Level level = l.getLevel();
78        if (level == null) {
79            return EMPTY_STRING;
80        } else {
81            return level.getLevelName();
82        }
83    }
84
85    @Override
86    public void setLoggerLevel(String loggerName, String levelName) {
87        if (loggerName == null) {
88            throw new NullPointerException("loggerName is null");
89        }
90
91        Logger logger = logManager.getLogger(loggerName);
92        if (logger == null) {
93            throw new IllegalArgumentException("Logger " + loggerName +
94                " does not exist");
95        }
96
97        Level level = null;
98        if (levelName != null) {
99            // parse will throw IAE if logLevel is invalid
100            level = Level.findLevel(levelName);
101            if (level == null) {
102                throw new IllegalArgumentException("Unknown level \"" + levelName + "\"");
103            }
104        }
105
106        logger.setLevel(level);
107    }
108
109    @Override
110    public String getParentLoggerName( String loggerName ) {
111        Logger l = logManager.getLogger( loggerName );
112        if (l == null) {
113            return null;
114        }
115
116        Logger p = l.getParent();
117        if (p == null) {
118            // root logger
119            return EMPTY_STRING;
120        } else {
121            return p.getName();
122        }
123    }
124
125    static Logging getInstance() {
126        return INSTANCE;
127    }
128
129    private static final Logging INSTANCE = new Logging();
130
131}
132