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