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