Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BeanFactoryResourceLoader |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright 2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.framework.resourceloader; | |
17 | ||
18 | import java.util.HashSet; | |
19 | import java.util.Set; | |
20 | ||
21 | import javax.xml.namespace.QName; | |
22 | ||
23 | import org.kuali.rice.core.api.resourceloader.ResourceLoader; | |
24 | import org.springframework.beans.factory.BeanFactory; | |
25 | ||
26 | ||
27 | /** | |
28 | * Wraps a {@link BeanFactory} as a {@link ResourceLoader}. | |
29 | * | |
30 | * Does not start or stop the {@link BeanFactory}. Assumes this is being done | |
31 | * by the application that started it. | |
32 | * | |
33 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
34 | * | |
35 | */ | |
36 | public class BeanFactoryResourceLoader extends BaseResourceLoader { | |
37 | ||
38 | private final BeanFactory beanFactory; | |
39 | private final Set<String> beanNames; | |
40 | ||
41 | public BeanFactoryResourceLoader(QName name, BeanFactory beanFactory) { | |
42 | 0 | this(name, beanFactory, null); |
43 | 0 | } |
44 | ||
45 | public BeanFactoryResourceLoader(QName name, BeanFactory beanFactory, Set<String> beanNames) { | |
46 | 0 | super(name); |
47 | 0 | this.beanFactory = beanFactory; |
48 | 0 | this.beanNames = beanNames; |
49 | 0 | } |
50 | ||
51 | @Override | |
52 | public Object getService(QName serviceName) { | |
53 | 0 | String beanName = translateBeanName(serviceName); |
54 | 0 | if ((beanNames == null || beanNames.contains(beanName)) && beanFactory.containsBean(beanName)) { |
55 | 0 | return beanFactory.getBean(beanName); |
56 | } | |
57 | 0 | return super.getService(serviceName); |
58 | } | |
59 | ||
60 | protected String translateBeanName(QName serviceName) { | |
61 | 0 | return serviceName.toString(); |
62 | } | |
63 | ||
64 | } |