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.util;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Objects;
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  /**
29   * A source for global configuration properties.
30   *
31   * @since 2.10.0
32   */
33  public interface PropertySource {
34  
35      /**
36       * Returns the order in which this PropertySource has priority. A higher value means that the source will be
37       * applied later so as to take precedence over other property sources.
38       *
39       * @return priority value
40       */
41      int getPriority();
42  
43      /**
44       * Iterates over all properties and performs an action for each key/value pair.
45       *
46       * @param action action to perform on each key/value pair
47       */
48      void forEach(BiConsumer<String, String> action);
49  
50      /**
51       * Converts a list of property name tokens into a normal form. For example, a list of tokens such as
52       * "foo", "bar", "baz", might be normalized into the property name "log4j2.fooBarBaz".
53       *
54       * @param tokens list of property name tokens
55       * @return a normalized property name using the given tokens
56       */
57      CharSequence getNormalForm(Iterable<? extends CharSequence> tokens);
58  
59      /**
60       * Comparator for ordering PropertySource instances by priority.
61       *
62       * @since 2.10.0
63       */
64      class Comparator implements java.util.Comparator<PropertySource>, Serializable {
65          private static final long serialVersionUID = 1L;
66  
67          @Override
68          public int compare(final PropertySource o1, final PropertySource o2) {
69              return Integer.compare(Objects.requireNonNull(o1).getPriority(), Objects.requireNonNull(o2).getPriority());
70          }
71      }
72  
73      /**
74       * Utility methods useful for PropertySource implementations.
75       *
76       * @since 2.10.0
77       */
78      final class Util {
79          private static final String PREFIXES = "(?i:^log4j2?[-._/]?|^org\\.apache\\.logging\\.log4j\\.)?";
80          private static final Pattern PROPERTY_TOKENIZER = Pattern.compile(PREFIXES + "([A-Z]*[a-z0-9]+|[A-Z0-9]+)[-._/]?");
81          private static final Map<CharSequence, List<CharSequence>> CACHE = new ConcurrentHashMap<>();
82  
83          /**
84           * Converts a property name string into a list of tokens. This will strip a prefix of {@code log4j},
85           * {@code log4j2}, {@code Log4j}, or {@code org.apache.logging.log4j}, along with separators of
86           * dash {@code -}, dot {@code .}, underscore {@code _}, and slash {@code /}. Tokens can also be separated
87           * by camel case conventions without needing a separator character in between.
88           *
89           * @param value property name
90           * @return the property broken into lower case tokens
91           */
92          public static List<CharSequence> tokenize(final CharSequence value) {
93              if (CACHE.containsKey(value)) {
94                  return CACHE.get(value);
95              }
96              List<CharSequence> tokens = new ArrayList<>();
97              final Matcher matcher = PROPERTY_TOKENIZER.matcher(value);
98              while (matcher.find()) {
99                  tokens.add(matcher.group(1).toLowerCase());
100             }
101             CACHE.put(value, tokens);
102             return tokens;
103         }
104 
105         /**
106          * Joins a list of strings using camelCaseConventions.
107          *
108          * @param tokens tokens to convert
109          * @return tokensAsCamelCase
110          */
111         public static CharSequence joinAsCamelCase(final Iterable<? extends CharSequence> tokens) {
112             final StringBuilder sb = new StringBuilder();
113             boolean first = true;
114             for (final CharSequence token : tokens) {
115                 if (first) {
116                     sb.append(token);
117                 } else {
118                     sb.append(Character.toUpperCase(token.charAt(0)));
119                     if (token.length() > 1) {
120                         sb.append(token.subSequence(1, token.length()));
121                     }
122                 }
123                 first = false;
124             }
125             return sb.toString();
126         }
127 
128         private Util() {
129         }
130     }
131 }