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.Objects;
20  
21  import javax.management.ObjectName;
22  
23  import org.apache.logging.log4j.core.Appender;
24  import org.apache.logging.log4j.core.filter.AbstractFilterable;
25  
26  /**
27   * Implementation of the {@code AppenderAdminMBean} interface.
28   */
29  public class AppenderAdmin implements AppenderAdminMBean {
30  
31      private final String contextName;
32      private final Appender appender;
33      private final ObjectName objectName;
34  
35      /**
36       * Constructs a new {@code AppenderAdmin} with the specified contextName
37       * and appender.
38       *
39       * @param contextName used in the {@code ObjectName} for this mbean
40       * @param appender the instrumented object
41       */
42      public AppenderAdmin(final String contextName, final Appender appender) {
43          // super(executor); // no notifications for now
44          this.contextName = Objects.requireNonNull(contextName, "contextName");
45          this.appender = Objects.requireNonNull(appender, "appender");
46          try {
47              final String ctxName = Server.escape(this.contextName);
48              final String configName = Server.escape(appender.getName());
49              final String name = String.format(PATTERN, ctxName, configName);
50              objectName = new ObjectName(name);
51          } catch (final Exception e) {
52              throw new IllegalStateException(e);
53          }
54      }
55  
56      /**
57       * Returns the {@code ObjectName} of this mbean.
58       *
59       * @return the {@code ObjectName}
60       * @see AppenderAdminMBean#PATTERN
61       */
62      public ObjectName getObjectName() {
63          return objectName;
64      }
65  
66      @Override
67      public String getName() {
68          return appender.getName();
69      }
70  
71      @Override
72      public String getLayout() {
73          return String.valueOf(appender.getLayout());
74      }
75  
76      @Override
77      public boolean isIgnoreExceptions() {
78          return appender.ignoreExceptions();
79      }
80  
81      @Override
82      public String getErrorHandler() {
83          return String.valueOf(appender.getHandler());
84      }
85  
86      @Override
87      public String getFilter() {
88          if (appender instanceof AbstractFilterable) {
89              return String.valueOf(((AbstractFilterable) appender).getFilter());
90          }
91          return null;
92      }
93  }