01: /*
02: * Copyright (c) 2005 ePlus Corporation. All Rights Reserved.
03: */
04: package com.opensymphony.webwork.spring.interceptor;
05:
06: import com.opensymphony.xwork.interceptor.AroundInterceptor;
07: import com.opensymphony.xwork.ActionInvocation;
08: import com.opensymphony.webwork.spring.lifecycle.ApplicationContextSessionListener;
09: import org.springframework.context.ApplicationContext;
10: import org.springframework.context.ConfigurableApplicationContext;
11: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
12:
13: import java.util.Map;
14:
15: /**
16: * <!-- START SNIPPET: description -->
17: * TODO: Give a description of the Interceptor.
18: * <!-- END SNIPPET: description -->
19: *
20: * <!-- START SNIPPET: parameters -->
21: * TODO: Describe the paramters for this Interceptor.
22: * <!-- END SNIPPET: parameters -->
23: *
24: * <!-- START SNIPPET: extending -->
25: * TODO: Discuss some possible extension of the Interceptor.
26: * <!-- END SNIPPET: extending -->
27: *
28: * <pre>
29: * <!-- START SNIPPET: example -->
30: * <!-- TODO: Describe how the Interceptor reference will effect execution -->
31: * <action name="someAction" class="com.examples.SomeAction">
32: * TODO: fill in the interceptor reference.
33: * <interceptor-ref name=""/>
34: * <result name="success">good_result.ftl</result>
35: * </action>
36: * <!-- END SNIPPET: example -->
37: * </pre>
38: *
39: * SessionContextAutowiringInterceptor
40: * Created : Aug 21, 2005 12:34:20 AM
41: *
42: * @author Jason Carreira <jcarreira@eplus.com>
43: */
44: public class SessionContextAutowiringInterceptor extends
45: AroundInterceptor {
46: private Integer autowireStrategy = new Integer(
47: AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
48:
49: public void setAutowireStrategy(Integer autowireStrategy) {
50: this .autowireStrategy = autowireStrategy;
51: }
52:
53: protected void after(ActionInvocation dispatcher, String result)
54: throws Exception {
55: }
56:
57: protected void before(ActionInvocation invocation) throws Exception {
58: Map session = invocation.getInvocationContext().getSession();
59: ApplicationContext applicationContext = (ApplicationContext) session
60: .get(ApplicationContextSessionListener.APP_CONTEXT_SESSION_KEY);
61: AutowireCapableBeanFactory factory = findAutoWiringBeanFactory(applicationContext);
62: factory.autowireBeanProperties(invocation.getAction(),
63: autowireStrategy.intValue(), false);
64: }
65:
66: protected AutowireCapableBeanFactory findAutoWiringBeanFactory(
67: ApplicationContext context) {
68: if (context instanceof AutowireCapableBeanFactory) {
69: // Check the context
70: return (AutowireCapableBeanFactory) context;
71: } else if (context instanceof ConfigurableApplicationContext) {
72: // Try and grab the beanFactory
73: return ((ConfigurableApplicationContext) context)
74: .getBeanFactory();
75: } else if (context.getParent() != null) {
76: // And if all else fails, try again with the parent context
77: return findAutoWiringBeanFactory(context.getParent());
78: }
79: return null;
80: }
81: }
|