View Javadoc

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