View Javadoc

1   /**
2    * Copyright 2005-2011 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.krad.config;
17  
18  import org.kuali.rice.core.api.config.ConfigurationException;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.springframework.beans.factory.FactoryBean;
21  import org.springframework.beans.factory.InitializingBean;
22  
23  /**
24   * Exports services in the {@link org.kuali.rice.core.api.resourceloader.GlobalResourceLoader} as beans available to Spring.
25   * 
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   *
28   */
29  public class GlobalResourceLoaderServiceFactoryBean implements FactoryBean<Object>, InitializingBean {
30  
31  	private String serviceName;
32  	private boolean singleton;
33  	private boolean mustExist;
34  
35  	// used to prevent a stack overflow when trying to get the service
36  	private boolean isFetchingService = false;
37  	
38  	public GlobalResourceLoaderServiceFactoryBean() {
39  		this.mustExist = true;
40  	}
41  	
42  	public Object getObject() throws Exception {
43  		if (isFetchingService) return null; // we already have been invoked, don't recurse, just return null.
44  		isFetchingService = true;
45  		try {
46  			Object service = GlobalResourceLoader.getService(this.getServiceName());
47  			if (mustExist && service == null) {
48  				throw new IllegalStateException("Service must exist and no service could be located with name: " + this.getServiceName());
49  			}
50  			return service;
51  		} finally {
52  			isFetchingService = false;
53  		}
54  	}
55  
56  	public Class<?> getObjectType() {
57  		return Object.class;
58  	}
59  
60  	public boolean isSingleton() {
61  		return singleton;
62  	}
63  
64  	public String getServiceName() {
65  		return serviceName;
66  	}
67  
68  	public void setServiceName(String serviceName) {
69  		this.serviceName = serviceName;
70  	}
71  
72  	public void setSingleton(boolean singleton) {
73  		this.singleton = singleton;
74  	}
75  	
76  	public boolean isMustExist() {
77  		return mustExist;
78  	}
79  	
80  	public void setMustExist(boolean mustExist) {
81  		this.mustExist = mustExist;
82  	}
83  	
84  
85  	public void afterPropertiesSet() throws Exception {
86  		if (this.getServiceName() == null) {
87  			throw new ConfigurationException("No serviceName given.");
88  		}
89  	}
90  
91  }