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    
023    /**
024     * Implementation of the {@code AppenderAdminMBean} interface.
025     */
026    public class AppenderAdmin implements AppenderAdminMBean {
027    
028        private final String contextName;
029        private final Appender<?> appender;
030        private final ObjectName objectName;
031    
032        /**
033         * Constructs a new {@code AppenderAdmin} with the specified contextName
034         * and appender.
035         * 
036         * @param contextName used in the {@code ObjectName} for this mbean
037         * @param appender the instrumented object
038         */
039        public AppenderAdmin(String contextName, Appender<?> appender) {
040            // super(executor); // no notifications for now
041            this.contextName = Assert.isNotNull(contextName, "contextName");
042            this.appender = Assert.isNotNull(appender, "appender");
043            try {
044                String ctxName = Server.escape(this.contextName);
045                String configName = Server.escape(appender.getName());
046                String name = String.format(PATTERN, ctxName, configName);
047                objectName = new ObjectName(name);
048            } catch (Exception e) {
049                throw new IllegalStateException(e);
050            }
051        }
052    
053        /** @see AppenderAdminMBean#PATTERN */
054        public ObjectName getObjectName() {
055            return objectName;
056        }
057    
058        @Override
059        public String getName() {
060            return appender.getName();
061        }
062        
063        @Override
064        public String getLayout() {
065            return String.valueOf(appender.getLayout());
066        }
067    
068        @Override
069        public boolean isExceptionSuppressed() {
070            return appender.isExceptionSuppressed();
071        }
072        
073        @Override
074        public String getErrorHandler() {
075            return String.valueOf(appender.getHandler());
076        }
077    }