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 java.util.List;
020    
021    import javax.management.ObjectName;
022    
023    import org.apache.logging.log4j.Level;
024    import org.apache.logging.log4j.core.config.AppenderRef;
025    import org.apache.logging.log4j.core.config.LoggerConfig;
026    import org.apache.logging.log4j.core.helpers.Assert;
027    
028    /**
029     * Implementation of the {@code LoggerConfigAdminMBean} interface.
030     */
031    public class LoggerConfigAdmin implements LoggerConfigAdminMBean {
032    
033        private final String contextName;
034        private final LoggerConfig loggerConfig;
035        private final ObjectName objectName;
036    
037        /**
038         * Constructs a new {@code LoggerConfigAdmin} with the specified contextName
039         * and logger config.
040         * 
041         * @param contextName used in the {@code ObjectName} for this mbean
042         * @param loggerConfig the instrumented object
043         */
044        public LoggerConfigAdmin(String contextName, LoggerConfig loggerConfig) {
045            // super(executor); // no notifications for now
046            this.contextName = Assert.isNotNull(contextName, "contextName");
047            this.loggerConfig = Assert.isNotNull(loggerConfig, "loggerConfig");
048            try {
049                String ctxName = Server.escape(this.contextName);
050                String configName = Server.escape(loggerConfig.getName());
051                String name = String.format(PATTERN, ctxName, configName);
052                objectName = new ObjectName(name);
053            } catch (Exception e) {
054                throw new IllegalStateException(e);
055            }
056        }
057    
058        /**
059         * Returns the {@code ObjectName} of this mbean.
060         * 
061         * @return the {@code ObjectName}
062         * @see LoggerConfigAdminMBean#PATTERN
063         */
064        public ObjectName getObjectName() {
065            return objectName;
066        }
067    
068        @Override
069        public String getName() {
070            return loggerConfig.getName();
071        }
072    
073        @Override
074        public String getLevel() {
075            return loggerConfig.getLevel().name();
076        }
077    
078        @Override
079        public void setLevel(String level) {
080            loggerConfig.setLevel(Level.valueOf(level));
081        }
082    
083        @Override
084        public boolean isAdditive() {
085            return loggerConfig.isAdditive();
086        }
087    
088        @Override
089        public void setAdditive(boolean additive) {
090            loggerConfig.setAdditive(additive);
091        }
092    
093        @Override
094        public boolean isIncludeLocation() {
095            return loggerConfig.isIncludeLocation();
096        }
097    
098        @Override
099        public String getFilter() {
100            return String.valueOf(loggerConfig.getFilter());
101        }
102    
103        @Override
104        public String[] getAppenderRefs() {
105            List<AppenderRef> refs = loggerConfig.getAppenderRefs();
106            String[] result = new String[refs.size()];
107            for (int i = 0; i < result.length; i++) {
108                result[i] = refs.get(i).getRef();
109            }
110            return result;
111        }
112    }