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    /**
036     * @since 2.11.2
037     */
038    public static final Property[] EMPTY_ARRAY = new Property[0];
039
040    private static final Logger LOGGER = StatusLogger.getLogger();
041
042    private final String name;
043    private final String value;
044    private final boolean valueNeedsLookup;
045
046    private Property(final String name, final String value) {
047        this.name = name;
048        this.value = value;
049        this.valueNeedsLookup = value != null && value.contains("${");
050    }
051
052    /**
053     * Returns the property name.
054     * @return the property name.
055     */
056    public String getName() {
057        return name;
058    }
059
060    /**
061     * Returns the property value.
062     * @return the value of the property.
063     */
064    public String getValue() {
065        return Objects.toString(value, Strings.EMPTY); // LOG4J2-1313 null would be same as Property not existing
066    }
067
068    /**
069     * Returns {@code true} if the value contains a substitutable property that requires a lookup to be resolved.
070     * @return {@code true} if the value contains {@code "${"}, {@code false} otherwise
071     */
072    public boolean isValueNeedsLookup() {
073        return valueNeedsLookup;
074    }
075
076    /**
077     * Creates a Property.
078     *
079     * @param name The key.
080     * @param value The value.
081     * @return A Property.
082     */
083    @PluginFactory
084    public static Property createProperty(
085            @PluginAttribute("name") final String name,
086            @PluginValue("value") final String value) {
087        if (name == null) {
088            LOGGER.error("Property name cannot be null");
089        }
090        return new Property(name, value);
091    }
092
093    @Override
094    public String toString() {
095        return name + '=' + getValue();
096    }
097}