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