001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.util; 015 016import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP; 017import static ch.qos.logback.core.CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP; 018 019import java.util.List; 020import java.util.Map; 021import java.util.Map.Entry; 022import java.util.Properties; 023 024import ch.qos.logback.core.Context; 025import ch.qos.logback.core.CoreConstants; 026import ch.qos.logback.core.hook.ShutdownHook; 027import ch.qos.logback.core.rolling.helper.FileNamePattern; 028import ch.qos.logback.core.spi.ContextAwareBase; 029 030public class ContextUtil extends ContextAwareBase { 031 032 static final String GROOVY_RUNTIME_PACKAGE = "org.codehaus.groovy.runtime"; 033 // static final String SYSTEM_LOGGER_FQCN = "java.lang.System$Logger"; 034 035 public ContextUtil(Context context) { 036 setContext(context); 037 } 038 039 public void addProperties(Properties props) { 040 if (props == null) { 041 return; 042 } 043 044 for (Entry<Object, Object> e : props.entrySet()) { 045 String key = (String) e.getKey(); 046 context.putProperty(key, (String) e.getValue()); 047 } 048 049 } 050 051 public static Map<String, String> getFilenameCollisionMap(Context context) { 052 if (context == null) 053 return null; 054 @SuppressWarnings("unchecked") 055 Map<String, String> map = (Map<String, String>) context.getObject(FA_FILENAME_COLLISION_MAP); 056 return map; 057 } 058 059 public static Map<String, FileNamePattern> getFilenamePatternCollisionMap(Context context) { 060 if (context == null) 061 return null; 062 @SuppressWarnings("unchecked") 063 Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context 064 .getObject(RFA_FILENAME_PATTERN_COLLISION_MAP); 065 return map; 066 } 067 068 public void addGroovyPackages(List<String> frameworkPackages) { 069 addFrameworkPackage(frameworkPackages, GROOVY_RUNTIME_PACKAGE); 070 } 071 072 public void addFrameworkPackage(List<String> frameworkPackages, String packageName) { 073 if (!frameworkPackages.contains(packageName)) { 074 frameworkPackages.add(packageName); 075 } 076 } 077 078 /** 079 * Add a shutdown hook thread with the JVM runtime. 080 * 081 * If a previous shutdown hook thread was registered, it is replaced. 082 * @param hook 083 * @since 1.5.7 084 */ 085 public void addOrReplaceShutdownHook(ShutdownHook hook) { 086 Runtime runtime = Runtime.getRuntime(); 087 088 Thread oldShutdownHookTread = (Thread) context.getObject(CoreConstants.SHUTDOWN_HOOK_THREAD); 089 if(oldShutdownHookTread != null) { 090 addInfo("Removing old shutdown hook from JVM runtime"); 091 runtime.removeShutdownHook(oldShutdownHookTread); 092 } 093 094 Thread hookThread = new Thread(hook, "Logback shutdown hook [" + context.getName() + "]"); 095 addInfo("Registering shutdown hook with JVM runtime."); 096 context.putObject(CoreConstants.SHUTDOWN_HOOK_THREAD, hookThread); 097 runtime.addShutdownHook(hookThread); 098 099 } 100 101}