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 027 /** 028 * Implementation of the {@code LoggerConfigAdminMBean} interface. 029 */ 030 public 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(String contextName, 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 String ctxName = Server.escape(this.contextName); 049 String configName = Server.escape(loggerConfig.getName()); 050 String name = String.format(PATTERN, ctxName, configName); 051 objectName = new ObjectName(name); 052 } catch (Exception e) { 053 throw new IllegalStateException(e); 054 } 055 } 056 057 /** @see LoggerConfigAdminMBean#PATTERN */ 058 public ObjectName getObjectName() { 059 return objectName; 060 } 061 062 @Override 063 public String getName() { 064 return loggerConfig.getName(); 065 } 066 067 @Override 068 public String getLevel() { 069 return loggerConfig.getLevel().name(); 070 } 071 072 @Override 073 public void setLevel(String level) { 074 loggerConfig.setLevel(Level.valueOf(level)); 075 } 076 077 @Override 078 public boolean isAdditive() { 079 return loggerConfig.isAdditive(); 080 } 081 082 @Override 083 public void setAdditive(boolean additive) { 084 loggerConfig.setAdditive(additive); 085 } 086 087 @Override 088 public boolean isIncludeLocation() { 089 return loggerConfig.isIncludeLocation(); 090 } 091 092 @Override 093 public String getFilter() { 094 return String.valueOf(loggerConfig.getFilter()); 095 } 096 097 @Override 098 public String[] getAppenderRefs() { 099 List<AppenderRef> refs = loggerConfig.getAppenderRefs(); 100 String[] result = new String[refs.size()]; 101 for (int i = 0; i < result.length; i++) { 102 result[i] = refs.get(i).getRef(); 103 } 104 return result; 105 } 106 }