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.PluginBuilderAttribute;
026import org.apache.logging.log4j.util.StringBuilders;
027
028/**
029 * PluginVisitor for PluginBuilderAttribute. If {@code null} is returned for the
030 * {@link #visit(org.apache.logging.log4j.core.config.Configuration, org.apache.logging.log4j.core.config.Node, org.apache.logging.log4j.core.LogEvent, StringBuilder)}
031 * method, then the default value of the field should remain untouched.
032 *
033 * @see org.apache.logging.log4j.core.config.plugins.util.PluginBuilder
034 */
035public class PluginBuilderAttributeVisitor extends AbstractPluginVisitor<PluginBuilderAttribute> {
036
037    public PluginBuilderAttributeVisitor() {
038        super(PluginBuilderAttribute.class);
039    }
040
041    @Override
042    public Object visit(final Configuration configuration, final Node node, final LogEvent event,
043                        final StringBuilder log) {
044        final String overridden = this.annotation.value();
045        final String name = overridden.isEmpty() ? this.member.getName() : overridden;
046        final Map<String, String> attributes = node.getAttributes();
047        final String rawValue = removeAttributeValue(attributes, name, this.aliases);
048        final String replacedValue = this.substitutor.replace(event, rawValue);
049        final Object value = convert(replacedValue, null);
050        final Object debugValue = this.annotation.sensitive() ? "*****" : value;
051        StringBuilders.appendKeyDqValue(log, name, debugValue);
052        return value;
053    }
054}