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    
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     */
035    public 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() ? NameUtil.md5(value + this.getClass().getName()) : value;
051            log.append(name).append("=\"").append(debugValue).append('"');
052            return value;
053        }
054    }