KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > ShutdownManager


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 JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import javax.servlet.ServletContextEvent JavaDoc;
13 import javax.servlet.ServletContextListener JavaDoc;
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 /**
23  * Handles shutdown coreTasks supplied by Providers. Can be used to cleanly shutdown repositories while stopping the web
24  * application.
25  * @author Fabrizio Giustina
26  * @version $Id: ShutdownManager.java 6341 2006-09-12 09:18:27Z philipp $
27  */

28 public class ShutdownManager extends ObservedManager implements ServletContextListener JavaDoc {
29
30     /**
31      * Logger.
32      */

33     private static Logger log = LoggerFactory.getLogger(ShutdownManager.class);
34
35     /**
36      * List of <code>ShutdownManager.ShutdownTask</code>s which will be executed when the web application is stopped.
37      */

38     private static List JavaDoc coreTasks = new ArrayList JavaDoc();
39
40     /**
41      * Other tasks that the shutdown manager will execute.
42      */

43     private static List JavaDoc customTasks = new ArrayList JavaDoc();
44
45     private static ShutdownManager instance = new ShutdownManager();
46
47     public static ShutdownManager getInstance() {
48         return instance;
49     }
50
51     /**
52      * Adds a new <code>ShutdownTask</code>. Most recently added task will be executed first
53      * @param task ShutdownTask implementation
54      */

55     public static void addShutdownTask(ShutdownTask task) {
56         coreTasks.add(0, task);
57     }
58
59     /**
60      * List the shutdown task that the server will execute
61      * @return <code>List</code> of
62      */

63     public static List JavaDoc listShutdownTasks() {
64         List JavaDoc allTasks = new ArrayList JavaDoc();
65         allTasks.addAll(coreTasks);
66         allTasks.addAll(0, customTasks);
67         return allTasks;
68     }
69
70     /**
71      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
72      */

73     public void contextInitialized(ServletContextEvent JavaDoc sce) {
74         // nothing to do
75
}
76
77     /**
78      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
79      */

80     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
81
82         log.info("Executing shutdown tasks");
83
84         for (Iterator JavaDoc 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 JavaDoc e) {
91                 log.warn(MessageFormat.format("Failed to execute shutdown task {0}: {1} {2}", new Object JavaDoc[]{
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 JavaDoc iter = mrc.getNames();
103         while (iter.hasNext()) {
104             Object JavaDoc element = iter.next();
105             log.info("Adding shutdown task:" + element.toString());
106             customTasks.add(0, mrc.getCommand((String JavaDoc) element)); // Last Registered First Executed
107
}
108
109     }
110
111     protected void onClear() {
112         customTasks.clear();
113     }
114 }
115
Popular Tags