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.impl.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 | 0 | 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 | 0 | if (rls == null || rls.isEmpty()) { |
41 | 0 | throw new IllegalArgumentException("rls is null or empty"); |
42 | |
} |
43 | |
|
44 | 0 | final Collection<BeanFactory> bfs = new ArrayList<BeanFactory>(); |
45 | 0 | for (SpringResourceLoader rl : rls) { |
46 | 0 | bfs.add(rl.getContext()); |
47 | |
} |
48 | 0 | return new CompositeBeanFactory(bfs); |
49 | |
} |
50 | |
|
51 | 0 | public CompositeBeanFactory(Collection<? extends BeanFactory> factories) { |
52 | 0 | if (factories == null || factories.isEmpty()) { |
53 | 0 | throw new IllegalArgumentException("factories is null or empty"); |
54 | |
} |
55 | |
|
56 | 0 | this.factories = new ArrayList<BeanFactory>(factories); |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
public Object getBean(String name) throws BeansException { |
61 | 0 | for (BeanFactory f : factories) { |
62 | |
try { |
63 | 0 | Object o = f.getBean(name); |
64 | 0 | if (o != null) { |
65 | 0 | return o; |
66 | |
} |
67 | 0 | } catch (BeansException e) { |
68 | 0 | LOG.debug("bean exception", e); |
69 | 0 | } |
70 | |
} |
71 | 0 | return null; |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public <T> T getBean(String name, Class<T> requiredType) throws BeansException { |
76 | 0 | for (BeanFactory f : factories) { |
77 | |
try { |
78 | 0 | T t = f.getBean(name, requiredType); |
79 | 0 | if (t != null) { |
80 | 0 | return t; |
81 | |
} |
82 | 0 | } catch (BeansException e) { |
83 | 0 | LOG.info("bean exception", e); |
84 | 0 | } |
85 | |
} |
86 | 0 | return null; |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public <T> T getBean(Class<T> requiredType) throws BeansException { |
91 | 0 | for (BeanFactory f : factories) { |
92 | |
try { |
93 | 0 | T t = f.getBean(requiredType); |
94 | 0 | if (t != null) { |
95 | 0 | return t; |
96 | |
} |
97 | 0 | } catch (BeansException e) { |
98 | 0 | LOG.info("bean exception", e); |
99 | 0 | } |
100 | |
} |
101 | 0 | return null; |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public Object getBean(String name, Object... args) throws BeansException { |
106 | 0 | for (BeanFactory f : factories) { |
107 | |
try { |
108 | 0 | Object o = f.getBean(name, args); |
109 | 0 | if (o != null) { |
110 | 0 | return o; |
111 | |
} |
112 | 0 | } catch (BeansException e) { |
113 | 0 | LOG.info("bean exception", e); |
114 | 0 | } |
115 | |
} |
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | |
@Override |
120 | |
public boolean containsBean(String name) { |
121 | 0 | for (BeanFactory f : factories) { |
122 | |
try { |
123 | 0 | boolean b = f.containsBean(name); |
124 | 0 | if (b) { |
125 | 0 | return b; |
126 | |
} |
127 | 0 | } catch (BeansException e) { |
128 | 0 | LOG.info("bean exception", e); |
129 | 0 | } |
130 | |
} |
131 | 0 | return false; |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
136 | 0 | for (BeanFactory f : factories) { |
137 | |
try { |
138 | 0 | boolean b = f.isSingleton(name); |
139 | 0 | if (b) { |
140 | 0 | return b; |
141 | |
} |
142 | 0 | } catch (BeansException e) { |
143 | 0 | LOG.info("bean exception", e); |
144 | 0 | } |
145 | |
} |
146 | 0 | return false; |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException { |
151 | 0 | for (BeanFactory f : factories) { |
152 | |
try { |
153 | 0 | boolean b = f.isPrototype(name); |
154 | 0 | if (b) { |
155 | 0 | return b; |
156 | |
} |
157 | 0 | } catch (BeansException e) { |
158 | 0 | LOG.info("bean exception", e); |
159 | 0 | } |
160 | |
} |
161 | 0 | return false; |
162 | |
} |
163 | |
|
164 | |
@Override |
165 | |
public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException { |
166 | 0 | for (BeanFactory f : factories) { |
167 | |
try { |
168 | 0 | boolean b = f.isTypeMatch(name, targetType); |
169 | 0 | if (b) { |
170 | 0 | return b; |
171 | |
} |
172 | 0 | } catch (BeansException e) { |
173 | 0 | LOG.info("bean exception", e); |
174 | 0 | } |
175 | |
} |
176 | 0 | return false; |
177 | |
} |
178 | |
|
179 | |
@Override |
180 | |
public Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
181 | 0 | for (BeanFactory f : factories) { |
182 | |
try { |
183 | 0 | Class<?> c = f.getType(name); |
184 | 0 | if (c != null) { |
185 | 0 | return c; |
186 | |
} |
187 | 0 | } catch (BeansException e) { |
188 | 0 | LOG.info("bean exception", e); |
189 | 0 | } |
190 | |
} |
191 | 0 | return null; |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public String[] getAliases(String name) { |
196 | 0 | for (BeanFactory f : factories) { |
197 | |
try { |
198 | 0 | String[] s = f.getAliases(name); |
199 | 0 | if (s != null) { |
200 | 0 | return s; |
201 | |
} |
202 | 0 | } catch (BeansException e) { |
203 | 0 | LOG.info("bean exception", e); |
204 | 0 | } |
205 | |
} |
206 | 0 | return null; |
207 | |
} |
208 | |
} |