<commit_before>/*
 * 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.core.resourceloader;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.config.ConfigContext;
import org.kuali.rice.core.exception.RiceRemoteServiceConnectionException;
import org.kuali.rice.core.exception.RiceRuntimeException;
import org.kuali.rice.core.reflect.ObjectDefinition;
import org.kuali.rice.core.util.ClassLoaderUtils;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;

/**
 * Wrapper on all the Resource loaders.  This is what programmers typically use to get in the resource loading
 * framework.
 *
 * @author Kuali Rice Team (<EMAIL>)
 */
public class GlobalResourceLoader {

	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class);

	private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();

	private static boolean initializing;

	public static ResourceLoader getResourceLoader() {
		ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
		return getResourceLoaderCheckParent(classLoader);
	}

	private static ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) {
	    System.err.println("classloader: " + classLoader);
        System.err.println("parent classloader: " + (classLoader != null ? classLoader.getParent() : null));

        ResourceLoader resourceLoader = getResourceLoader(classLoader);
	    System.err.println("resourceLoader: " + resourceLoader);

        if (resourceLoader != null && classLoader.getParent() != null) {
            ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
            System.err.println("parentResourceLoader: " + parentResourceLoader);

            if (parentResourceLoader != null) {
                resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader);
                System.err.println("new resourceloader from parent: " + resourceLoader);
		    }
	    }

	    if (resourceLoader == null && classLoader.getParent() != null) {
		    resourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
            System.err.println("last condition resourceloader: " + resourceLoader);
	    }
	    return resourceLoader;
	}

	public static ResourceLoader getResourceLoader(ClassLoader classloader) {
		return rootResourceLoaders.get(classloader);
	}

	public synchronized static void start() throws Exception {
		try {
			initializing = true;
			ResourceLoader internalResourceLoader = getResourceLoader();
			if (internalResourceLoader == null) {
				throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader());
			}
			internalResourceLoader.start();
		} finally {
			initializing = false;
		}
	}

	public synchronized static void addResourceLoader(ResourceLoader resourceLoader) {
		initialize();
		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
		if (resourceLoader == null) {
			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
		}
		getResourceLoader().addResourceLoader(resourceLoader);
	}

	public synchronized static void addResourceLoaderFirst(ResourceLoader resourceLoader) {
		initialize();
		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
		if (resourceLoader == null) {
			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
		}
		getResourceLoader().addResourceLoaderFirst(resourceLoader);
	}

	protected static void initialize() {
		if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
			LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader");
			rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
		}
	}

	public static ResourceLoader getResourceLoader(QName name) {
		return getResourceLoader().getResourceLoader(name);
	}

	/**
	 * Stop the resource loader for the current context classloader.  Don't stop or clear them all
	 * because the stop was issued from the context of a single classloader.
	 *
	 * @throws Exception
	 */
	public static void stop() throws Exception {
		LOG.info("Stopping the GlobalResourceLoader...");
		if (getResourceLoader() != null) {
			LOG.info("Destroying GlobalResourceLoader");
			getResourceLoader().stop();
			rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader());
		}
		LOG.info("...GlobalResourceLoader successfully stopped.");
	}

	public static <T extends Object> T getService(QName serviceName) {
		if (serviceName == null) {
			throw new IllegalArgumentException("The service name must be non-null.");
		}
		LOG.debug("GlobalResourceLoader fetching service " + serviceName);
		try {
			return getResourceLoader().<T>getService(serviceName);
		} catch (RiceRemoteServiceConnectionException ex) {
			LOG.warn(ex.getMessage());
			return null;
		}
	}

	public static <T extends Object> T getService(String localServiceName) {
		if (StringUtils.isEmpty(localServiceName)) {
			throw new IllegalArgumentException("The service name must be non-null.");
		}
		return GlobalResourceLoader.<T>getService(new QName(localServiceName));
	}

	public static <T extends Object> T getObject(ObjectDefinition objectDefinition) {
		return getResourceLoader().<T>getObject(objectDefinition);
	}

	public static boolean isInitialized() {
		return getResourceLoader() != null;
	}

	public static void logContents() {
		if (LOG.isInfoEnabled()) {
			LOG.info(getResourceLoader().getContents("", false));
		}
	}

	public static boolean isInitializing() {
		return initializing;
	}

	public static void setInitializing(boolean initializing) {
		GlobalResourceLoader.initializing = initializing;
	}
}<commit_msg>KULRICE-4900: fixing several synchronization errors<commit_after>/*
 * 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.core.resourceloader;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.core.config.ConfigContext;
import org.kuali.rice.core.exception.RiceRemoteServiceConnectionException;
import org.kuali.rice.core.exception.RiceRuntimeException;
import org.kuali.rice.core.reflect.ObjectDefinition;
import org.kuali.rice.core.util.ClassLoaderUtils;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;

/**
 * Wrapper on all the Resource loaders.  This is what programmers typically use to get in the resource loading
 * framework.
 *
 * @author Kuali Rice Team (<EMAIL>)
 */
public class GlobalResourceLoader {

	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class);

	private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();

	private static boolean initializing;

	public static synchronized ResourceLoader getResourceLoader() {
		ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
		return getResourceLoaderCheckParent(classLoader);
	}

	private static synchronized ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) {
	    System.err.println("classloader: " + classLoader);
        System.err.println("parent classloader: " + (classLoader != null ? classLoader.getParent() : null));

        ResourceLoader resourceLoader = getResourceLoader(classLoader);
	    System.err.println("resourceLoader: " + resourceLoader);

        if (resourceLoader != null && classLoader.getParent() != null) {
            ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
            System.err.println("parentResourceLoader: " + parentResourceLoader);

            if (parentResourceLoader != null) {
                resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader);
                System.err.println("new resourceloader from parent: " + resourceLoader);
		    }
	    }

	    if (resourceLoader == null && classLoader.getParent() != null) {
		    resourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
            System.err.println("last condition resourceloader: " + resourceLoader);
	    }
	    return resourceLoader;
	}

	public static synchronized ResourceLoader getResourceLoader(ClassLoader classloader) {
		return rootResourceLoaders.get(classloader);
	}

	public static synchronized void start() throws Exception {
		try {
			initializing = true;
			ResourceLoader internalResourceLoader = getResourceLoader();
			if (internalResourceLoader == null) {
				throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader());
			}
			internalResourceLoader.start();
		} finally {
			initializing = false;
		}
	}

	public static synchronized void addResourceLoader(ResourceLoader resourceLoader) {
		initialize();
		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
		if (resourceLoader == null) {
			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
		}
		getResourceLoader().addResourceLoader(resourceLoader);
	}

	public static synchronized void addResourceLoaderFirst(ResourceLoader resourceLoader) {
		initialize();
		LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
		if (resourceLoader == null) {
			throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
		}
		getResourceLoader().addResourceLoaderFirst(resourceLoader);
	}

	protected static synchronized void initialize() {
		if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
			LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader");
			rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
		}
	}

	public static synchronized ResourceLoader getResourceLoader(QName name) {
		return getResourceLoader().getResourceLoader(name);
	}

	/**
	 * Stop the resource loader for the current context classloader.  Don't stop or clear them all
	 * because the stop was issued from the context of a single classloader.
	 *
	 * @throws Exception
	 */
	public static synchronized void stop() throws Exception {
		LOG.info("Stopping the GlobalResourceLoader...");
		if (getResourceLoader() != null) {
			LOG.info("Destroying GlobalResourceLoader");
			getResourceLoader().stop();
			rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader());
		}
		LOG.info("...GlobalResourceLoader successfully stopped.");
	}

	public static synchronized <T extends Object> T getService(QName serviceName) {
		if (serviceName == null) {
			throw new IllegalArgumentException("The service name must be non-null.");
		}
		LOG.debug("GlobalResourceLoader fetching service " + serviceName);
		try {
			return getResourceLoader().<T>getService(serviceName);
		} catch (RiceRemoteServiceConnectionException ex) {
			LOG.warn(ex.getMessage());
			return null;
		}
	}

	public static synchronized <T extends Object> T getService(String localServiceName) {
		if (StringUtils.isEmpty(localServiceName)) {
			throw new IllegalArgumentException("The service name must be non-null.");
		}
		return GlobalResourceLoader.<T>getService(new QName(localServiceName));
	}

	public static synchronized <T extends Object> T getObject(ObjectDefinition objectDefinition) {
		return getResourceLoader().<T>getObject(objectDefinition);
	}

	public static synchronized boolean isInitialized() {
		return getResourceLoader() != null;
	}

	public static synchronized void logContents() {
		if (LOG.isInfoEnabled()) {
			LOG.info(getResourceLoader().getContents("", false));
		}
	}

	public static synchronized boolean isInitializing() {
		return initializing;
	}

	public static synchronized void setInitializing(boolean initializing) {
		GlobalResourceLoader.initializing = initializing;
	}
}
{'fraction_non_alphanumeric': 0.09422470096132678, 'fraction_numerical': 0.0022015117047038965, 'mean_word_length': 4.969338589575121, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003588', 'n_tokens_mistral': 3706, 'n_tokens_neox': 3445, 'n_words': 973}