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.util.Assert;
22  
23  import com.lmax.disruptor.RingBuffer;
24  
25  /**
26   * Instruments an LMAX Disruptor ring buffer.
27   */
28  public class RingBufferAdmin implements RingBufferAdminMBean {
29  
30      private final RingBuffer<?> ringBuffer;
31      private final ObjectName objectName;
32  
33      public static RingBufferAdmin forAsyncLogger(final RingBuffer<?> ringBuffer, final String contextName) {
34          final String ctxName = Server.escape(contextName);
35          final String name = String.format(PATTERN_ASYNC_LOGGER, ctxName);
36          return new RingBufferAdmin(ringBuffer, name);
37      }
38  
39      public static RingBufferAdmin forAsyncLoggerConfig(final RingBuffer<?> ringBuffer, 
40              final String contextName, final String configName) {
41          final String ctxName = Server.escape(contextName);
42          final String cfgName = Server.escape(configName);
43          final String name = String.format(PATTERN_ASYNC_LOGGER_CONFIG, ctxName, cfgName);
44          return new RingBufferAdmin(ringBuffer, name);
45      }
46      
47      protected RingBufferAdmin(final RingBuffer<?> ringBuffer, final String mbeanName) {
48          this.ringBuffer = Assert.requireNonNull(ringBuffer, "ringbuffer");        
49          try {
50              objectName = new ObjectName(mbeanName);
51          } catch (final Exception e) {
52              throw new IllegalStateException(e);
53          }
54      }
55      
56      @Override
57      public long getBufferSize() {
58          return ringBuffer.getBufferSize();
59      }
60      
61      @Override
62      public long getRemainingCapacity() {
63          return ringBuffer.remainingCapacity();
64      }
65  
66      /**
67       * Returns the {@code ObjectName} of this mbean.
68       *
69       * @return the {@code ObjectName}
70       * @see RingBufferAdminMBean#PATTERN_ASYNC_LOGGER
71       * @see RingBufferAdminMBean#PATTERN_ASYNC_LOGGER_CONFIG
72       */
73      public ObjectName getObjectName() {
74          return objectName;
75      }
76  
77  }