View Javadoc

1   /*
2    * $Id: ServletContextHolderListener.java 462530 2006-10-10 19:48:19Z 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.portlet.context;
19  
20  import javax.servlet.ServletContext;
21  import javax.servlet.ServletContextEvent;
22  import javax.servlet.ServletContextListener;
23  
24  /***
25   * Some of the factory/managers (e.g. the ObjectFactory) need access to 
26   * the {@link org.apache.struts2.ServletActionContext} object when initializing.
27   * This {@link javax.servlet.ServletContextListener} keeps a reference to the 
28   * {@link javax.servlet.ServletContext} and exposes it through a <code>public static</code>
29   * method.
30   * 
31   */
32  public class ServletContextHolderListener implements ServletContextListener {
33  
34      private static ServletContext context = null;
35  
36      /***
37       * @return The current servlet context
38       */
39      public static ServletContext getServletContext() {
40          return context;
41      }
42  
43      /***
44       * Stores the reference to the {@link ServletContext}.
45       * 
46       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
47       */
48      public void contextInitialized(ServletContextEvent event) {
49          context = event.getServletContext();
50          
51      }
52  
53      /***
54       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
55       */
56      public void contextDestroyed(ServletContextEvent event) {
57      	context = null;
58      }
59  
60  }