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