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 */
017package org.apache.logging.log4j.core.jmx;
018
019import javax.management.ObjectName;
020
021import org.apache.logging.log4j.core.appender.AsyncAppender;
022import org.apache.logging.log4j.core.util.Assert;
023
024/**
025 * Implementation of the {@code AsyncAppenderAdminMBean} interface.
026 */
027public class AsyncAppenderAdmin implements AsyncAppenderAdminMBean {
028
029    private final String contextName;
030    private final AsyncAppender asyncAppender;
031    private final ObjectName objectName;
032
033    /**
034     * Constructs a new {@code AsyncAppenderAdmin} with the specified contextName
035     * and async appender.
036     *
037     * @param contextName used in the {@code ObjectName} for this mbean
038     * @param appender the instrumented object
039     */
040    public AsyncAppenderAdmin(final String contextName, final AsyncAppender appender) {
041        // super(executor); // no notifications for now
042        this.contextName = Assert.requireNonNull(contextName, "contextName");
043        this.asyncAppender = Assert.requireNonNull(appender, "async appender");
044        try {
045            final String ctxName = Server.escape(this.contextName);
046            final String configName = Server.escape(appender.getName());
047            final String name = String.format(PATTERN, ctxName, configName);
048            objectName = new ObjectName(name);
049        } catch (final Exception e) {
050            throw new IllegalStateException(e);
051        }
052    }
053
054    /**
055     * Returns the {@code ObjectName} of this mbean.
056     *
057     * @return the {@code ObjectName}
058     * @see AppenderAdminMBean#PATTERN
059     */
060    public ObjectName getObjectName() {
061        return objectName;
062    }
063
064    @Override
065    public String getName() {
066        return asyncAppender.getName();
067    }
068
069    @Override
070    public String getLayout() {
071        return String.valueOf(asyncAppender.getLayout());
072    }
073
074    @Override
075    public boolean isIgnoreExceptions() {
076        return asyncAppender.ignoreExceptions();
077    }
078
079    @Override
080    public String getErrorHandler() {
081        return String.valueOf(asyncAppender.getHandler());
082    }
083
084    @Override
085    public String getFilter() {
086        return String.valueOf(asyncAppender.getFilter());
087    }
088
089    @Override
090    public String[] getAppenderRefs() {
091        return asyncAppender.getAppenderRefStrings();
092    }
093    
094    /**
095     * Returns {@code true} if this AsyncAppender will take a snapshot of the stack with
096     * every log event to determine the class and method where the logging call
097     * was made.
098     * @return {@code true} if location is included with every event, {@code false} otherwise
099     */
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}