01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.kuali.rice.resourceloader;
18:
19: import javax.xml.namespace.QName;
20:
21: import org.springframework.beans.BeansException;
22: import org.springframework.beans.factory.BeanFactory;
23: import org.springframework.beans.factory.BeanFactoryAware;
24:
25: /**
26: * A ResourceLoader that is BeanFactoryAware and can be wired inside of Spring to provide
27: * resource loading capabilities to that Spring BeanFactory.
28: *
29: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
30: */
31: public class SpringBeanFactoryResourceLoader extends BaseResourceLoader
32: implements BeanFactoryAware {
33:
34: private BeanFactory beanFactory;
35:
36: public SpringBeanFactoryResourceLoader() {
37: this (new QName("", "BeanFactoryResourceLoader"));
38: }
39:
40: public SpringBeanFactoryResourceLoader(QName name) {
41: super (name);
42: }
43:
44: public void setBeanFactory(BeanFactory beanFactory)
45: throws BeansException {
46: this .beanFactory = beanFactory;
47: }
48:
49: protected BeanFactory getBeanFactory() {
50: return this .beanFactory;
51: }
52:
53: @Override
54: public Object getService(QName serviceName) {
55: String resolvedServiceName = resolveServiceName(serviceName);
56: if (this .beanFactory.containsBean(resolvedServiceName)) {
57: Object service = this .beanFactory.getBean(serviceName
58: .toString());
59: if (service != null) {
60: return postProcessService(serviceName, service);
61: }
62: }
63: return super .getService(serviceName);
64: }
65:
66: /**
67: * Resolves the given QName service name to a String representation which is used
68: * to lookup the service in Spring. Default implementation simply calls toString()
69: * on the QName.
70: */
71: protected String resolveServiceName(QName serviceName) {
72: return serviceName.toString();
73: }
74:
75: }
|