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 018 package org.apache.logging.log4j.core.config.plugins.visitors; 019 020 import java.util.Map; 021 022 import org.apache.logging.log4j.core.LogEvent; 023 import org.apache.logging.log4j.core.config.Configuration; 024 import org.apache.logging.log4j.core.config.Node; 025 import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 026 import org.apache.logging.log4j.core.util.NameUtil; 027 028 /** 029 * PluginVisitor implementation for {@link PluginAttribute}. 030 */ 031 public class PluginAttributeVisitor extends AbstractPluginVisitor<PluginAttribute> { 032 public PluginAttributeVisitor() { 033 super(PluginAttribute.class); 034 } 035 036 @Override 037 public Object visit(final Configuration configuration, final Node node, final LogEvent event, 038 final StringBuilder log) { 039 final String name = this.annotation.value(); 040 final Map<String, String> attributes = node.getAttributes(); 041 final String rawValue = removeAttributeValue(attributes, name, this.aliases); 042 final String replacedValue = this.substitutor.replace(event, rawValue); 043 final Object defaultValue = findDefaultValue(event); 044 final Object value = convert(replacedValue, defaultValue); 045 final Object debugValue = this.annotation.sensitive() ? NameUtil.md5(value + this.getClass().getName()) : value; 046 log.append(name).append("=\"").append(debugValue).append('"'); 047 return value; 048 } 049 050 private Object findDefaultValue(final LogEvent event) { 051 if (this.conversionType == int.class || this.conversionType == Integer.class) { 052 return this.annotation.defaultInt(); 053 } 054 if (this.conversionType == long.class || this.conversionType == Long.class) { 055 return this.annotation.defaultLong(); 056 } 057 if (this.conversionType == boolean.class || this.conversionType == Boolean.class) { 058 return this.annotation.defaultBoolean(); 059 } 060 if (this.conversionType == float.class || this.conversionType == Float.class) { 061 return this.annotation.defaultFloat(); 062 } 063 if (this.conversionType == double.class || this.conversionType == Double.class) { 064 return this.annotation.defaultDouble(); 065 } 066 if (this.conversionType == byte.class || this.conversionType == Byte.class) { 067 return this.annotation.defaultByte(); 068 } 069 if (this.conversionType == char.class || this.conversionType == Character.class) { 070 return this.annotation.defaultChar(); 071 } 072 if (this.conversionType == short.class || this.conversionType == Short.class) { 073 return this.annotation.defaultShort(); 074 } 075 if (this.conversionType == Class.class) { 076 return this.annotation.defaultClass(); 077 } 078 return this.substitutor.replace(event, this.annotation.defaultString()); 079 } 080 }