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