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