1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.spring.lifecycle;
19
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27
28 import org.apache.struts2.dispatcher.Dispatcher;
29 import org.apache.struts2.dispatcher.DispatcherListener;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.ApplicationContextAware;
32 import org.springframework.web.context.support.WebApplicationContextUtils;
33
34 import com.opensymphony.xwork2.config.Configuration;
35 import com.opensymphony.xwork2.config.ExternalReferenceResolver;
36 import com.opensymphony.xwork2.config.entities.PackageConfig;
37
38 /***
39 * Setup any {@link com.opensymphony.xwork2.config.ExternalReferenceResolver}s
40 * that implement the ApplicationContextAware interface from the Spring
41 * framework. Relies on Spring's
42 * {@link org.springframework.web.context.ContextLoaderListener}having been
43 * called first.
44 */
45 public class SpringExternalReferenceResolverSetupListener implements
46 ServletContextListener {
47
48 private Map<ServletContext,Listener> listeners = new HashMap<ServletContext,Listener>();
49
50
51
52
53 public synchronized void contextDestroyed(ServletContextEvent event) {
54 Listener l = listeners.get(event.getServletContext());
55 Dispatcher.removeDispatcherListener(l);
56 listeners.remove(event.getServletContext());
57 }
58
59
60
61
62 public synchronized void contextInitialized(ServletContextEvent event) {
63 Listener l = new Listener(event.getServletContext());
64 Dispatcher.addDispatcherListener(l);
65 listeners.put(event.getServletContext(), l);
66 }
67
68 /***
69 * Handles initializing and cleaning up the dispatcher
70 * @author brownd
71 *
72 */
73 private class Listener implements DispatcherListener {
74
75 private ServletContext servletContext;
76
77 /***
78 * Constructs the listener
79 *
80 * @param ctx The servlet context
81 */
82 public Listener(ServletContext ctx) {
83 this.servletContext = ctx;
84 }
85
86
87
88
89 public void dispatcherInitialized(Dispatcher du) {
90 ApplicationContext appContext = WebApplicationContextUtils
91 .getWebApplicationContext(servletContext);
92
93 Configuration xworkConfig = du.getConfigurationManager().getConfiguration();
94 Map packageConfigs = xworkConfig.getPackageConfigs();
95 Iterator i = packageConfigs.values().iterator();
96
97 while (i.hasNext()) {
98 PackageConfig packageConfig = (PackageConfig) i.next();
99 ExternalReferenceResolver resolver = packageConfig.getExternalRefResolver();
100 if (resolver == null || !(resolver instanceof ApplicationContextAware))
101 continue;
102 ApplicationContextAware contextAware = (ApplicationContextAware) resolver;
103 contextAware.setApplicationContext(appContext);
104 }
105
106 }
107
108
109
110
111 public void dispatcherDestroyed(Dispatcher du) {
112 }
113 }
114 }