Coverage Report - org.kuali.rice.core.framework.resourceloader.BaseResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseResourceLoader
0%
0/66
0%
0/34
2.056
 
 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.framework.resourceloader;
 17  
 
 18  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 19  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 20  
 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
 21  
 import org.kuali.rice.core.api.resourceloader.ResourceLoaderContainer;
 22  
 import org.kuali.rice.core.api.resourceloader.ServiceLocator;
 23  
 import org.kuali.rice.core.api.util.ClassLoaderUtils;
 24  
 
 25  
 import javax.xml.namespace.QName;
 26  
 
 27  
 /**
 28  
  * A simple ResourceLoader implementation which will load objects from the
 29  
  * specified classloader and also locate services in an optional ServiceLocator.
 30  
  *
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  */
 33  
 public class BaseResourceLoader extends ResourceLoaderContainer implements ResourceLoader {
 34  
 
 35  0
         protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BaseResourceLoader.class);
 36  
 
 37  
         private ServiceLocator serviceLocator;
 38  
 
 39  
         private ClassLoader classLoader;
 40  
 
 41  0
         private boolean postProcessContainer = true;
 42  
 
 43  
         public BaseResourceLoader(QName name, ClassLoader classLoader) {
 44  0
                 this(name, classLoader, null);
 45  0
         }
 46  
 
 47  
         public BaseResourceLoader(QName name) {
 48  0
                 this(name, ClassLoaderUtils.getDefaultClassLoader());
 49  0
         }
 50  
 
 51  
         public BaseResourceLoader(QName name, ServiceLocator serviceLocator) {
 52  0
                 this(name, ClassLoaderUtils.getDefaultClassLoader(), serviceLocator);
 53  0
         }
 54  
 
 55  
         public BaseResourceLoader(QName name, ClassLoader classLoader, ServiceLocator serviceLocator) {
 56  0
                 super(name);
 57  0
                 this.classLoader = classLoader;
 58  0
                 this.serviceLocator = serviceLocator;
 59  0
         }
 60  
 
 61  
         public Object getObject(ObjectDefinition objectDefinition) {
 62  
                 // if this resource locator has no NameSpaceURI(applicationId) or the
 63  
                 // objectDefinition has no applicationId just try to find the class here
 64  
                 // or if the applicationId of the object is the same as the applicationId of the locator
 65  0
                 if (getName().getNamespaceURI() == null || getName().getNamespaceURI().equals(objectDefinition.getApplicationId()) ||
 66  
                                 objectDefinition.getApplicationId() == null) {
 67  0
                         Object object = ObjectDefinitionResolver.createObject(objectDefinition, this.classLoader, true);
 68  0
                         if (object != null) {
 69  0
                                 return postProcessObject(objectDefinition, object);
 70  
                         }
 71  
                 }
 72  0
                 Object superObject = super.getObject(objectDefinition);
 73  0
                 return (isPostProcessContainer() ? postProcessObject(objectDefinition, superObject) : superObject);
 74  
         }
 75  
 
 76  
         public Object getService(QName serviceName) {
 77  0
                 if (LOG.isDebugEnabled()) {
 78  0
                         LOG.debug("ResourceLoader " + getName() + " fetching service " + serviceName + getMemStatus());
 79  
                 }
 80  0
                 if (this.serviceLocator != null) {
 81  0
                         if (LOG.isDebugEnabled()) {
 82  0
                                 LOG.debug("Using internal service locator to fetch service " + serviceName);
 83  
                         }
 84  0
                         Object service = this.serviceLocator.getService(serviceName);
 85  0
                         if (service != null) {
 86  0
                                 return postProcessService(serviceName, service);
 87  
                         }
 88  
                 }
 89  0
                 if (LOG.isDebugEnabled()) {
 90  0
                         LOG.debug("ResourceLoader " + getName() + " didn't find service differing to child resource loaders ");
 91  
                 }
 92  0
                 Object superService = super.getService(serviceName);
 93  0
                 return (isPostProcessContainer() ? postProcessService(serviceName, superService) : superService);
 94  
         }
 95  
 
 96  
         public void start() throws Exception {
 97  0
                 if (this.classLoader instanceof Lifecycle) {
 98  0
                         ((Lifecycle)this.classLoader).start();
 99  
                 }
 100  0
                 if (this.serviceLocator != null) {
 101  0
                         LOG.info("Starting ResourceLoader " + this.getName());
 102  0
                         this.serviceLocator.start();
 103  
                 }
 104  0
                 super.start();
 105  0
         }
 106  
 
 107  
         public void stop() throws Exception {
 108  0
                 super.stop();
 109  0
                 if (this.serviceLocator != null) {
 110  0
                         LOG.info("Stopping ResourceLoader " + this.getName());
 111  0
                         this.serviceLocator.stop();
 112  
                 }
 113  0
                 if (this.classLoader instanceof Lifecycle) {
 114  0
                         ((Lifecycle) this.classLoader).stop();
 115  
                 }
 116  0
                 this.classLoader = null;
 117  0
                 this.serviceLocator = null;
 118  0
         }
 119  
 
 120  
         public ClassLoader getClassLoader() {
 121  0
                 return this.classLoader;
 122  
         }
 123  
 
 124  
         public void setClassLoader(ClassLoader classLoader) {
 125  0
                 this.classLoader = classLoader;
 126  0
         }
 127  
 
 128  
         protected Object postProcessObject(ObjectDefinition definition, Object object) {
 129  0
                 return object;
 130  
         }
 131  
 
 132  
         protected Object postProcessService(QName serviceName, Object service) {
 133  0
                 return service;
 134  
         }
 135  
 
 136  
         public boolean isPostProcessContainer() {
 137  0
                 return postProcessContainer;
 138  
         }
 139  
 
 140  
         public void setPostProcessContainer(boolean postProcessContainer) {
 141  0
                 this.postProcessContainer = postProcessContainer;
 142  0
         }
 143  
 
 144  
         public String getContents(String indent, boolean servicePerLine) {
 145  0
                 String contents = indent + this + "\n";
 146  
 
 147  0
                 if (this.serviceLocator != null) {
 148  0
                         contents += this.serviceLocator.getContents(indent + "+++", servicePerLine);
 149  
                 }
 150  
 
 151  0
                 for (ResourceLoader resourceLoader : this.getResourceLoaders()) {
 152  0
                         contents += resourceLoader.getContents(indent + "+++", servicePerLine);
 153  
                 }
 154  
 
 155  0
                 return contents;
 156  
         }
 157  
 
 158  
         private String getMemStatus() {
 159  0
                 return "\n############################################################## \n" + "# " + dumpMemory() + "\n##############################################################\n";
 160  
         }
 161  
 
 162  
         private String dumpMemory() {
 163  0
                 long total = Runtime.getRuntime().totalMemory() / 1024;
 164  0
                 long free = Runtime.getRuntime().freeMemory() / 1024;
 165  0
                 long max = Runtime.getRuntime().maxMemory() / 1024;
 166  0
                 return "[Memory] max: " + max + "K, total: " + total + "K, free: " + free + "K, used: " + (total - free) + "K";
 167  
         }
 168  
 
 169  
         public ServiceLocator getServiceLocator() {
 170  0
                 return this.serviceLocator;
 171  
         }
 172  
 }