001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017 package org.apache.logging.log4j.core.jmx;
018
019 import javax.management.ObjectName;
020
021 import org.apache.logging.log4j.core.util.Assert;
022
023 import com.lmax.disruptor.RingBuffer;
024
025 /**
026 * Instruments an LMAX Disruptor ring buffer.
027 */
028 public class RingBufferAdmin implements RingBufferAdminMBean {
029
030 private final RingBuffer<?> ringBuffer;
031 private final ObjectName objectName;
032
033 public static RingBufferAdmin forAsyncLogger(final RingBuffer<?> ringBuffer, final String contextName) {
034 final String ctxName = Server.escape(contextName);
035 final String name = String.format(PATTERN_ASYNC_LOGGER, ctxName);
036 return new RingBufferAdmin(ringBuffer, name);
037 }
038
039 public static RingBufferAdmin forAsyncLoggerConfig(final RingBuffer<?> ringBuffer,
040 final String contextName, final String configName) {
041 final String ctxName = Server.escape(contextName);
042 final String cfgName = Server.escape(configName);
043 final String name = String.format(PATTERN_ASYNC_LOGGER_CONFIG, ctxName, cfgName);
044 return new RingBufferAdmin(ringBuffer, name);
045 }
046
047 protected RingBufferAdmin(final RingBuffer<?> ringBuffer, final String mbeanName) {
048 this.ringBuffer = Assert.requireNonNull(ringBuffer, "ringbuffer");
049 try {
050 objectName = new ObjectName(mbeanName);
051 } catch (final Exception e) {
052 throw new IllegalStateException(e);
053 }
054 }
055
056 @Override
057 public long getBufferSize() {
058 return ringBuffer.getBufferSize();
059 }
060
061 @Override
062 public long getRemainingCapacity() {
063 return ringBuffer.remainingCapacity();
064 }
065
066 /**
067 * Returns the {@code ObjectName} of this mbean.
068 *
069 * @return the {@code ObjectName}
070 * @see RingBufferAdminMBean#PATTERN_ASYNC_LOGGER
071 * @see RingBufferAdminMBean#PATTERN_ASYNC_LOGGER_CONFIG
072 */
073 public ObjectName getObjectName() {
074 return objectName;
075 }
076
077 }