View Javadoc

1   /*
2    * $Id: ServletContextSingleton.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.config;
23  
24  import javax.servlet.ServletContext;
25  
26  /***
27   * This singleton holds an instance of the web servlet context.
28   * <p/>
29   * This is needed for running Struts on Weblogic Server 6.1
30   * because there is no provision to retrieve the servlet context
31   * from the web session object.
32   * <p/>
33   * This class is created to bet that this singleton can be set by
34   * {@link org.apache.struts2.dispatcher.FilterDispatcherCompatWeblogic61}
35   * before the servlet context is needed by
36   * {@link org.apache.struts2.lifecycle.SessionLifecycleListener}
37   * which will use this object to get it.
38   *
39   */
40  public class ServletContextSingleton {
41      /***
42       * The web servlet context.  Holding this is the
43       * purpose of this singleton.
44       */
45      private ServletContext servletContext;
46  
47      /***
48       * The sole instance of this class.
49       */
50      private static ServletContextSingleton singleton;
51  
52      /***
53       * Constructor which cannot be called
54       * publicly.
55       */
56      private ServletContextSingleton() {
57      }
58  
59      /***
60       * Answers the singleton.
61       * <p/>
62       * At some point, the caller must populate the web servlet
63       * context.
64       *
65       * @return Answers the singleton instance of this class
66       */
67      public static ServletContextSingleton getInstance() {
68          if (singleton == null) {
69              singleton = new ServletContextSingleton();
70          }
71          return singleton;
72      }
73  
74      /***
75       * Gets the servlet context
76       *
77       * @return The web servlet context
78       */
79      public ServletContext getServletContext() {
80          return servletContext;
81      }
82  
83      /***
84       * Sets the servlet context
85       *
86       * @param context The web servlet context
87       */
88      public void setServletContext(ServletContext context) {
89          servletContext = context;
90      }
91  
92  }