1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.test;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27
28
29
30
31
32
33 public final class CompositeBeanFactory implements BeanFactory {
34
35 private static final Log LOG = LogFactory.getLog(CompositeBeanFactory.class);
36
37 private final Collection<BeanFactory> factories;
38
39 public static BeanFactory createBeanFactory(Collection<? extends SpringResourceLoader> rls) {
40 if (rls == null || rls.isEmpty()) {
41 throw new IllegalArgumentException("rls is null or empty");
42 }
43
44 final Collection<BeanFactory> bfs = new ArrayList<BeanFactory>();
45 for (SpringResourceLoader rl : rls) {
46 bfs.add(rl.getContext());
47 }
48 return new CompositeBeanFactory(bfs);
49 }
50
51 public CompositeBeanFactory(Collection<? extends BeanFactory> factories) {
52 if (factories == null || factories.isEmpty()) {
53 throw new IllegalArgumentException("factories is null or empty");
54 }
55
56 this.factories = new ArrayList<BeanFactory>(factories);
57 }
58
59 @Override
60 public Object getBean(String name) throws BeansException {
61 for (BeanFactory f : factories) {
62 try {
63 Object o = f.getBean(name);
64 if (o != null) {
65 return o;
66 }
67 } catch (BeansException e) {
68 LOG.debug("bean exception", e);
69 }
70 }
71 return null;
72 }
73
74 @Override
75 public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
76 for (BeanFactory f : factories) {
77 try {
78 T t = f.getBean(name, requiredType);
79 if (t != null) {
80 return t;
81 }
82 } catch (BeansException e) {
83 LOG.info("bean exception", e);
84 }
85 }
86 return null;
87 }
88
89 @Override
90 public <T> T getBean(Class<T> requiredType) throws BeansException {
91 for (BeanFactory f : factories) {
92 try {
93 T t = f.getBean(requiredType);
94 if (t != null) {
95 return t;
96 }
97 } catch (BeansException e) {
98 LOG.info("bean exception", e);
99 }
100 }
101 return null;
102 }
103
104 @Override
105 public Object getBean(String name, Object... args) throws BeansException {
106 for (BeanFactory f : factories) {
107 try {
108 Object o = f.getBean(name, args);
109 if (o != null) {
110 return o;
111 }
112 } catch (BeansException e) {
113 LOG.info("bean exception", e);
114 }
115 }
116 return null;
117 }
118
119 @Override
120 public boolean containsBean(String name) {
121 for (BeanFactory f : factories) {
122 try {
123 boolean b = f.containsBean(name);
124 if (b) {
125 return b;
126 }
127 } catch (BeansException e) {
128 LOG.info("bean exception", e);
129 }
130 }
131 return false;
132 }
133
134 @Override
135 public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
136 for (BeanFactory f : factories) {
137 try {
138 boolean b = f.isSingleton(name);
139 if (b) {
140 return b;
141 }
142 } catch (BeansException e) {
143 LOG.info("bean exception", e);
144 }
145 }
146 return false;
147 }
148
149 @Override
150 public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
151 for (BeanFactory f : factories) {
152 try {
153 boolean b = f.isPrototype(name);
154 if (b) {
155 return b;
156 }
157 } catch (BeansException e) {
158 LOG.info("bean exception", e);
159 }
160 }
161 return false;
162 }
163
164 @Override
165 public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
166 for (BeanFactory f : factories) {
167 try {
168 boolean b = f.isTypeMatch(name, targetType);
169 if (b) {
170 return b;
171 }
172 } catch (BeansException e) {
173 LOG.info("bean exception", e);
174 }
175 }
176 return false;
177 }
178
179 @Override
180 public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
181 for (BeanFactory f : factories) {
182 try {
183 Class<?> c = f.getType(name);
184 if (c != null) {
185 return c;
186 }
187 } catch (BeansException e) {
188 LOG.info("bean exception", e);
189 }
190 }
191 return null;
192 }
193
194 @Override
195 public String[] getAliases(String name) {
196 for (BeanFactory f : factories) {
197 try {
198 String[] s = f.getAliases(name);
199 if (s != null) {
200 return s;
201 }
202 } catch (BeansException e) {
203 LOG.info("bean exception", e);
204 }
205 }
206 return null;
207 }
208 }