KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > lifecycle > ApplicationLifecycleListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.lifecycle;
6
7 import com.opensymphony.xwork.interceptor.component.ComponentConfiguration;
8 import com.opensymphony.xwork.interceptor.component.ComponentManager;
9 import com.opensymphony.xwork.interceptor.component.DefaultComponentManager;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.xml.sax.SAXException JavaDoc;
13
14 import javax.servlet.ServletContext JavaDoc;
15 import javax.servlet.ServletContextEvent JavaDoc;
16 import javax.servlet.ServletContextListener JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.beans.Introspector JavaDoc;
20
21
22 /**
23  * A servlet context listener to handle the lifecycle of an application-based XWork component manager.
24  *
25  * @author <a HREF="mailto:joew@thoughtworks.com">Joe Walnes</a>
26  * @author Patrick Lightbody
27  * @author Bill Lynch (docs)
28  */

29 public class ApplicationLifecycleListener implements ServletContextListener JavaDoc {
30     //~ Static fields/initializers /////////////////////////////////////////////
31

32     private static final Log log = LogFactory.getLog(ApplicationLifecycleListener.class);
33
34     //~ Methods ////////////////////////////////////////////////////////////////
35

36     /**
37      * Destroys the XWork component manager because the server is shutting down.
38      *
39      * @param event the servlet context event.
40      */

41     public void contextDestroyed(ServletContextEvent JavaDoc event) {
42         ServletContext JavaDoc application = event.getServletContext();
43         ComponentManager container = (ComponentManager) application.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY);
44
45         if (container != null) {
46             container.dispose();
47         }
48
49         // do some cleanup
50

51         // If the JavaBeans Introspector has been used to analyze application classes,
52
// the Introspector cache will hold a hard reference to those classes.
53
// Consequently, those classes and the web app class loader will not be
54
// garbage collected on web app shutdown!
55
Introspector.flushCaches(); // WW-758
56

57         LogFactory.releaseAll();
58     }
59
60     /**
61      * Initializes the XWork compontent manager. Loads component config from the <tt>components.xml</tt> file
62      * in the classpath. Adds the component manager and compontent config as attributes of the servlet context.
63      *
64      * @param event the servlet context event.
65      */

66     public void contextInitialized(ServletContextEvent JavaDoc event) {
67         ServletContext JavaDoc application = event.getServletContext();
68         ComponentManager container = createComponentManager();
69         ComponentConfiguration config = loadConfiguration();
70
71         if (config != null) {
72             config.configure(container, "application");
73             application.setAttribute(ComponentManager.COMPONENT_MANAGER_KEY, container);
74             application.setAttribute("ComponentConfiguration", config);
75         }
76     }
77
78     /**
79      * Returns a new <tt>DefaultComponentManager</tt> instance. This method is useful for developers
80      * wishing to subclass this class and provide a different implementation of <tt>DefaultComponentManager</tt>.
81      *
82      * @return a new <tt>DefaultComponentManager</tt> instance.
83      */

84     protected DefaultComponentManager createComponentManager() {
85         return new DefaultComponentManager();
86     }
87
88     private ComponentConfiguration loadConfiguration() {
89         ComponentConfiguration config = new ComponentConfiguration();
90         InputStream JavaDoc configXml = Thread.currentThread().getContextClassLoader().getResourceAsStream("components.xml");
91
92         if (configXml == null) {
93             final String JavaDoc message = "Unable to find the file components.xml in the classpath, XWork IoC *not* initialized.";
94             log.warn(message);
95             return null;
96         }
97
98         try {
99             config.loadFromXml(configXml);
100         } catch (IOException JavaDoc ioe) {
101             log.error(ioe);
102             throw new RuntimeException JavaDoc("Unable to load component configuration");
103         } catch (SAXException JavaDoc sae) {
104             log.error(sae);
105             throw new RuntimeException JavaDoc("Unable to load component configuration");
106         }
107
108         return config;
109     }
110 }
111
Popular Tags