View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.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.core.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 (rice.collab@kuali.org)
30   */
31  public class SpringBeanFactoryResourceLoader extends BaseResourceLoader implements BeanFactoryAware {
32  
33  	private BeanFactory beanFactory;
34  
35  	public SpringBeanFactoryResourceLoader() {
36  		this(new QName("", "BeanFactoryResourceLoader"));
37  	}
38  
39  	public SpringBeanFactoryResourceLoader(QName name) {
40  		super(name);
41  	}
42  
43  	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
44  		this.beanFactory = beanFactory;
45  	}
46  
47  	protected BeanFactory getBeanFactory() {
48  		return this.beanFactory;
49  	}
50  
51  	@Override
52  	public Object getService(QName serviceName) {
53  		String resolvedServiceName = resolveServiceName(serviceName);
54  		if (this.beanFactory.containsBean(resolvedServiceName)) {
55  			Object service = this.beanFactory.getBean(resolvedServiceName);
56  			if (service != null) {
57  				return postProcessService(serviceName, service);
58  			}
59  		}
60  		return super.getService(serviceName);
61  	}
62  
63  	/**
64  	 * Resolves the given QName service name to a String representation which is used
65  	 * to lookup the service in Spring.  Default implementation simply calls toString()
66  	 * on the QName.
67  	 */
68  	protected String resolveServiceName(QName serviceName) {
69  		return serviceName.toString();
70  	}
71  
72  
73  
74  }