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.util;
18  
19  import java.io.InterruptedIOException;
20  import java.util.Locale;
21  import java.util.Properties;
22  
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.status.StatusLogger;
26  import org.apache.logging.log4j.util.PropertiesUtil;
27  import org.apache.logging.log4j.util.Strings;
28  
29  /**
30   * A convenience class to convert property values to specific types.
31   */
32  public final class OptionConverter {
33  
34      private static final Logger LOGGER = StatusLogger.getLogger();
35  
36      private static final String DELIM_START = "${";
37      private static final char DELIM_STOP = '}';
38      private static final int DELIM_START_LEN = 2;
39      private static final int DELIM_STOP_LEN = 1;
40      private static final int ONE_K = 1024;
41  
42      /**
43       * OptionConverter is a static class.
44       */
45      private OptionConverter() {
46      }
47  
48      public static String[] concatenateArrays(final String[] l, final String[] r) {
49          final int len = l.length + r.length;
50          final String[] a = new String[len];
51  
52          System.arraycopy(l, 0, a, 0, l.length);
53          System.arraycopy(r, 0, a, l.length, r.length);
54  
55          return a;
56      }
57  
58      public static String convertSpecialChars(final String s) {
59          char c;
60          final int len = s.length();
61          final StringBuilder sbuf = new StringBuilder(len);
62  
63          int i = 0;
64          while (i < len) {
65              c = s.charAt(i++);
66              if (c == '\\') {
67                  c = s.charAt(i++);
68                  switch (c) {
69                  case 'n':
70                      c = '\n';
71                      break;
72                  case 'r':
73                      c = '\r';
74                      break;
75                  case 't':
76                      c = '\t';
77                      break;
78                  case 'f':
79                      c = '\f';
80                      break;
81                  case 'b':
82                      c = '\b';
83                      break;
84                  case '"':
85                      c = '\"';
86                      break;
87                  case '\'':
88                      c = '\'';
89                      break;
90                  case '\\':
91                      c = '\\';
92                      break;
93                  default:
94                      // there is no default case.
95                  }
96              }
97              sbuf.append(c);
98          }
99          return sbuf.toString();
100     }
101 
102     public static Object instantiateByKey(final Properties props, final String key, final Class<?> superClass,
103                                    final Object defaultValue) {
104 
105         // Get the value of the property in string form
106         final String className = findAndSubst(key, props);
107         if (className == null) {
108             LOGGER.error("Could not find value for key {}", key);
109             return defaultValue;
110         }
111         // Trim className to avoid trailing spaces that cause problems.
112         return OptionConverter.instantiateByClassName(className.trim(), superClass,
113             defaultValue);
114     }
115 
116     /**
117      * If <code>value</code> is "true", then {@code true} is
118      * returned. If <code>value</code> is "false", then
119      * {@code false} is returned. Otherwise, <code>default</code> is
120      * returned.
121      *
122      * <p>Case of value is unimportant.</p>
123      * @param value The value to convert.
124      * @param defaultValue The default value.
125      * @return true or false, depending on the value and/or default.
126      */
127     public static boolean toBoolean(final String value, final boolean defaultValue) {
128         if (value == null) {
129             return defaultValue;
130         }
131         final String trimmedVal = value.trim();
132         if ("true".equalsIgnoreCase(trimmedVal)) {
133             return true;
134         }
135         if ("false".equalsIgnoreCase(trimmedVal)) {
136             return false;
137         }
138         return defaultValue;
139     }
140 
141     /**
142      * Convert the String value to an int.
143      * @param value The value as a String.
144      * @param defaultValue The default value.
145      * @return The value as an int.
146      */
147     public static int toInt(final String value, final int defaultValue) {
148         if (value != null) {
149             final String s = value.trim();
150             try {
151                 return Integer.parseInt(s);
152             } catch (final NumberFormatException e) {
153                 LOGGER.error("[{}] is not in proper int form.", s, e);
154             }
155         }
156         return defaultValue;
157     }
158 
159     public static Level toLevel(String value, Level defaultValue) {
160         if(value == null) {
161             return defaultValue;
162         }
163 
164         value = value.trim();
165 
166         int hashIndex = value.indexOf('#');
167         if (hashIndex == -1) {
168             if("NULL".equalsIgnoreCase(value)) {
169                 return null;
170             } else {
171                 // no class name specified : use standard Level class
172                 return Level.toLevel(value, defaultValue);
173             }
174         }
175 
176         Level result = defaultValue;
177 
178         String clazz = value.substring(hashIndex+1);
179         String levelName = value.substring(0, hashIndex);
180 
181         // This is degenerate case but you never know.
182         if("NULL".equalsIgnoreCase(levelName)) {
183             return null;
184         }
185 
186         LOGGER.debug("toLevel" + ":class=[" + clazz + "]"
187             + ":pri=[" + levelName + "]");
188 
189         try {
190             Class customLevel = Loader.loadClass(clazz);
191 
192             // get a ref to the specified class' static method
193             // toLevel(String, org.apache.log4j.Level)
194             Class[] paramTypes = new Class[] { String.class, Level.class
195             };
196             java.lang.reflect.Method toLevelMethod =
197                 customLevel.getMethod("toLevel", paramTypes);
198 
199             // now call the toLevel method, passing level string + default
200             Object[] params = new Object[] {levelName, defaultValue};
201             Object o = toLevelMethod.invoke(null, params);
202 
203             result = (Level) o;
204         } catch(ClassNotFoundException e) {
205             LOGGER.warn("custom level class [" + clazz + "] not found.");
206         } catch(NoSuchMethodException e) {
207             LOGGER.warn("custom level class [" + clazz + "]"
208                 + " does not have a class function toLevel(String, Level)", e);
209         } catch(java.lang.reflect.InvocationTargetException e) {
210             if (e.getTargetException() instanceof InterruptedException
211                 || e.getTargetException() instanceof InterruptedIOException) {
212                 Thread.currentThread().interrupt();
213             }
214             LOGGER.warn("custom level class [" + clazz + "]" + " could not be instantiated", e);
215         } catch(ClassCastException e) {
216             LOGGER.warn("class [" + clazz + "] is not a subclass of org.apache.log4j.Level", e);
217         } catch(IllegalAccessException e) {
218             LOGGER.warn("class ["+clazz+ "] cannot be instantiated due to access restrictions", e);
219         } catch(RuntimeException e) {
220             LOGGER.warn("class ["+clazz+"], level [" + levelName + "] conversion failed.", e);
221         }
222         return result;
223     }
224 
225     /**
226      *
227      * @param value The size of the file as a String.
228      * @param defaultValue The default value.
229      * @return The size of the file as a long.
230      */
231     public static long toFileSize(final String value, final long defaultValue) {
232         if (value == null) {
233             return defaultValue;
234         }
235 
236         String str = value.trim().toUpperCase(Locale.ENGLISH);
237         long multiplier = 1;
238         int index;
239 
240         if ((index = str.indexOf("KB")) != -1) {
241             multiplier = ONE_K;
242             str = str.substring(0, index);
243         } else if ((index = str.indexOf("MB")) != -1) {
244             multiplier = ONE_K * ONE_K;
245             str = str.substring(0, index);
246         } else if ((index = str.indexOf("GB")) != -1) {
247             multiplier = ONE_K * ONE_K * ONE_K;
248             str = str.substring(0, index);
249         }
250         try {
251             return Long.parseLong(str) * multiplier;
252         } catch (final NumberFormatException e) {
253             LOGGER.error("[{}] is not in proper int form.", str);
254             LOGGER.error("[{}] not in expected format.", value, e);
255         }
256         return defaultValue;
257     }
258 
259     /**
260      * Find the value corresponding to <code>key</code> in
261      * <code>props</code>. Then perform variable substitution on the
262      * found value.
263      * @param key The key to locate.
264      * @param props The properties.
265      * @return The String after substitution.
266      */
267     public static String findAndSubst(final String key, final Properties props) {
268         final String value = props.getProperty(key);
269         if (value == null) {
270             return null;
271         }
272 
273         try {
274             return substVars(value, props);
275         } catch (final IllegalArgumentException e) {
276             LOGGER.error("Bad option value [{}].", value, e);
277             return value;
278         }
279     }
280 
281     /**
282      * Instantiate an object given a class name. Check that the
283      * <code>className</code> is a subclass of
284      * <code>superClass</code>. If that test fails or the object could
285      * not be instantiated, then <code>defaultValue</code> is returned.
286      *
287      * @param className    The fully qualified class name of the object to instantiate.
288      * @param superClass   The class to which the new object should belong.
289      * @param defaultValue The object to return in case of non-fulfillment
290      * @return The created object.
291      */
292     public static Object instantiateByClassName(final String className, final Class<?> superClass,
293                                          final Object defaultValue) {
294         if (className != null) {
295             try {
296                 final Class<?> classObj = Loader.loadClass(className);
297                 if (!superClass.isAssignableFrom(classObj)) {
298                     LOGGER.error("A \"{}\" object is not assignable to a \"{}\" variable.", className,
299                         superClass.getName());
300                     LOGGER.error("The class \"{}\" was loaded by [{}] whereas object of type [{}] was loaded by [{}].",
301                         superClass.getName(), superClass.getClassLoader(), classObj.getName());
302                     return defaultValue;
303                 }
304                 return classObj.newInstance();
305             } catch (final Exception e) {
306                 LOGGER.error("Could not instantiate class [{}].", className, e);
307             }
308         }
309         return defaultValue;
310     }
311 
312 
313     /**
314      * Perform variable substitution in string <code>val</code> from the
315      * values of keys found in the system propeties.
316      *
317      * <p>The variable substitution delimiters are <b>${</b> and <b>}</b>.</p>
318      *
319      * <p>For example, if the System properties contains "key=value", then
320      * the call</p>
321      * <pre>
322      * String s = OptionConverter.substituteVars("Value of key is ${key}.");
323      * </pre>
324      * <p>
325      * will set the variable <code>s</code> to "Value of key is value.".
326      * </p>
327      * <p>If no value could be found for the specified key, then the
328      * <code>props</code> parameter is searched, if the value could not
329      * be found there, then substitution defaults to the empty string.</p>
330      *
331      * <p>For example, if system properties contains no value for the key
332      * "inexistentKey", then the call
333      * </p>
334      * <pre>
335      * String s = OptionConverter.subsVars("Value of inexistentKey is [${inexistentKey}]");
336      * </pre>
337      * <p>
338      * will set <code>s</code> to "Value of inexistentKey is []"
339      * </p>
340      * <p>An {@link java.lang.IllegalArgumentException} is thrown if
341      * <code>val</code> contains a start delimeter "${" which is not
342      * balanced by a stop delimeter "}". </p>
343      *
344      * @param val The string on which variable substitution is performed.
345      * @param props The properties to use for substitution.
346      * @return The String after substitution.
347      * @throws IllegalArgumentException if <code>val</code> is malformed.
348      */
349     public static String substVars(final String val, final Properties props) throws
350         IllegalArgumentException {
351 
352         final StringBuilder sbuf = new StringBuilder();
353 
354         int i = 0;
355         int j;
356         int k;
357 
358         while (true) {
359             j = val.indexOf(DELIM_START, i);
360             if (j == -1) {
361                 // no more variables
362                 if (i == 0) { // this is a simple string
363                     return val;
364                 }
365                 // add the tail string which contails no variables and return the result.
366                 sbuf.append(val.substring(i, val.length()));
367                 return sbuf.toString();
368             }
369             sbuf.append(val.substring(i, j));
370             k = val.indexOf(DELIM_STOP, j);
371             if (k == -1) {
372                 throw new IllegalArgumentException(Strings.dquote(val)
373                     + " has no closing brace. Opening brace at position " + j
374                     + '.');
375             }
376             j += DELIM_START_LEN;
377             final String key = val.substring(j, k);
378             // first try in System properties
379             String replacement = PropertiesUtil.getProperties().getStringProperty(key, null);
380             // then try props parameter
381             if (replacement == null && props != null) {
382                 replacement = props.getProperty(key);
383             }
384 
385             if (replacement != null) {
386                 // Do variable substitution on the replacement string
387                 // such that we can solve "Hello ${x2}" as "Hello p1"
388                 // the where the properties are
389                 // x1=p1
390                 // x2=${x1}
391                 final String recursiveReplacement = substVars(replacement, props);
392                 sbuf.append(recursiveReplacement);
393             }
394             i = k + DELIM_STOP_LEN;
395         }
396     }
397 }