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  import javax.management.ObjectName;
21  
22  import org.apache.logging.log4j.Level;
23  import org.apache.logging.log4j.core.config.AppenderRef;
24  import org.apache.logging.log4j.core.config.LoggerConfig;
25  import org.apache.logging.log4j.core.helpers.Assert;
26  
27  /**
28   * Implementation of the {@code LoggerConfigAdminMBean} interface.
29   */
30  public class LoggerConfigAdmin implements LoggerConfigAdminMBean {
31  
32      private final String contextName;
33      private final LoggerConfig loggerConfig;
34      private final ObjectName objectName;
35  
36      /**
37       * Constructs a new {@code LoggerConfigAdmin} with the specified contextName
38       * and logger config.
39       *
40       * @param contextName used in the {@code ObjectName} for this mbean
41       * @param loggerConfig the instrumented object
42       */
43      public LoggerConfigAdmin(final String contextName, final LoggerConfig loggerConfig) {
44          // super(executor); // no notifications for now
45          this.contextName = Assert.isNotNull(contextName, "contextName");
46          this.loggerConfig = Assert.isNotNull(loggerConfig, "loggerConfig");
47          try {
48              final String ctxName = Server.escape(this.contextName);
49              final String configName = Server.escape(loggerConfig.getName());
50              final String name = String.format(PATTERN, ctxName, configName);
51              objectName = new ObjectName(name);
52          } catch (final Exception e) {
53              throw new IllegalStateException(e);
54          }
55      }
56  
57      /**
58       * Returns the {@code ObjectName} of this mbean.
59       *
60       * @return the {@code ObjectName}
61       * @see LoggerConfigAdminMBean#PATTERN
62       */
63      public ObjectName getObjectName() {
64          return objectName;
65      }
66  
67      @Override
68      public String getName() {
69          return loggerConfig.getName();
70      }
71  
72      @Override
73      public String getLevel() {
74          return loggerConfig.getLevel().name();
75      }
76  
77      @Override
78      public void setLevel(final String level) {
79          loggerConfig.setLevel(Level.getLevel(level));
80      }
81  
82      @Override
83      public boolean isAdditive() {
84          return loggerConfig.isAdditive();
85      }
86  
87      @Override
88      public void setAdditive(final boolean additive) {
89          loggerConfig.setAdditive(additive);
90      }
91  
92      @Override
93      public boolean isIncludeLocation() {
94          return loggerConfig.isIncludeLocation();
95      }
96  
97      @Override
98      public String getFilter() {
99          return String.valueOf(loggerConfig.getFilter());
100     }
101 
102     @Override
103     public String[] getAppenderRefs() {
104         final List<AppenderRef> refs = loggerConfig.getAppenderRefs();
105         final String[] result = new String[refs.size()];
106         for (int i = 0; i < result.length; i++) {
107             result[i] = refs.get(i).getRef();
108         }
109         return result;
110     }
111 }