KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > ResolverSetupServletContextListener


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

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.xwork.config.Configuration;
8 import com.opensymphony.xwork.config.ConfigurationManager;
9 import com.opensymphony.xwork.config.entities.PackageConfig;
10
11 import javax.servlet.ServletContextEvent JavaDoc;
12 import javax.servlet.ServletContextListener JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15
16 /**
17  * A Servlet Context Listener that will loop through all Reference Resolvers available in
18  * the xwork Configuration and set the ServletContext on those that are ServletContextAware.
19  * The Servlet Context can be used by the External Reference Resolver to initialise it's state. i.e. the
20  * Spring framework uses a ContextServletListener to initialise it's IoC container, storing it's
21  * container context (ApplicationContext in Spring terms) in the Servlet context, the External
22  * Reference Resolver can get a reference to the container context from the servlet context.
23  *
24  * @author Ross
25  */

26 public class ResolverSetupServletContextListener implements ServletContextListener JavaDoc {
27     //~ Methods ////////////////////////////////////////////////////////////////
28

29     public void contextDestroyed(ServletContextEvent JavaDoc event) {
30     }
31
32     public void contextInitialized(ServletContextEvent JavaDoc event) {
33         Configuration config = ConfigurationManager.getConfiguration();
34         String JavaDoc key;
35         PackageConfig packageConfig;
36
37         for (Iterator JavaDoc iter = config.getPackageConfigNames().iterator();
38              iter.hasNext();) {
39             key = (String JavaDoc) iter.next();
40             packageConfig = config.getPackageConfig(key);
41
42             if (packageConfig.getExternalRefResolver() instanceof ServletContextAware) {
43                 ((ServletContextAware) packageConfig.getExternalRefResolver()).setServletContext(event.getServletContext());
44             }
45         }
46     }
47 }
48
Popular Tags