1 package info.magnolia.cms.beans.config; 2 3 import info.magnolia.cms.core.Content; 4 import info.magnolia.commands.MgnlRepositoryCatalog; 5 import info.magnolia.context.MgnlContext; 6 7 import java.text.MessageFormat ; 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import javax.servlet.ServletContextEvent ; 13 import javax.servlet.ServletContextListener ; 14 15 import org.apache.commons.chain.Catalog; 16 import org.apache.commons.chain.Command; 17 import org.apache.commons.chain.Context; 18 import org.slf4j.Logger; 19 import org.slf4j.LoggerFactory; 20 21 22 28 public class ShutdownManager extends ObservedManager implements ServletContextListener { 29 30 33 private static Logger log = LoggerFactory.getLogger(ShutdownManager.class); 34 35 38 private static List coreTasks = new ArrayList (); 39 40 43 private static List customTasks = new ArrayList (); 44 45 private static ShutdownManager instance = new ShutdownManager(); 46 47 public static ShutdownManager getInstance() { 48 return instance; 49 } 50 51 55 public static void addShutdownTask(ShutdownTask task) { 56 coreTasks.add(0, task); 57 } 58 59 63 public static List listShutdownTasks() { 64 List allTasks = new ArrayList (); 65 allTasks.addAll(coreTasks); 66 allTasks.addAll(0, customTasks); 67 return allTasks; 68 } 69 70 73 public void contextInitialized(ServletContextEvent sce) { 74 } 76 77 80 public void contextDestroyed(ServletContextEvent sce) { 81 82 log.info("Executing shutdown tasks"); 83 84 for (Iterator iter = listShutdownTasks().iterator(); iter.hasNext();) { 85 Command task = (Command) iter.next(); 86 Context c = MgnlContext.getSystemContext(); 87 try { 88 task.execute(c); 89 } 90 catch (Throwable e) { 91 log.warn(MessageFormat.format("Failed to execute shutdown task {0}: {1} {2}", new Object []{ 92 task, 93 e.getClass().getName(), 94 e.getMessage()})); 95 } 96 97 } 98 } 99 100 protected void onRegister(Content node) { 101 Catalog mrc = new MgnlRepositoryCatalog(node); 102 Iterator iter = mrc.getNames(); 103 while (iter.hasNext()) { 104 Object element = iter.next(); 105 log.info("Adding shutdown task:" + element.toString()); 106 customTasks.add(0, mrc.getCommand((String ) element)); } 108 109 } 110 111 protected void onClear() { 112 customTasks.clear(); 113 } 114 } 115 | Popular Tags |