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