View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.jmx;
18  
19  import java.util.List;
20  
21  import javax.management.ObjectName;
22  
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.core.config.AppenderRef;
25  import org.apache.logging.log4j.core.config.LoggerConfig;
26  import org.apache.logging.log4j.core.helpers.Assert;
27  
28  /**
29   * Implementation of the {@code LoggerConfigAdminMBean} interface.
30   */
31  public class LoggerConfigAdmin implements LoggerConfigAdminMBean {
32  
33      private final String contextName;
34      private final LoggerConfig loggerConfig;
35      private final ObjectName objectName;
36  
37      /**
38       * Constructs a new {@code LoggerConfigAdmin} with the specified contextName
39       * and logger config.
40       * 
41       * @param contextName used in the {@code ObjectName} for this mbean
42       * @param loggerConfig the instrumented object
43       */
44      public LoggerConfigAdmin(String contextName, LoggerConfig loggerConfig) {
45          // super(executor); // no notifications for now
46          this.contextName = Assert.isNotNull(contextName, "contextName");
47          this.loggerConfig = Assert.isNotNull(loggerConfig, "loggerConfig");
48          try {
49              String ctxName = Server.escape(this.contextName);
50              String configName = Server.escape(loggerConfig.getName());
51              String name = String.format(PATTERN, ctxName, configName);
52              objectName = new ObjectName(name);
53          } catch (Exception e) {
54              throw new IllegalStateException(e);
55          }
56      }
57  
58      /**
59       * Returns the {@code ObjectName} of this mbean.
60       * 
61       * @return the {@code ObjectName}
62       * @see LoggerConfigAdminMBean#PATTERN
63       */
64      public ObjectName getObjectName() {
65          return objectName;
66      }
67  
68      @Override
69      public String getName() {
70          return loggerConfig.getName();
71      }
72  
73      @Override
74      public String getLevel() {
75          return loggerConfig.getLevel().name();
76      }
77  
78      @Override
79      public void setLevel(String level) {
80          loggerConfig.setLevel(Level.valueOf(level));
81      }
82  
83      @Override
84      public boolean isAdditive() {
85          return loggerConfig.isAdditive();
86      }
87  
88      @Override
89      public void setAdditive(boolean additive) {
90          loggerConfig.setAdditive(additive);
91      }
92  
93      @Override
94      public boolean isIncludeLocation() {
95          return loggerConfig.isIncludeLocation();
96      }
97  
98      @Override
99      public String getFilter() {
100         return String.valueOf(loggerConfig.getFilter());
101     }
102 
103     @Override
104     public String[] getAppenderRefs() {
105         List<AppenderRef> refs = loggerConfig.getAppenderRefs();
106         String[] result = new String[refs.size()];
107         for (int i = 0; i < result.length; i++) {
108             result[i] = refs.get(i).getRef();
109         }
110         return result;
111     }
112 }