View Javadoc
1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.controller.support;
17  
18  import javax.servlet.ServletContext;
19  
20  import org.apache.commons.logging.Log;
21  
22  import com.fatwire.gst.foundation.controller.AppContext;
23  import com.fatwire.gst.foundation.facade.logging.LogUtil;
24  
25  /**
26   * @author Dolf.Dijkstra
27   * @since 4 April 2012
28   */
29  public class WebContextUtil {
30  
31      protected static final Log LOG = LogUtil.getLog(WebContextUtil.class);
32      private WebContextUtil() {
33          super();
34      }
35  
36      public static AppContext getWebAppContext(ServletContext ctx) {
37          Object o = ctx.getAttribute(WebAppContext.WEB_CONTEXT_NAME);
38          if (o == null) {
39              LOG.trace("Configuring WebAppContext from WebContextUtil; it is not explicitly configured in web.xml. Using default setup!");
40              return new WebAppContextLoader().configureWebAppContext(ctx);
41          }
42          if (o instanceof AppContext) {
43              return (AppContext) o;
44          }
45  
46          throw new IncompleteConfigurationException("There was no " + WebAppContext.WEB_CONTEXT_NAME
47                  + " object in the ServletContext found. Is the WebAppContextLoader listener configured in web.xml?");
48      }
49  
50  }