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 java.util.Objects;
020
021import javax.management.ObjectName;
022
023import org.apache.logging.log4j.core.appender.AsyncAppender;
024
025/**
026 * Implementation of the {@code AsyncAppenderAdminMBean} interface.
027 */
028public class AsyncAppenderAdmin implements AsyncAppenderAdminMBean {
029
030    private final String contextName;
031    private final AsyncAppender asyncAppender;
032    private final ObjectName objectName;
033
034    /**
035     * Constructs a new {@code AsyncAppenderAdmin} with the specified contextName
036     * and async appender.
037     *
038     * @param contextName used in the {@code ObjectName} for this mbean
039     * @param appender the instrumented object
040     */
041    public AsyncAppenderAdmin(final String contextName, final AsyncAppender appender) {
042        // super(executor); // no notifications for now
043        this.contextName = Objects.requireNonNull(contextName, "contextName");
044        this.asyncAppender = Objects.requireNonNull(appender, "async appender");
045        try {
046            final String ctxName = Server.escape(this.contextName);
047            final String configName = Server.escape(appender.getName());
048            final String name = String.format(PATTERN, ctxName, configName);
049            objectName = new ObjectName(name);
050        } catch (final Exception e) {
051            throw new IllegalStateException(e);
052        }
053    }
054
055    /**
056     * Returns the {@code ObjectName} of this mbean.
057     *
058     * @return the {@code ObjectName}
059     * @see AppenderAdminMBean#PATTERN
060     */
061    public ObjectName getObjectName() {
062        return objectName;
063    }
064
065    @Override
066    public String getName() {
067        return asyncAppender.getName();
068    }
069
070    @Override
071    public String getLayout() {
072        return String.valueOf(asyncAppender.getLayout());
073    }
074
075    @Override
076    public boolean isIgnoreExceptions() {
077        return asyncAppender.ignoreExceptions();
078    }
079
080    @Override
081    public String getErrorHandler() {
082        return String.valueOf(asyncAppender.getHandler());
083    }
084
085    @Override
086    public String getFilter() {
087        return String.valueOf(asyncAppender.getFilter());
088    }
089
090    @Override
091    public String[] getAppenderRefs() {
092        return asyncAppender.getAppenderRefStrings();
093    }
094
095    /**
096     * Returns {@code true} if this AsyncAppender will take a snapshot of the stack with
097     * every log event to determine the class and method where the logging call
098     * was made.
099     * @return {@code true} if location is included with every event, {@code false} otherwise
100     */
101    @Override
102    public boolean isIncludeLocation() {
103        return asyncAppender.isIncludeLocation();
104    }
105
106    /**
107     * Returns {@code true} if this AsyncAppender will block when the queue is full,
108     * or {@code false} if events are dropped when the queue is full.
109     * @return whether this AsyncAppender will block or drop events when the queue is full.
110     */
111    @Override
112    public boolean isBlocking() {
113        return asyncAppender.isBlocking();
114    }
115
116    /**
117     * Returns the name of the appender that any errors are logged to or {@code null}.
118     * @return the name of the appender that any errors are logged to or {@code null}
119     */
120    @Override
121    public String getErrorRef() {
122        return asyncAppender.getErrorRef();
123    }
124
125    @Override
126    public int getQueueCapacity() {
127        return asyncAppender.getQueueCapacity();
128    }
129
130    @Override
131    public int getQueueRemainingCapacity() {
132        return asyncAppender.getQueueRemainingCapacity();
133    }
134}