View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   
6   package com.hack23.cia.web.common;
7   
8   import javax.servlet.ServletContextEvent;
9   
10  import org.springframework.web.context.ContextLoaderListener;
11  import org.springframework.web.context.WebApplicationContext;
12  import org.springframework.web.context.support.WebApplicationContextUtils;
13  
14  /***
15   * The listener interface for receiving staticContextLoader events.
16   * The class that is interested in processing a staticContextLoader
17   * event implements this interface, and the object created
18   * with that class is registered with a component using the
19   * component's <code>addStaticContextLoaderListener<code> method. When
20   * the staticContextLoader event occurs, that object's appropriate
21   * method is invoked.
22   *
23   * @see StaticContextLoaderEvent
24   */
25  public class StaticContextLoaderListener extends ContextLoaderListener {
26  
27      /*** The web app ctx. */
28      private static WebApplicationContext webAppCtx = null;
29  
30  	/***
31  	 * Gets the web application context.
32  	 *
33  	 * @return the web application context
34  	 */
35      protected static WebApplicationContext getWebApplicationContext() {
36          return webAppCtx;
37      }
38  
39      /***
40       * Instantiates a new static context loader listener.
41       */
42      public StaticContextLoaderListener() {
43  		super();
44  	}
45  
46      /*
47       * (non-Javadoc)
48       * 
49       * @see
50       * org.springframework.web.context.ContextLoaderListener#contextDestroyed
51       * (javax.servlet.ServletContextEvent)
52       */
53      @Override
54      public final void contextDestroyed(final ServletContextEvent event) {
55      	super.contextDestroyed(event);
56          StaticContextLoaderListener.webAppCtx = null;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see
63       * org.springframework.web.context.ContextLoaderListener#contextInitialized
64       * (javax.servlet.ServletContextEvent)
65       */
66      @Override
67      public final void contextInitialized(final ServletContextEvent event) {
68          super.contextInitialized(event);
69          StaticContextLoaderListener.webAppCtx = WebApplicationContextUtils
70                  .getRequiredWebApplicationContext(event.getServletContext());
71      }
72  }