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