001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2022, 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.model.processor; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.CoreConstants; 018import ch.qos.logback.core.hook.DefaultShutdownHook; 019import ch.qos.logback.core.hook.ShutdownHook; 020import ch.qos.logback.core.hook.ShutdownHookBase; 021import ch.qos.logback.core.model.Model; 022import ch.qos.logback.core.model.ShutdownHookModel; 023import ch.qos.logback.core.util.DynamicClassLoadingException; 024import ch.qos.logback.core.util.IncompatibleClassException; 025import ch.qos.logback.core.util.OptionHelper; 026 027public class ShutdownHookModelHandler extends ModelHandlerBase { 028 029 static final String OLD_SHUTDOWN_HOOK_CLASSNAME = "ch.qos.logback.core.hook.DelayingShutdownHook"; 030 static final String DEFAULT_SHUTDOWN_HOOK_CLASSNAME = DefaultShutdownHook.class.getName(); 031 static public final String RENAME_WARNING = OLD_SHUTDOWN_HOOK_CLASSNAME + " was renamed as "+ DEFAULT_SHUTDOWN_HOOK_CLASSNAME; 032 033 public ShutdownHookModelHandler(Context context) { 034 super(context); 035 } 036 boolean inError = false; 037 ShutdownHook hook = null; 038 039 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) { 040 return new ShutdownHookModelHandler(context); 041 } 042 043 @Override 044 protected Class<ShutdownHookModel> getSupportedModelClass() { 045 return ShutdownHookModel.class; 046 } 047 048 @Override 049 public void handle(ModelInterpretationContext mic, Model model) { 050 051 ShutdownHookModel shutdownHookModel = (ShutdownHookModel) model; 052 053 String className = shutdownHookModel.getClassName(); 054 if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) { 055 className = DEFAULT_SHUTDOWN_HOOK_CLASSNAME; 056 addInfo("Assuming className [" + className + "]"); 057 } else { 058 className = mic.getImport(className); 059 if(className.equals(OLD_SHUTDOWN_HOOK_CLASSNAME)) { 060 className = DEFAULT_SHUTDOWN_HOOK_CLASSNAME; 061 addWarn(RENAME_WARNING); 062 addWarn("Please use the new class name"); 063 } 064 } 065 066 addInfo("About to instantiate shutdown hook of type [" + className + "]"); 067 068 try { 069 hook = (ShutdownHookBase) OptionHelper.instantiateByClassName(className, ShutdownHookBase.class, context); 070 hook.setContext(context); 071 } catch (IncompatibleClassException | DynamicClassLoadingException e) { 072 addError("Could not create a shutdown hook of type [" + className + "].", e); 073 inError = true; 074 return; 075 } 076 077 mic.pushObject(hook); 078 } 079 080 @Override 081 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 082 if (inError) { 083 return; 084 } 085 Object o = mic.peekObject(); 086 087 if (o != hook) { 088 addWarn("The object on the top the of the stack is not the hook object pushed earlier."); 089 } else { 090 Thread hookThread = new Thread(hook, "Logback shutdown hook [" + context.getName() + "]"); 091 addInfo("Registering shutdown hook with JVM runtime."); 092 context.putObject(CoreConstants.SHUTDOWN_HOOK_THREAD, hookThread); 093 Runtime.getRuntime().addShutdownHook(hookThread); 094 095 mic.popObject(); 096 } 097 } 098}