1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
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
74
75 }
76
77 }