Coverage Report - org.kuali.rice.core.api.resourceloader.GlobalResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobalResourceLoader
0%
0/70
0%
0/32
2.294
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.core.api.resourceloader;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.config.CoreConfigHelper;
 20  
 import org.kuali.rice.core.api.exception.RiceRemoteServiceConnectionException;
 21  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 22  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 23  
 import org.kuali.rice.core.api.util.ClassLoaderUtils;
 24  
 
 25  
 import javax.xml.namespace.QName;
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Wrapper on all the Resource loaders.  This is what programmers typically use to get in the resource loading
 31  
  * framework.
 32  
  *
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  0
 public class GlobalResourceLoader {
 36  
 
 37  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalResourceLoader.class);
 38  
 
 39  0
         private static Map<ClassLoader, ResourceLoader> rootResourceLoaders = new HashMap<ClassLoader, ResourceLoader>();
 40  
 
 41  
         private static boolean initializing;
 42  
 
 43  
         public static synchronized ResourceLoader getResourceLoader() {
 44  0
                 ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
 45  0
                 return getResourceLoaderCheckParent(classLoader);
 46  
         }
 47  
 
 48  
         private static synchronized ResourceLoader getResourceLoaderCheckParent(ClassLoader classLoader) {
 49  0
         ResourceLoader resourceLoader = getResourceLoader(classLoader);
 50  
 
 51  0
         if (resourceLoader != null && classLoader.getParent() != null) {
 52  0
             ResourceLoader parentResourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
 53  
 
 54  0
             if (parentResourceLoader != null) {
 55  0
                 resourceLoader = new ParentChildResourceLoader(parentResourceLoader, resourceLoader);
 56  
                     }
 57  
             }
 58  
 
 59  0
             if (resourceLoader == null && classLoader.getParent() != null) {
 60  0
                     resourceLoader = getResourceLoaderCheckParent(classLoader.getParent());
 61  
             }
 62  0
             return resourceLoader;
 63  
         }
 64  
 
 65  
         public static synchronized ResourceLoader getResourceLoader(ClassLoader classloader) {
 66  0
                 return rootResourceLoaders.get(classloader);
 67  
         }
 68  
 
 69  
         public static synchronized void start() throws Exception {
 70  
                 try {
 71  0
                         initializing = true;
 72  0
                         ResourceLoader internalResourceLoader = getResourceLoader();
 73  0
                         if (internalResourceLoader == null) {
 74  0
                                 throw new RiceRuntimeException("Cannot start GlobalResourceLoader because no resource loaders have been added for the current ContextClassLoader :" + Thread.currentThread().getContextClassLoader());
 75  
                         }
 76  0
                         internalResourceLoader.start();
 77  
                 } finally {
 78  0
                         initializing = false;
 79  0
                 }
 80  0
         }
 81  
 
 82  
         public static synchronized void addResourceLoader(ResourceLoader resourceLoader) {
 83  0
                 initialize();
 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
                 LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
 88  0
                 getResourceLoader().addResourceLoader(resourceLoader);
 89  0
         }
 90  
 
 91  
         public static synchronized void addResourceLoaderFirst(ResourceLoader resourceLoader) {
 92  0
                 initialize();
 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
                 LOG.info("Adding ResourceLoader " + resourceLoader.getName() + " to GlobalResourceLoader");
 97  0
                 getResourceLoader().addResourceLoaderFirst(resourceLoader);
 98  0
         }
 99  
 
 100  
         protected static synchronized void initialize() {
 101  0
                 if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) == null) {
 102  0
                         LOG.info("Creating CompositeResourceLoader in GlobalResourceLoader");
 103  0
                         rootResourceLoaders.put(ClassLoaderUtils.getDefaultClassLoader(), new ResourceLoaderContainer(new QName(CoreConfigHelper.getApplicationId(), ResourceLoader.ROOT_RESOURCE_LOADER_NAME)));
 104  
                 }
 105  0
         }
 106  
 
 107  
         public static synchronized ResourceLoader getResourceLoader(QName name) {
 108  0
                 return getResourceLoader().getResourceLoader(name);
 109  
         }
 110  
 
 111  
         /**
 112  
          * Stop the resource loader for the current context classloader.  Don't stop or clear them all
 113  
          * because the stop was issued from the context of a single classloader.
 114  
          *
 115  
          * @throws Exception
 116  
          */
 117  
         public static synchronized void stop() throws Exception {
 118  0
                 LOG.info("Stopping the GlobalResourceLoader...");
 119  0
                 if (getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()) != null) {
 120  0
                         LOG.info("Destroying GlobalResourceLoader");
 121  0
                         getResourceLoader(ClassLoaderUtils.getDefaultClassLoader()).stop();
 122  0
                         rootResourceLoaders.remove(ClassLoaderUtils.getDefaultClassLoader());
 123  
                 }
 124  0
                 LOG.info("...GlobalResourceLoader successfully stopped.");
 125  0
         }
 126  
 
 127  
         public static synchronized <T extends Object> T getService(QName serviceName) {
 128  0
                 if (serviceName == null) {
 129  0
                         throw new IllegalArgumentException("The service name must be non-null.");
 130  
                 }
 131  0
                 LOG.debug("GlobalResourceLoader fetching service " + serviceName);
 132  
                 try {
 133  0
                         return getResourceLoader().<T>getService(serviceName);
 134  0
                 } catch (RiceRemoteServiceConnectionException ex) {
 135  0
                         LOG.warn(ex.getMessage());
 136  0
                         return null;
 137  
                 }
 138  
         }
 139  
 
 140  
         public static synchronized <T extends Object> T getService(String localServiceName) {
 141  0
                 if (StringUtils.isEmpty(localServiceName)) {
 142  0
                         throw new IllegalArgumentException("The service name must be non-null.");
 143  
                 }
 144  0
                 return GlobalResourceLoader.<T>getService(new QName(localServiceName));
 145  
         }
 146  
 
 147  
         public static synchronized <T extends Object> T getObject(ObjectDefinition objectDefinition) {
 148  0
                 return getResourceLoader().<T>getObject(objectDefinition);
 149  
         }
 150  
 
 151  
         public static synchronized boolean isInitialized() {
 152  0
                 return getResourceLoader() != null;
 153  
         }
 154  
 
 155  
         public static synchronized void logContents() {
 156  0
                 if (LOG.isInfoEnabled()) {
 157  0
                         LOG.info(getResourceLoader().getContents("", false));
 158  
                 }
 159  0
         }
 160  
         
 161  
         public static synchronized void logAllContents() {
 162  0
                 if (LOG.isInfoEnabled()) {
 163  0
                         LOG.info("######################### Logging All Contents ###########################");
 164  0
                         for (ResourceLoader rl : rootResourceLoaders.values()) {
 165  0
                                 LOG.info("Logging contents for ResourceLoader: " + rl.getName() + "\n" + rl.getContents("  ", true));
 166  
                         }
 167  0
                         LOG.info("###################### Done Logging All Contents #########################");
 168  
                 }
 169  0
         }
 170  
 
 171  
         public static synchronized boolean isInitializing() {
 172  0
                 return initializing;
 173  
         }
 174  
 
 175  
         public static synchronized void setInitializing(boolean initializing) {
 176  0
                 GlobalResourceLoader.initializing = initializing;
 177  0
         }
 178  
 }