01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.web.struts;
18:
19: import org.apache.struts.action.Action;
20: import org.apache.struts.action.ActionServlet;
21:
22: import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
23:
24: /**
25: * BeanPostProcessor implementation that passes the ActionServlet to beans
26: * that extend the Struts Action class. Invokes <code>Action.setServlet</code>
27: * with <code>null</dode> on bean destruction, providing the same lifecycle
28: * handling as Struts' ActionServlet.
29: *
30: * <p>ContextLoaderPlugIn automatically registers this processor
31: * with the underlying bean factory of its WebApplicationContext.
32: * Applications do not need to use this class directly.
33: *
34: * @author Juergen Hoeller
35: * @since 1.0.1
36: * @see ContextLoaderPlugIn
37: * @see org.apache.struts.action.Action#setServlet
38: */
39: public class ActionServletAwareProcessor implements
40: DestructionAwareBeanPostProcessor {
41:
42: private final ActionServlet actionServlet;
43:
44: /**
45: * Create a new ActionServletAwareProcessor for the given servlet.
46: */
47: public ActionServletAwareProcessor(ActionServlet actionServlet) {
48: this .actionServlet = actionServlet;
49: }
50:
51: public Object postProcessBeforeInitialization(Object bean,
52: String beanName) {
53: if (bean instanceof Action) {
54: ((Action) bean).setServlet(this .actionServlet);
55: }
56: return bean;
57: }
58:
59: public Object postProcessAfterInitialization(Object bean,
60: String beanName) {
61: return bean;
62: }
63:
64: public void postProcessBeforeDestruction(Object bean,
65: String beanName) {
66: if (bean instanceof Action) {
67: ((Action) bean).setServlet(null);
68: }
69: }
70:
71: }
|