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  import org.springframework.util.Assert;
22  import org.springframework.web.context.WebApplicationContext;
23  import org.springframework.web.context.support.WebApplicationContextUtils;
24  
25  import com.fatwire.gst.foundation.controller.AppContext;
26  import com.fatwire.gst.foundation.controller.action.ActionNameResolver;
27  import com.fatwire.gst.foundation.controller.action.support.NullActionNameResolver;
28  import com.fatwire.gst.foundation.facade.logging.LogUtil;
29  
30  public class SpringWebAppContext implements AppContext {
31      protected static final Log LOG = LogUtil.getLog(SpringWebAppContext.class);
32      private static final ActionNameResolver nullActionNameResolver = new NullActionNameResolver();
33      private final WebApplicationContext wac;
34  
35      /**
36       * This constructor is needed for the {@link WebAppContextLoader}. 
37       * 
38       * @param ctx
39       * @param app
40       */
41      public SpringWebAppContext(ServletContext ctx, AppContext app) {
42          this.wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ctx);
43          Assert.notNull(wac);
44  
45      }
46  
47      @SuppressWarnings("unchecked")
48      @Override
49      public <T> T getBean(String name, Class<T> t) {
50          T bean = null;
51          if (wac.containsBean(name)) {
52              bean = (T) wac.getBean(name, t);
53              if (LOG.isTraceEnabled()) {
54                  LOG.trace("Using " + name + " as configured in spring: " + bean.getClass().getName());
55              }
56          } else {
57              LOG.debug("Cannot find the '" + name + "' bean in the Spring WebApplicationContext: " + wac.toString());
58              try {
59                  bean = TemplateMethodFactory.createByMethod(this, t);
60              } catch (final Exception e) {
61                  LOG.debug(e.getMessage() + " while trying to create  a" + t.getName());
62              }
63          }
64          return bean;
65      }
66  
67      public ActionNameResolver createActionNameResolver() {
68          return nullActionNameResolver;
69      }
70  
71      @Override
72      public void init() {
73          // TODO Auto-generated method stub
74  
75      }
76  
77  }