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.CoreConstants;
26  import ch.qos.logback.core.hook.ShutdownHook;
27  import ch.qos.logback.core.rolling.helper.FileNamePattern;
28  import ch.qos.logback.core.spi.ContextAwareBase;
29  
30  public class ContextUtil extends ContextAwareBase {
31  
32      static final String GROOVY_RUNTIME_PACKAGE = "org.codehaus.groovy.runtime";
33      // static final String SYSTEM_LOGGER_FQCN = "java.lang.System$Logger";
34  
35      public ContextUtil(Context context) {
36          setContext(context);
37      }
38  
39      public void addProperties(Properties props) {
40          if (props == null) {
41              return;
42          }
43  
44          for (Entry<Object, Object> e : props.entrySet()) {
45              String key = (String) e.getKey();
46              context.putProperty(key, (String) e.getValue());
47          }
48  
49      }
50  
51      public static Map<String, String> getFilenameCollisionMap(Context context) {
52          if (context == null)
53              return null;
54          @SuppressWarnings("unchecked")
55          Map<String, String> map = (Map<String, String>) context.getObject(FA_FILENAME_COLLISION_MAP);
56          return map;
57      }
58  
59      public static Map<String, FileNamePattern> getFilenamePatternCollisionMap(Context context) {
60          if (context == null)
61              return null;
62          @SuppressWarnings("unchecked")
63          Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context
64                  .getObject(RFA_FILENAME_PATTERN_COLLISION_MAP);
65          return map;
66      }
67  
68      public void addGroovyPackages(List<String> frameworkPackages) {
69          addFrameworkPackage(frameworkPackages, GROOVY_RUNTIME_PACKAGE);
70      }
71  
72      public void addFrameworkPackage(List<String> frameworkPackages, String packageName) {
73          if (!frameworkPackages.contains(packageName)) {
74              frameworkPackages.add(packageName);
75          }
76      }
77  
78      /**
79       * Add a shutdown hook thread with the JVM runtime.
80       *
81       * If a previous shutdown hook thread was registered, it is replaced.
82       * @param hook
83       * @since 1.5.7
84       */
85      public void addOrReplaceShutdownHook(ShutdownHook hook) {
86          Runtime runtime = Runtime.getRuntime();
87  
88          Thread oldShutdownHookTread = (Thread) context.getObject(CoreConstants.SHUTDOWN_HOOK_THREAD);
89          if(oldShutdownHookTread != null) {
90              addInfo("Removing old shutdown hook from JVM runtime");
91              runtime.removeShutdownHook(oldShutdownHookTread);
92          }
93  
94          Thread hookThread = new Thread(hook, "Logback shutdown hook [" + context.getName() + "]");
95          addInfo("Registering shutdown hook with JVM runtime.");
96          context.putObject(CoreConstants.SHUTDOWN_HOOK_THREAD, hookThread);
97          runtime.addShutdownHook(hookThread);
98  
99      }
100 
101 }