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      /**
36       * @since 2.11.2
37       */
38      public static final Property[] EMPTY_ARRAY = new Property[0];
39  
40      private static final Logger LOGGER = StatusLogger.getLogger();
41  
42      private final String name;
43      private final String value;
44      private final boolean valueNeedsLookup;
45  
46      private Property(final String name, final String value) {
47          this.name = name;
48          this.value = value;
49          this.valueNeedsLookup = value != null && value.contains("${");
50      }
51  
52      /**
53       * Returns the property name.
54       * @return the property name.
55       */
56      public String getName() {
57          return name;
58      }
59  
60      /**
61       * Returns the property value.
62       * @return the value of the property.
63       */
64      public String getValue() {
65          return Objects.toString(value, Strings.EMPTY); // LOG4J2-1313 null would be same as Property not existing
66      }
67  
68      /**
69       * Returns {@code true} if the value contains a substitutable property that requires a lookup to be resolved.
70       * @return {@code true} if the value contains {@code "${"}, {@code false} otherwise
71       */
72      public boolean isValueNeedsLookup() {
73          return valueNeedsLookup;
74      }
75  
76      /**
77       * Creates a Property.
78       *
79       * @param name The key.
80       * @param value The value.
81       * @return A Property.
82       */
83      @PluginFactory
84      public static Property createProperty(
85              @PluginAttribute("name") final String name,
86              @PluginValue("value") final String value) {
87          if (name == null) {
88              LOGGER.error("Property name cannot be null");
89          }
90          return new Property(name, value);
91      }
92  
93      @Override
94      public String toString() {
95          return name + '=' + getValue();
96      }
97  }