Coverage Report - org.kuali.rice.core.resourceloader.GlobalResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobalResourceLoader
0%
0/64
0%
0/28
2.25
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.resourceloader;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.core.config.ConfigContext;
 26  
 import org.kuali.rice.core.exception.RiceRemoteServiceConnectionException;
 27  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 28  
 import org.kuali.rice.core.reflect.ObjectDefinition;
 29  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 30  
 
 31  
 /**
 32  
  * Wrapper on all the Resource loaders.  This is what programmers typically use to get in the resource loading
 33  
  * framework.
 34  
  *
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  */
 37  0
 public class GlobalResourceLoader {
 38  
 
 39  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class);
 40  
 
 41  0
         private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();
 42  
 
 43  
         private static boolean initializing;
 44  
 
 45  
         public static ResourceLoader getResourceLoader() {
 46  0
                 ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
 47  0
                 return getResourceLoaderCheckParent(classLoader);
 48  
         }
 49  
 
 50  
         private static ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) {
 51  0
             ResourceLoader resourceLoader = getResourceLoader(classLoader);
 52  0
             if (resourceLoader != null && classLoader.getParent() != null) {
 53  0
                 ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
 54  0
                 if (parentResourceLoader != null) {
 55  0
                     resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader);
 56  
                 }
 57  
             }
 58  0
             if (resourceLoader == null && classLoader.getParent() != null) {
 59  0
                 resourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
 60  
             }
 61  0
             return resourceLoader;
 62  
         }
 63  
 
 64  
         public static ResourceLoader getResourceLoader(ClassLoader classloader) {
 65  0
                 return rootResourceLoaders.get(classloader);
 66  
         }
 67  
 
 68  
         public synchronized static void start() throws Exception {
 69  
                 try {
 70  0
                         initializing = true;
 71  0
                         ResourceLoader internalResourceLoader = getResourceLoader();
 72  0
                         if (internalResourceLoader == null) {
 73  0
                                 throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader());
 74  
                         }
 75  0
                         internalResourceLoader.start();
 76  
                 } finally {
 77  0
                         initializing = false;
 78  0
                 }
 79  0
         }
 80  
 
 81  
         public synchronized static void addResourceLoader(ResourceLoader resourceLoader) {
 82  0
                 initialize();
 83  0
                 LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
 84  0
                 if (resourceLoader == null) {
 85  0
                         throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
 86  
                 }
 87  0
                 getResourceLoader().addResourceLoader(resourceLoader);
 88  0
         }
 89  
 
 90  
         public synchronized static void addResourceLoaderFirst(ResourceLoader resourceLoader) {
 91  0
                 initialize();
 92  0
                 LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
 93  0
                 if (resourceLoader == null) {
 94  0
                         throw new ResourceLoaderException("Attempted to add a null resource loader to the Global resource loader.");
 95  
                 }
 96  0
                 getResourceLoader().addResourceLoaderFirst(resourceLoader);
 97  0
         }
 98  
 
 99  
         protected static void initialize() {
 100  0
                 if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
 101  0
                         LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader");
 102  0
                         rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(ConfigContext.getCurrentContextConfig().getServiceNamespace(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
 103  
                 }
 104  0
         }
 105  
 
 106  
         public static ResourceLoader getResourceLoader(QName name) {
 107  0
                 return getResourceLoader().getResourceLoader(name);
 108  
         }
 109  
 
 110  
         /**
 111  
          * Stop the resource loader for the current context classloader.  Don't stop or clear them all
 112  
          * because the stop was issued from the context of a single classloader.
 113  
          *
 114  
          * @throws Exception
 115  
          */
 116  
         public static void stop() throws Exception {
 117  0
                 LOG.info("Stopping the GlobalResourceLoader...");
 118  0
                 if (getResourceLoader() != null) {
 119  0
                         LOG.info("Destroying GlobalResourceLoader");
 120  0
                         getResourceLoader().stop();
 121  0
                         rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader());
 122  
                 }
 123  0
                 LOG.info("...GlobalResourceLoader successfully stopped.");
 124  0
         }
 125  
 
 126  
         public static Object getService(QName serviceName) {
 127  0
                 if (serviceName == null) {
 128  0
                         throw new IllegalArgumentException("The service name must be non-null.");
 129  
                 }
 130  0
                 LOG.debug("GlobalResourceLoader fetching service " + serviceName);
 131  
                 try {
 132  0
                         return getResourceLoader().getService(serviceName);
 133  0
                 } catch (RiceRemoteServiceConnectionException ex) {
 134  0
                         LOG.warn(ex.getMessage());
 135  0
                         return null;
 136  
                 }
 137  
         }
 138  
 
 139  
         public static Object getService(String localServiceName) {
 140  0
                 if (StringUtils.isEmpty(localServiceName)) {
 141  0
                         throw new IllegalArgumentException("The service name must be non-null.");
 142  
                 }
 143  0
                 return getService(new QName(localServiceName));
 144  
         }
 145  
 
 146  
         public static Object getObject(ObjectDefinition objectDefinition) {
 147  0
                 return getResourceLoader().getObject(objectDefinition);
 148  
         }
 149  
 
 150  
         public static boolean isInitialized() {
 151  0
                 return getResourceLoader() != null;
 152  
         }
 153  
 
 154  
         public static void logContents() {
 155  0
                 if (LOG.isInfoEnabled()) {
 156  0
                         LOG.info(getResourceLoader().getContents("", false));
 157  
                 }
 158  0
         }
 159  
 
 160  
         public static boolean isInitializing() {
 161  0
                 return initializing;
 162  
         }
 163  
 
 164  
         public static void setInitializing(boolean initializing) {
 165  0
                 GlobalResourceLoader.initializing = initializing;
 166  0
         }
 167  
 }