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 */
017package org.apache.logging.log4j.core.config;
018
019import java.util.Objects;
020
021import org.apache.logging.log4j.Logger;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
024import org.apache.logging.log4j.core.config.plugins.PluginFactory;
025import org.apache.logging.log4j.core.config.plugins.PluginValue;
026import org.apache.logging.log4j.status.StatusLogger;
027import org.apache.logging.log4j.util.Strings;
028
029/**
030 * Represents a key/value pair in the configuration.
031 */
032@Plugin(name = "property", category = Node.CATEGORY, printObject = true)
033public final class Property {
034
035    private static final Logger LOGGER = StatusLogger.getLogger();
036
037    private final String name;
038    private final String value;
039    private final boolean valueNeedsLookup;
040
041    private Property(final String name, final String value) {
042        this.name = name;
043        this.value = value;
044        this.valueNeedsLookup = value != null && value.contains("${");
045    }
046
047    /**
048     * Returns the property name.
049     * @return the property name.
050     */
051    public String getName() {
052        return name;
053    }
054
055    /**
056     * Returns the property value.
057     * @return the value of the property.
058     */
059    public String getValue() {
060        return Objects.toString(value, Strings.EMPTY); // LOG4J2-1313 null would be same as Property not existing
061    }
062
063    /**
064     * Returns {@code true} if the value contains a substitutable property that requires a lookup to be resolved.
065     * @return {@code true} if the value contains {@code "${"}, {@code false} otherwise
066     */
067    public boolean isValueNeedsLookup() {
068        return valueNeedsLookup;
069    }
070
071    /**
072     * Creates a Property.
073     *
074     * @param name The key.
075     * @param value The value.
076     * @return A Property.
077     */
078    @PluginFactory
079    public static Property createProperty(
080            @PluginAttribute("name") final String name,
081            @PluginValue("value") final String value) {
082        if (name == null) {
083            LOGGER.error("Property name cannot be null");
084        }
085        return new Property(name, value);
086    }
087
088    @Override
089    public String toString() {
090        return name + '=' + getValue();
091    }
092}