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.helpers.Assert;
023    
024    /**
025     * Implementation of the {@code AppenderAdminMBean} interface.
026     */
027    public class AppenderAdmin implements AppenderAdminMBean {
028    
029        private final String contextName;
030        private final Appender<?> appender;
031        private final ObjectName objectName;
032    
033        /**
034         * Constructs a new {@code AppenderAdmin} with the specified contextName
035         * and appender.
036         * 
037         * @param contextName used in the {@code ObjectName} for this mbean
038         * @param appender the instrumented object
039         */
040        public AppenderAdmin(String contextName, Appender<?> appender) {
041            // super(executor); // no notifications for now
042            this.contextName = Assert.isNotNull(contextName, "contextName");
043            this.appender = Assert.isNotNull(appender, "appender");
044            try {
045                String ctxName = Server.escape(this.contextName);
046                String configName = Server.escape(appender.getName());
047                String name = String.format(PATTERN, ctxName, configName);
048                objectName = new ObjectName(name);
049            } catch (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 appender.getName();
067        }
068        
069        @Override
070        public String getLayout() {
071            return String.valueOf(appender.getLayout());
072        }
073    
074        @Override
075        public boolean isExceptionSuppressed() {
076            return appender.isExceptionSuppressed();
077        }
078        
079        @Override
080        public String getErrorHandler() {
081            return String.valueOf(appender.getHandler());
082        }
083    }