package org.apache.logging.log4j.core.jmx;

import com.lmax.disruptor.RingBuffer;
import javax.management.ObjectName;
import org.apache.logging.log4j.core.util.Assert;

public class RingBufferAdmin implements RingBufferAdminMBean {
   private final RingBuffer<?> ringBuffer;
   private final ObjectName objectName;

   public static RingBufferAdmin forAsyncLogger(RingBuffer<?> ringBuffer, String contextName) {
      String ctxName = Server.escape(contextName);
      String name = String.format("org.apache.logging.log4j2:type=%s,component=AsyncLoggerRingBuffer", ctxName);
      return new RingBufferAdmin(ringBuffer, name);
   }

   public static RingBufferAdmin forAsyncLoggerConfig(RingBuffer<?> ringBuffer, String contextName, String configName) {
      String ctxName = Server.escape(contextName);
      String cfgName = Server.escape(configName);
      String name = String.format("org.apache.logging.log4j2:type=%s,component=Loggers,name=%s,subtype=RingBuffer", ctxName, cfgName);
      return new RingBufferAdmin(ringBuffer, name);
   }

   protected RingBufferAdmin(RingBuffer<?> ringBuffer, String mbeanName) {
      this.ringBuffer = (RingBuffer)Assert.requireNonNull(ringBuffer, "ringbuffer");

      try {
         this.objectName = new ObjectName(mbeanName);
      } catch (Exception var4) {
         throw new IllegalStateException(var4);
      }
   }

   public long getBufferSize() {
      return (long)this.ringBuffer.getBufferSize();
   }

   public long getRemainingCapacity() {
      return this.ringBuffer.remainingCapacity();
   }

   public ObjectName getObjectName() {
      return this.objectName;
   }
}
