View Javadoc

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