    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.Appender;
    022    import org.apache.logging.log4j.core.filter.AbstractFilterable;
    023    import org.apache.logging.log4j.core.util.Assert;
    024
    025    /**
    026     * Implementation of the {@code AppenderAdminMBean} interface.
    027     */
    028    public class AppenderAdmin implements AppenderAdminMBean {
    029
    030        private final String contextName;
    031        private final Appender appender;
    032        private final ObjectName objectName;
    033
    034        /**
    035         * Constructs a new {@code AppenderAdmin} with the specified contextName
    036         * and appender.
    037         *
    038         * @param contextName used in the {@code ObjectName} for this mbean
    039         * @param appender the instrumented object
    040         */
    041        public AppenderAdmin(final String contextName, final Appender appender) {
    042            // super(executor); // no notifications for now
    043            this.contextName = Assert.requireNonNull(contextName, "contextName");
    044            this.appender = Assert.requireNonNull(appender, "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 appender.getName();
    068        }
    069
    070        @Override
    071        public String getLayout() {
    072            return String.valueOf(appender.getLayout());
    073        }
    074
    075        @Override
    076        public boolean isIgnoreExceptions() {
    077            return appender.ignoreExceptions();
    078        }
    079
    080        @Override
    081        public String getErrorHandler() {
    082            return String.valueOf(appender.getHandler());
    083        }
    084
    085        @Override
    086        public String getFilter() {
    087            if (appender instanceof AbstractFilterable) {
    088                return String.valueOf(((AbstractFilterable) appender).getFilter());
    089            }
    090            return null;
    091        }
    092    }