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.util;
15  
16  import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP;
17  import static ch.qos.logback.core.CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP;
18  
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  import java.util.Properties;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.rolling.helper.FileNamePattern;
26  import ch.qos.logback.core.spi.ContextAwareBase;
27  
28  public class ContextUtil extends ContextAwareBase {
29  
30      static final String GROOVY_RUNTIME_PACKAGE = "org.codehaus.groovy.runtime";
31      // static final String SYSTEM_LOGGER_FQCN = "java.lang.System$Logger";
32  
33      public ContextUtil(Context context) {
34          setContext(context);
35      }
36  
37      public void addProperties(Properties props) {
38          if (props == null) {
39              return;
40          }
41  
42          for (Entry<Object, Object> e : props.entrySet()) {
43              String key = (String) e.getKey();
44              context.putProperty(key, (String) e.getValue());
45          }
46  
47      }
48  
49      public static Map<String, String> getFilenameCollisionMap(Context context) {
50          if (context == null)
51              return null;
52          @SuppressWarnings("unchecked")
53          Map<String, String> map = (Map<String, String>) context.getObject(FA_FILENAME_COLLISION_MAP);
54          return map;
55      }
56  
57      public static Map<String, FileNamePattern> getFilenamePatternCollisionMap(Context context) {
58          if (context == null)
59              return null;
60          @SuppressWarnings("unchecked")
61          Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context
62                  .getObject(RFA_FILENAME_PATTERN_COLLISION_MAP);
63          return map;
64      }
65  
66      public void addGroovyPackages(List<String> frameworkPackages) {
67          addFrameworkPackage(frameworkPackages, GROOVY_RUNTIME_PACKAGE);
68      }
69  
70      public void addFrameworkPackage(List<String> frameworkPackages, String packageName) {
71          if (!frameworkPackages.contains(packageName)) {
72              frameworkPackages.add(packageName);
73          }
74      }
75  
76  }