/*
 * Copyright 2006-2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kuali.rice.kns.config;

import org.kuali.rice.core.api.config.ConfigurationException;
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * Exports services in the {@link org.kuali.rice.core.api.resourceloader.GlobalResourceLoader} as beans available to Spring.
 * 
 * @author Kuali Rice Team (rice.collab@kuali.org)
 *
 */
public class GlobalResourceLoaderServiceFactoryBean implements FactoryBean<Object>, InitializingBean {

	private String serviceName;
	private boolean singleton;
	private boolean mustExist;
	
	public GlobalResourceLoaderServiceFactoryBean() {
		this.mustExist = true;
	}
	
	public Object getObject() throws Exception {
		Object service = GlobalResourceLoader.getService(this.getServiceName());
		if (mustExist && service == null) {
			throw new IllegalStateException("Service must exist and no service could be located with name: " + this.getServiceName());
		}
		return service;
	}

	public Class<?> getObjectType() {
		return Object.class;
	}

	public boolean isSingleton() {
		return singleton;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public void setSingleton(boolean singleton) {
		this.singleton = singleton;
	}
	
	public boolean isMustExist() {
		return mustExist;
	}
	
	public void setMustExist(boolean mustExist) {
		this.mustExist = mustExist;
	}
	

	public void afterPropertiesSet() throws Exception {
		if (this.getServiceName() == null) {
			throw new ConfigurationException("No serviceName given.");
		}
	}

}

@@ -651,16 +651,65 @@
 onfig;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 import o
@@ -762,16 +762,16 @@
 eption;%0A
-
 import o
@@ -1323,16 +1323,129 @@
 stExist;
+%0A%0A%09// used to prevent a stack overflow when trying to get the service%0A%09private boolean isFetchingService = false;
 %0A%09%0A%09publ
@@ -1566,16 +1566,157 @@
 ption %7B%0A
+%09%09if (isFetchingService) return null; // we already have been invoked, don't recurse, just return null.%0A%09%09isFetchingService = true;%0A%09%09try %7B%0A%09
 %09%09Object
@@ -1784,16 +1784,17 @@
 e());%0A%09%09
+%09
 if (must
@@ -1813,32 +1813,33 @@
 vice == null) %7B%0A
+%09
 %09%09%09throw new Ill
@@ -1950,18 +1950,20 @@
 e());%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return
@@ -1972,16 +1972,64 @@
 ervice;%0A
+%09%09%7D finally %7B%0A%09%09%09isFetchingService = false;%0A%09%09%7D%0A
 %09%7D%0A%0A%09pub

Fix infinite recursion problem w/ GlobalResourceLoaderServiceFactoryBean