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