View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config;
18  
19  import java.util.Objects;
20  
21  import org.apache.logging.log4j.Logger;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  import org.apache.logging.log4j.core.config.plugins.PluginValue;
26  import org.apache.logging.log4j.status.StatusLogger;
27  import org.apache.logging.log4j.util.Strings;
28  
29  /**
30   * Represents a key/value pair in the configuration.
31   */
32  @Plugin(name = "property", category = Node.CATEGORY, printObject = true)
33  public final class Property {
34  
35      private static final Logger LOGGER = StatusLogger.getLogger();
36  
37      private final String name;
38      private final String value;
39      private final boolean valueNeedsLookup;
40  
41      private Property(final String name, final String value) {
42          this.name = name;
43          this.value = value;
44          this.valueNeedsLookup = value != null && value.contains("${");
45      }
46  
47      /**
48       * Returns the property name.
49       * @return the property name.
50       */
51      public String getName() {
52          return name;
53      }
54  
55      /**
56       * Returns the property value.
57       * @return the value of the property.
58       */
59      public String getValue() {
60          return Objects.toString(value, Strings.EMPTY); // LOG4J2-1313 null would be same as Property not existing
61      }
62  
63      /**
64       * Returns {@code true} if the value contains a substitutable property that requires a lookup to be resolved.
65       * @return {@code true} if the value contains {@code "${"}, {@code false} otherwise
66       */
67      public boolean isValueNeedsLookup() {
68          return valueNeedsLookup;
69      }
70  
71      /**
72       * Creates a Property.
73       *
74       * @param name The key.
75       * @param value The value.
76       * @return A Property.
77       */
78      @PluginFactory
79      public static Property createProperty(
80              @PluginAttribute("name") final String name,
81              @PluginValue("value") final String value) {
82          if (name == null) {
83              LOGGER.error("Property name cannot be null");
84          }
85          return new Property(name, value);
86      }
87  
88      @Override
89      public String toString() {
90          return name + '=' + getValue();
91      }
92  }