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