View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.action;
15  
16  import java.util.Properties;
17  
18  import ch.qos.logback.core.joran.spi.InterpretationContext;
19  import ch.qos.logback.core.util.ContextUtil;
20  import ch.qos.logback.core.util.OptionHelper;
21  
22  public class ActionUtil {
23  
24      public enum Scope {
25          LOCAL, CONTEXT, SYSTEM
26      };
27  
28      /**
29       * Convert a string into a scope. Scole.LOCAL is returned by default.
30       * @param scopeStr
31       * @return a scope corresponding to the input string;  Scope.LOCAL by default.
32       */
33      static public Scope stringToScope(String scopeStr) {
34          if (Scope.SYSTEM.toString().equalsIgnoreCase(scopeStr))
35              return Scope.SYSTEM;
36          if (Scope.CONTEXT.toString().equalsIgnoreCase(scopeStr))
37              return Scope.CONTEXT;
38  
39          return Scope.LOCAL;
40      }
41  
42      static public void setProperty(InterpretationContext ic, String key, String value, Scope scope) {
43          switch (scope) {
44          case LOCAL:
45              ic.addSubstitutionProperty(key, value);
46              break;
47          case CONTEXT:
48              ic.getContext().putProperty(key, value);
49              break;
50          case SYSTEM:
51              OptionHelper.setSystemProperty(ic, key, value);
52          }
53      }
54  
55      /**
56       * Add all the properties found in the argument named 'props' to an
57       * InterpretationContext.
58       */
59      static public void setProperties(InterpretationContext ic, Properties props, Scope scope) {
60          switch (scope) {
61          case LOCAL:
62              ic.addSubstitutionProperties(props);
63              break;
64          case CONTEXT:
65              ContextUtil cu = new ContextUtil(ic.getContext());
66              cu.addProperties(props);
67              break;
68          case SYSTEM:
69              OptionHelper.setSystemProperties(ic, props);
70          }
71      }
72  
73  }