01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.source;
16:
17: import javax.servlet.ServletContext;
18:
19: import org.springframework.web.context.WebApplicationContext;
20: import org.springframework.web.context.support.WebApplicationContextUtils;
21: import org.strecks.context.ActionContext;
22: import org.strecks.util.Assert;
23:
24: /**
25: * <code>ActionBeanSource</code> implementation which locates a Spring-registered action bean.
26: * Note that this action bean should not be registered as a singleton
27: * @author Phil Zoio
28: */
29: public class SpringActionBeanSource implements ActionBeanSource {
30:
31: private Class actionBeanClass;
32:
33: private String beanName;
34:
35: private boolean checkNotSingleton;
36:
37: public SpringActionBeanSource(Class actionBeanClass, String beanName) {
38: super ();
39: Assert.notNull(actionBeanClass);
40: Assert.notNull(beanName);
41: this .actionBeanClass = actionBeanClass;
42: this .beanName = beanName;
43: }
44:
45: public Object createBean(ActionContext context) {
46: ServletContext servletContext = context.getContext();
47: WebApplicationContext wac = WebApplicationContextUtils
48: .getRequiredWebApplicationContext(servletContext);
49:
50: synchronized (this ) {
51: if (!checkNotSingleton) {
52: Assert.isTrue(!wac.isSingleton(beanName),
53: "Spring-registered action bean " + beanName
54: + " cannot be a singleton");
55: checkNotSingleton = true;
56: }
57: }
58:
59: return wac.getBean(beanName, actionBeanClass);
60: }
61:
62: public Class getBeanClass() {
63: return actionBeanClass;
64: }
65:
66: }
|