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.AsyncAppender;
22  import org.apache.logging.log4j.core.util.Assert;
23  
24  /**
25   * Implementation of the {@code AsyncAppenderAdminMBean} interface.
26   */
27  public class AsyncAppenderAdmin implements AsyncAppenderAdminMBean {
28  
29      private final String contextName;
30      private final AsyncAppender asyncAppender;
31      private final ObjectName objectName;
32  
33      /**
34       * Constructs a new {@code AsyncAppenderAdmin} with the specified contextName
35       * and async appender.
36       *
37       * @param contextName used in the {@code ObjectName} for this mbean
38       * @param appender the instrumented object
39       */
40      public AsyncAppenderAdmin(final String contextName, final AsyncAppender appender) {
41          // super(executor); // no notifications for now
42          this.contextName = Assert.requireNonNull(contextName, "contextName");
43          this.asyncAppender = Assert.requireNonNull(appender, "async appender");
44          try {
45              final String ctxName = Server.escape(this.contextName);
46              final String configName = Server.escape(appender.getName());
47              final String name = String.format(PATTERN, ctxName, configName);
48              objectName = new ObjectName(name);
49          } catch (final 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 asyncAppender.getName();
67      }
68  
69      @Override
70      public String getLayout() {
71          return String.valueOf(asyncAppender.getLayout());
72      }
73  
74      @Override
75      public boolean isIgnoreExceptions() {
76          return asyncAppender.ignoreExceptions();
77      }
78  
79      @Override
80      public String getErrorHandler() {
81          return String.valueOf(asyncAppender.getHandler());
82      }
83  
84      @Override
85      public String getFilter() {
86          return String.valueOf(asyncAppender.getFilter());
87      }
88  
89      @Override
90      public String[] getAppenderRefs() {
91          return asyncAppender.getAppenderRefStrings();
92      }
93      
94      /**
95       * Returns {@code true} if this AsyncAppender will take a snapshot of the stack with
96       * every log event to determine the class and method where the logging call
97       * was made.
98       * @return {@code true} if location is included with every event, {@code false} otherwise
99       */
100     @Override
101     public boolean isIncludeLocation() {
102         return asyncAppender.isIncludeLocation();
103     }
104     
105     /**
106      * Returns {@code true} if this AsyncAppender will block when the queue is full,
107      * or {@code false} if events are dropped when the queue is full.
108      * @return whether this AsyncAppender will block or drop events when the queue is full.
109      */
110     @Override
111     public boolean isBlocking() {
112         return asyncAppender.isBlocking();
113     }
114     
115     /**
116      * Returns the name of the appender that any errors are logged to or {@code null}.
117      * @return the name of the appender that any errors are logged to or {@code null}
118      */
119     @Override
120     public String getErrorRef() {
121         return asyncAppender.getErrorRef();
122     }
123     
124     @Override
125     public int getQueueCapacity() {
126         return asyncAppender.getQueueCapacity();
127     }
128     
129     @Override
130     public int getQueueRemainingCapacity() {
131         return asyncAppender.getQueueRemainingCapacity();
132     }
133 }