|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.core.api.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.lifecycle.Lifecycle;  | 
  |  23 |     | 
   import org.kuali.rice.core.api.reflect.ObjectDefinition;  | 
  |  24 |     | 
   import org.kuali.rice.core.api.reflect.ObjectDefinition;  | 
  |  25 |     | 
   import org.kuali.rice.core.util.ClassLoaderUtils;  | 
  |  26 |     | 
     | 
  |  27 |     | 
     | 
  |  28 |     | 
     | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  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 |     | 
                     | 
  |  63 |     | 
                     | 
  |  64 |     | 
                     | 
  |  65 |    0 |                    if (getName().getNamespaceURI() == null || getName().getNamespaceURI().equals(objectDefinition.getServiceNamespace()) ||  | 
  |  66 |     | 
                                   objectDefinition.getServiceNamespace() == 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 |     | 
   }  |