001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.test;
017    
018    import org.apache.commons.logging.Log;
019    import org.apache.commons.logging.LogFactory;
020    import org.kuali.rice.core.impl.resourceloader.SpringResourceLoader;
021    import org.springframework.beans.BeansException;
022    import org.springframework.beans.factory.BeanFactory;
023    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    
028    /** 
029     * Wraps a collection of bean factories delegating to the inner bean factories.
030     * 
031     * The first bean factory that returns a non-null/true result is the value that is returned/
032     */
033    public final class CompositeBeanFactory implements BeanFactory {
034    
035            private static final Log LOG = LogFactory.getLog(CompositeBeanFactory.class);
036            
037            private final Collection<BeanFactory> factories;
038            
039            public static BeanFactory createBeanFactory(Collection<? extends SpringResourceLoader> rls) {
040                    if (rls == null || rls.isEmpty()) {
041                            throw new IllegalArgumentException("rls is null or empty");
042                    }
043                    
044                    final Collection<BeanFactory> bfs = new ArrayList<BeanFactory>();
045                    for (SpringResourceLoader rl : rls) {
046                            bfs.add(rl.getContext());
047                    }
048                    return new CompositeBeanFactory(bfs);
049            }
050            
051            public CompositeBeanFactory(Collection<? extends BeanFactory> factories) {
052                    if (factories == null || factories.isEmpty()) {
053                            throw new IllegalArgumentException("factories is null or empty");
054                    }
055                    
056                    this.factories = new ArrayList<BeanFactory>(factories);
057            }
058    
059            @Override
060            public Object getBean(String name) throws BeansException {
061                    for (BeanFactory f : factories) {
062                            try {
063                                    Object o = f.getBean(name);
064                                    if (o != null) {
065                                            return o;
066                                    }       
067                            } catch (BeansException e) {
068                                    LOG.debug("bean exception", e);
069                            }
070                    }
071                    return null;
072            }
073    
074            @Override
075            public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
076                    for (BeanFactory f : factories) {
077                            try {
078                                    T t = f.getBean(name, requiredType);
079                                    if (t != null) {
080                                            return t;
081                                    }       
082                            } catch (BeansException e) {
083                                    LOG.info("bean exception", e);
084                            }
085                    }
086                    return null;
087            }
088    
089            @Override
090            public <T> T getBean(Class<T> requiredType) throws BeansException {
091                    for (BeanFactory f : factories) {
092                            try {
093                                    T t = f.getBean(requiredType);
094                                    if (t != null) {
095                                            return t;
096                                    }       
097                            } catch (BeansException e) {
098                                    LOG.info("bean exception", e);
099                            }
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    }