KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > spring > lifecycle > SpringExternalReferenceResolverSetupListener


1 /*
2  * Created on Jun 14, 2004
3  */

4 package com.opensymphony.webwork.spring.lifecycle;
5
6 import com.opensymphony.xwork.config.Configuration;
7 import com.opensymphony.xwork.config.ConfigurationManager;
8 import com.opensymphony.xwork.config.ExternalReferenceResolver;
9 import com.opensymphony.xwork.config.entities.PackageConfig;
10 import org.springframework.context.ApplicationContext;
11 import org.springframework.context.ApplicationContextAware;
12 import org.springframework.web.context.support.WebApplicationContextUtils;
13
14 import javax.servlet.ServletContextEvent JavaDoc;
15 import javax.servlet.ServletContextListener JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Setup any {@link com.opensymphony.xwork.config.ExternalReferenceResolver}s
21  * that implement the ApplicationContextAware interface from the Spring
22  * framework. Relies on Spring's
23  * {@link org.springframework.web.context.ContextLoaderListener}having been
24  * called first.
25  *
26  * @author Simon Stewart
27  */

28 public class SpringExternalReferenceResolverSetupListener implements
29         ServletContextListener JavaDoc {
30     public void contextInitialized(ServletContextEvent JavaDoc event) {
31         ApplicationContext appContext = WebApplicationContextUtils
32                 .getWebApplicationContext(event.getServletContext());
33
34         Configuration xworkConfig = ConfigurationManager.getConfiguration();
35         Map JavaDoc packageConfigs = xworkConfig.getPackageConfigs();
36         Iterator JavaDoc i = packageConfigs.values().iterator();
37
38         while (i.hasNext()) {
39             PackageConfig packageConfig = (PackageConfig) i.next();
40             ExternalReferenceResolver resolver = packageConfig.getExternalRefResolver();
41             if (resolver == null || !(resolver instanceof ApplicationContextAware))
42                     continue;
43             ApplicationContextAware contextAware = (ApplicationContextAware) resolver;
44             contextAware.setApplicationContext(appContext);
45         }
46     }
47
48     public void contextDestroyed(ServletContextEvent JavaDoc event) {
49         // Nothing to do
50
}
51
52 }
Popular Tags