01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.xwork.spring;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
10: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
11: import org.springframework.beans.factory.support.RootBeanDefinition;
12: import org.springframework.context.ApplicationContext;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16: import java.util.Map;
17:
18: /**
19: * SpringProxyableObjectFactory.
20: *
21: * @author Jason Carreira
22: */
23: public class SpringProxyableObjectFactory extends SpringObjectFactory {
24:
25: private static final Log log = LogFactory
26: .getLog(SpringProxyableObjectFactory.class);
27:
28: private List skipBeanNames = new ArrayList();
29:
30: public Object buildBean(String beanName, Map extraContext)
31: throws Exception {
32: if (log.isDebugEnabled()) {
33: log.debug("Building bean for name " + beanName);
34: }
35: if (!skipBeanNames.contains(beanName)) {
36: ApplicationContext anAppContext = getApplicationContext(extraContext);
37: try {
38: if (log.isDebugEnabled()) {
39: log
40: .debug("Trying the application context... appContext = "
41: + anAppContext
42: + ",\n bean name = "
43: + beanName);
44: }
45: return anAppContext.getBean(beanName);
46: } catch (NoSuchBeanDefinitionException e) {
47: if (log.isDebugEnabled()) {
48: log
49: .debug("Did not find bean definition for bean named "
50: + beanName
51: + ", creating a new one...");
52: }
53: if (autoWiringFactory instanceof BeanDefinitionRegistry) {
54: try {
55: Class clazz = Class.forName(beanName);
56: BeanDefinitionRegistry registry = (BeanDefinitionRegistry) autoWiringFactory;
57: RootBeanDefinition def = new RootBeanDefinition(
58: clazz, autowireStrategy);
59: def.setSingleton(false);
60: if (log.isDebugEnabled()) {
61: log
62: .debug("Registering a new bean definition for class "
63: + beanName);
64: }
65: registry.registerBeanDefinition(beanName, def);
66: try {
67: return anAppContext.getBean(beanName);
68: } catch (NoSuchBeanDefinitionException e2) {
69: log
70: .warn("Could not register new bean definition for bean "
71: + beanName);
72: skipBeanNames.add(beanName);
73: }
74: } catch (ClassNotFoundException e1) {
75: skipBeanNames.add(beanName);
76: }
77: }
78: }
79: }
80: if (log.isDebugEnabled()) {
81: log
82: .debug("Returning autowired instance created by default ObjectFactory");
83: }
84: return autoWireBean(super .buildBean(beanName, extraContext),
85: autoWiringFactory);
86: }
87:
88: /**
89: * Subclasses may override this to return a different application context.
90: * Note that this application context should see any changes made to the
91: * <code>autoWiringFactory</code>, so the application context should be either
92: * the original or a child context of the original.
93: *
94: * @param context provided context.
95: */
96: protected ApplicationContext getApplicationContext(Map context) {
97: return appContext;
98: }
99: }
|