View Javadoc

1   /*
2    * $Id: ResolverSetupServletContextListener.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.util;
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  
31  import com.opensymphony.xwork2.config.Configuration;
32  import com.opensymphony.xwork2.config.entities.PackageConfig;
33  
34  
35  /***
36   * A Servlet Context Listener that will loop through all Reference Resolvers available in
37   * the xwork Configuration and set the ServletContext on those that are ServletContextAware.
38   * The Servlet Context can be used by the External Reference Resolver to initialise it's state. i.e. the
39   * Spring framework uses a ContextServletListener to initialise it's IoC container, storing it's
40   * container context (ApplicationContext in Spring terms) in the Servlet context, the External
41   * Reference Resolver can get a reference to the container context from the servlet context.
42   */
43  public class ResolverSetupServletContextListener implements ServletContextListener {
44  
45      Map<ServletContext,Listener> listeners = new HashMap<ServletContext,Listener>();
46      
47      public synchronized void contextDestroyed(ServletContextEvent event) {
48          Listener l = listeners.get(event.getServletContext());
49          Dispatcher.removeDispatcherListener(l);
50          listeners.remove(event.getServletContext());
51      }
52  
53      public synchronized void contextInitialized(ServletContextEvent event) {
54          Listener l = new Listener(event.getServletContext());
55          Dispatcher.addDispatcherListener(l);
56          listeners.put(event.getServletContext(), l);
57      }
58      
59      private class Listener implements DispatcherListener {
60  
61          private ServletContext servletContext;
62          
63          public Listener(ServletContext ctx) {
64              this.servletContext = ctx;
65          }
66          
67          public void dispatcherInitialized(Dispatcher du) {
68              Configuration config = du.getConfigurationManager().getConfiguration();
69              String key;
70              PackageConfig packageConfig;
71  
72              for (Iterator iter = config.getPackageConfigNames().iterator();
73                   iter.hasNext();) {
74                  key = (String) iter.next();
75                  packageConfig = config.getPackageConfig(key);
76  
77                  if (packageConfig.getExternalRefResolver()instanceof ServletContextAware) {
78                      ((ServletContextAware) packageConfig.getExternalRefResolver()).setServletContext(servletContext);
79                  }
80              }
81              
82          }
83  
84          public void dispatcherDestroyed(Dispatcher du) {
85          }
86      }
87  }