Coverage Report - org.kuali.rice.kew.plugin.PluginClassLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginClassLoader
0%
0/100
0%
0/52
2.714
 
 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.kew.plugin;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 import java.net.MalformedURLException;
 22  
 import java.net.URL;
 23  
 import java.net.URLClassLoader;
 24  
 import java.util.Enumeration;
 25  
 
 26  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 27  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 28  
 import org.kuali.rice.kew.util.SimpleEnumeration;
 29  
 
 30  
 
 31  
 /**
 32  
  * A simple class loader implementation which looks at itself before delegating to its parent.
 33  
  *
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  
 public class PluginClassLoader extends URLClassLoader implements Lifecycle {//implements Modifiable {
 37  
     static final String CLASSES_DIR = "classes";
 38  
     static final String LIB_DIR = "lib";
 39  0
     private static final String[] SYSTEM_CLASSES = new String[] { "java.", "javax.servlet.", "javax.xml.", "javax.management.", "org.xml.", "org.w3c." };
 40  
 
 41  
     //private ModificationTracker modTracker = new ModificationTracker();
 42  
     //this is purposely typed.
 43  
     private PluginConfig config;
 44  0
     private boolean started = false;
 45  
 
 46  
     public PluginClassLoader() {
 47  0
         super(new URL[0]);
 48  0
     }
 49  
 
 50  
     public PluginClassLoader(ClassLoader parent) {
 51  0
         super(new URL[0], parent);
 52  0
     }
 53  
 
 54  
     public PluginClassLoader(ClassLoader parent, File sharedDirectory, File pluginDirectory) throws MalformedURLException {
 55  0
         super(new URL[0], parent);
 56  0
         if (sharedDirectory != null) {
 57  0
             addClassesDirectory(new File(sharedDirectory, CLASSES_DIR));
 58  0
             addLibDirectory(new File(sharedDirectory, LIB_DIR));
 59  
         }
 60  0
         addClassesDirectory(new File(pluginDirectory, CLASSES_DIR));
 61  0
         addLibDirectory(new File(pluginDirectory, LIB_DIR));
 62  0
     }
 63  
 
 64  
     public void addClassesDirectory(File classesDir) throws MalformedURLException {
 65  0
         if (classesDir != null && classesDir.isDirectory()) {
 66  0
             addURL(classesDir.toURI().toURL());
 67  
         }
 68  0
     }
 69  
 
 70  
     public void addLibDirectory(File libDir) throws MalformedURLException {
 71  0
         File[] jars = PluginUtils.findJars(libDir);
 72  0
         for (int index = 0; index < jars.length; index++) {
 73  0
             addURL(jars[index].toURI().toURL());
 74  
         }
 75  0
     }
 76  
 
 77  
     public Class loadClass(String className) throws ClassNotFoundException {
 78  0
         return loadClass(className, false);
 79  
     }
 80  
 
 81  
     public void addURL(URL url) {
 82  0
         super.addURL(url);
 83  
         //modTracker.addURL(url);
 84  0
     }
 85  
 
 86  
     //public boolean isModified() {
 87  
     //    return modTracker.isModified();
 88  
     //}
 89  
 
 90  
     public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
 91  0
         Class loadedClass = loadExistingClass(name, resolve);
 92  0
         if (loadedClass != null) {
 93  0
             return loadedClass;
 94  
         }
 95  0
         loadedClass = loadSystemClass(name, resolve);
 96  0
         if (loadedClass != null) {
 97  0
             return loadedClass;
 98  
         }
 99  0
         loadedClass = loadLocalClass(name, resolve);
 100  0
         if (loadedClass != null) {
 101  0
             return loadedClass;
 102  
         }
 103  0
         loadedClass = loadParentClass(name, resolve);
 104  0
         if (loadedClass != null) {
 105  0
             return loadedClass;
 106  
         }
 107  0
         throw new ClassNotFoundException(name);
 108  
     }
 109  
 
 110  
     public URL getResource(String name) {
 111  0
         URL resource = findResource(name);
 112  0
         if (resource == null) {
 113  0
             resource = getParent().getResource(name);
 114  
         }
 115  0
         return resource;
 116  
     }
 117  
 
 118  
     public Enumeration<URL> getResources(String name) throws IOException {
 119  0
             Enumeration<URL> localResources = findResources(name);
 120  0
             Enumeration<URL> parentResources = getParent().getResources(name);
 121  0
             return new SimpleEnumeration<URL>(localResources, parentResources);
 122  
     }
 123  
 
 124  
 
 125  
 
 126  
     private Class loadExistingClass(String name, boolean resolve) {
 127  0
         Class loadedClass = findLoadedClass(name);
 128  0
         if (loadedClass != null && resolve) {
 129  0
             resolveClass(loadedClass);
 130  
         }
 131  0
         return loadedClass;
 132  
     }
 133  
 
 134  
     private Class loadSystemClass(String name, boolean resolve) {
 135  0
             Class loadedClass = null;
 136  0
             if (isSystemClass(name)) {
 137  
                     try {
 138  0
                             loadedClass = getSystemClassLoader().loadClass(name);
 139  0
                             if (loadedClass != null && resolve) {
 140  0
                                     resolveClass(loadedClass);
 141  
                             }
 142  0
                     } catch (ClassNotFoundException e) {
 143  
                             // not found in system class loader
 144  0
                     }
 145  
             }
 146  0
                 return loadedClass;
 147  
     }
 148  
 
 149  
     private Class loadLocalClass(String name, boolean resolve) {
 150  0
         Class loadedClass = null;
 151  
         try {
 152  0
             loadedClass = findClass(name);
 153  0
             if (loadedClass != null && resolve) {
 154  0
                 resolveClass(loadedClass);
 155  
             }
 156  0
         } catch (ClassNotFoundException e) {
 157  
             // not found locally
 158  0
         }
 159  0
         return loadedClass;
 160  
     }
 161  
 
 162  
     private Class loadParentClass(String name, boolean resolve) {
 163  0
         Class loadedClass = null;
 164  
         try {
 165  0
             loadedClass = getParent().loadClass(name);
 166  0
             if (loadedClass != null && resolve) {
 167  0
                 resolveClass(loadedClass);
 168  
             }
 169  0
         } catch (ClassNotFoundException e) {
 170  
             // not found in parent
 171  0
         }
 172  0
         return loadedClass;
 173  
     }
 174  
 
 175  
     /**
 176  
      * This method modeled on the isSystemPath method in Jetty's ContextLoader.
 177  
      *
 178  
      * When loading classes from the system classloader, we really only want to load certain classes
 179  
      * from there so this will tell us whether or not the class name given is one we want to load
 180  
      * from the system classloader.
 181  
      */
 182  
     private boolean isSystemClass(String name) {
 183  0
             name = name.replace('/','.');
 184  0
         while(name.startsWith(".")) {
 185  0
                 name=name.substring(1);
 186  
         }
 187  0
         for (int index = 0; index < SYSTEM_CLASSES.length; index++) {
 188  0
                 String systemClass = SYSTEM_CLASSES[index];
 189  0
                 if (systemClass.endsWith(".")) {
 190  0
                         if (name.startsWith(systemClass)) {
 191  0
                                 return true;
 192  
                         }
 193  
                 }
 194  0
                 else if (name.equals(systemClass)) {
 195  0
                         return true;
 196  
                 }
 197  
         }
 198  0
         return false;
 199  
     }
 200  
 
 201  
     public String toString() {
 202  0
         StringBuffer sb = new StringBuffer("[PluginClassLoader: urls=");
 203  0
         URL[] urls = getURLs();
 204  0
         if (urls == null) {
 205  0
             sb.append("null");
 206  
         } else {
 207  0
             for (int i = 0; i < urls.length; i++) {
 208  0
                 sb.append(urls[i]);
 209  0
                 sb.append(",");
 210  
             }
 211  
             // remove trailing comma
 212  0
             if (urls.length > 1) {
 213  0
                 sb.setLength(sb.length() - 1);
 214  
             }
 215  
         }
 216  0
         sb.append("]");
 217  0
         return sb.toString();
 218  
     }
 219  
 
 220  
         public PluginConfig getConfig() {
 221  0
                 return config;
 222  
         }
 223  
 
 224  
         public void setConfig(PluginConfig config) {
 225  0
                 this.config = config;
 226  0
         }
 227  
 
 228  
         public void start() {
 229  0
                 started = true;
 230  0
         }
 231  
 
 232  
         public void stop() {
 233  0
                 config = null;
 234  0
                 started = false;
 235  0
         }
 236  
 
 237  
         public boolean isStarted() {
 238  0
                 return started;
 239  
         }
 240  
 }