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