View Javadoc

1   /*
2    * $Id: ServletContextSingleton.java 421742 2006-07-13 23:48: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.config;
19  
20  import javax.servlet.ServletContext;
21  
22  /***
23   * This singleton holds an instance of the web servlet context.
24   * <p/>
25   * This is needed for running Struts on Weblogic Server 6.1
26   * because there is no provision to retrieve the servlet context
27   * from the web session object.
28   * <p/>
29   * This class is created to bet that this singleton can be set by
30   * {@link org.apache.struts2.dispatcher.FilterDispatcherCompatWeblogic61}
31   * before the servlet context is needed by
32   * {@link org.apache.struts2.lifecycle.SessionLifecycleListener}
33   * which will use this object to get it.
34   *
35   */
36  public class ServletContextSingleton {
37      /***
38       * The web servlet context.  Holding this is the
39       * purpose of this singleton.
40       */
41      private ServletContext servletContext;
42  
43      /***
44       * The sole instance of this class.
45       */
46      private static ServletContextSingleton singleton;
47  
48      /***
49       * Constructor which cannot be called
50       * publicly.
51       */
52      private ServletContextSingleton() {
53      }
54  
55      /***
56       * Answers the singleton.
57       * <p/>
58       * At some point, the caller must populate the web servlet
59       * context.
60       *
61       * @return Answers the singleton instance of this class
62       */
63      public static ServletContextSingleton getInstance() {
64          if (singleton == null) {
65              singleton = new ServletContextSingleton();
66          }
67          return singleton;
68      }
69  
70      /***
71       * Gets the servlet context
72       * 
73       * @return The web servlet context
74       */
75      public ServletContext getServletContext() {
76          return servletContext;
77      }
78  
79      /***
80       * Sets the servlet context
81       * 
82       * @param context The web servlet context
83       */
84      public void setServletContext(ServletContext context) {
85          servletContext = context;
86      }
87  
88  }