View Javadoc

1   /*
2    * $Id: SpringExternalReferenceResolverSetupListener.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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      /* (non-Javadoc)
51       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
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      /* (non-Javadoc)
60       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
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          /* (non-Javadoc)
87           * @see org.apache.struts2.dispatcher.DispatcherListener#dispatcherInitialized(org.apache.struts2.dispatcher.Dispatcher)
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         /* (non-Javadoc)
109          * @see org.apache.struts2.dispatcher.DispatcherListener#dispatcherDestroyed(org.apache.struts2.dispatcher.Dispatcher)
110          */
111         public void dispatcherDestroyed(Dispatcher du) {
112         }
113     }
114 }