View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools.logging.jdk;
12  
13  import java.lang.reflect.InvocationTargetException;
14  import java.lang.reflect.Method;
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.Enumeration;
18  import java.util.List;
19  
20  import org.apache.commons.lang3.reflect.MethodUtils;
21  
22  import psiprobe.tools.logging.DefaultAccessor;
23  import psiprobe.tools.logging.LogDestination;
24  
25  /**
26   * The Class Jdk14ManagerAccessor.
27   */
28  public class Jdk14ManagerAccessor extends DefaultAccessor {
29  
30    /**
31     * Instantiates a new jdk14 manager accessor.
32     *
33     * @param cl the cl
34     *
35     * @throws ClassNotFoundException the class not found exception
36     * @throws IllegalAccessException the illegal access exception
37     * @throws InvocationTargetException the invocation target exception
38     */
39    public Jdk14ManagerAccessor(ClassLoader cl)
40        throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
41  
42      Class<?> clazz = cl.loadClass("java.util.logging.LogManager");
43      Method getManager = MethodUtils.getAccessibleMethod(clazz, "getLogManager");
44      Object manager = getManager.invoke(null);
45      if (manager == null) {
46        throw new NullPointerException(clazz.getName() + ".getLogManager() returned null");
47      }
48      setTarget(manager);
49    }
50  
51    /**
52     * Gets the root logger.
53     *
54     * @return the root logger
55     */
56    public Jdk14LoggerAccessor getRootLogger() {
57      return getLogger("");
58    }
59  
60    /**
61     * Gets the logger.
62     *
63     * @param name the name
64     *
65     * @return the logger
66     */
67    public Jdk14LoggerAccessor getLogger(String name) {
68      try {
69        Object logger = MethodUtils.invokeMethod(getTarget(), "getLogger", name);
70        if (logger == null) {
71          throw new NullPointerException(
72              getTarget().getClass().getName() + "#getLogger(\"" + name + "\") returned null");
73        }
74        Jdk14LoggerAccessor accessor = new Jdk14LoggerAccessor();
75        accessor.setTarget(logger);
76        accessor.setApplication(getApplication());
77        return accessor;
78      } catch (Exception e) {
79        logger.error("{}#getLogger('{}') failed", getTarget().getClass().getName(), name, e);
80      }
81      return null;
82    }
83  
84    /**
85     * Gets the handlers.
86     *
87     * @return the handlers
88     */
89    @SuppressWarnings("unchecked")
90    public List<LogDestination> getHandlers() {
91      List<LogDestination> allHandlers = new ArrayList<>();
92      try {
93        for (String name : Collections
94            .list((Enumeration<String>) MethodUtils.invokeMethod(getTarget(), "getLoggerNames"))) {
95          Jdk14LoggerAccessor accessor = getLogger(name);
96          if (accessor != null) {
97            allHandlers.addAll(accessor.getHandlers());
98          }
99        }
100     } catch (Exception e) {
101       logger.error("{}#getLoggerNames() failed", getTarget().getClass().getName(), e);
102     }
103     return allHandlers;
104   }
105 
106 }