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 org.apache.commons.lang3.reflect.MethodUtils;
14  
15  import psiprobe.tools.logging.AbstractLogDestination;
16  
17  /**
18   * The Class Jdk14HandlerAccessor.
19   */
20  public class Jdk14HandlerAccessor extends AbstractLogDestination {
21  
22    /** The logger accessor. */
23    private Jdk14LoggerAccessor loggerAccessor;
24  
25    /** The index. */
26    private String index;
27  
28    /**
29     * Gets the logger accessor.
30     *
31     * @return the logger accessor
32     */
33    public Jdk14LoggerAccessor getLoggerAccessor() {
34      return loggerAccessor;
35    }
36  
37    /**
38     * Sets the logger accessor.
39     *
40     * @param loggerAccessor the new logger accessor
41     */
42    public void setLoggerAccessor(Jdk14LoggerAccessor loggerAccessor) {
43      this.loggerAccessor = loggerAccessor;
44    }
45  
46    @Override
47    public boolean isContext() {
48      return getLoggerAccessor().isContext();
49    }
50  
51    @Override
52    public boolean isRoot() {
53      return getLoggerAccessor().isRoot();
54    }
55  
56    @Override
57    public String getName() {
58      return getLoggerAccessor().getName();
59    }
60  
61    @Override
62    public String getIndex() {
63      return index;
64    }
65  
66    /**
67     * Sets the index.
68     *
69     * @param index the new index
70     */
71    public void setIndex(String index) {
72      this.index = index;
73    }
74  
75    @Override
76    public String getLogType() {
77      return "jdk";
78    }
79  
80    @Override
81    public String getLevel() {
82      return getLoggerAccessor().getLevel();
83    }
84  
85    /**
86     * Sets the level.
87     *
88     * @param newLevelStr the new level
89     */
90    public void setLevel(String newLevelStr) {
91      try {
92        Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
93        Object newLevel = MethodUtils.invokeMethod(level, "parse", newLevelStr);
94        MethodUtils.invokeMethod(getTarget(), "setLevel", newLevel);
95      } catch (Exception e) {
96        logger.error("{}#setLevel('{}') failed", getTarget().getClass().getName(), newLevelStr, e);
97      }
98    }
99  
100   @Override
101   public String[] getValidLevels() {
102     return new String[] {"OFF", "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST",
103         "ALL"};
104   }
105 
106 }