com.codemonster.surinam.core.loader
Class GreedyClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by com.codemonster.surinam.core.loader.GreedyClassLoader

public class GreedyClassLoader
extends URLClassLoader

This is Surinam's primary class loader. The goal of this loader is to be as greedy as possible so it tries to find the class locally on it's own path before delegating to its parent. This helps maintain the isolation necessary for Hyper-Dynamic behavior.

Rather than writing a completely custom class loader, we override a standard loader and set the parent to null. This causes it to default to the System loader (where we should not be finding any application classes) and therefore causes parent-level loading to fail. Then we try to load the class locally and failing that, we delegate to the secondary parent, which we expect to succeed.

We have focused mainly on constructors to set up the URLs, we allow the inherited method to poke through to 'add' a new URL. The end result is the ability to add to the classpath at runtime.

Author:
Samuel Provencher

Constructor Summary
GreedyClassLoader()
          This is an empty constructor that adds nothing to the classpath, and has no parent loader.
GreedyClassLoader(ClassLoader secondaryParent)
          This is a simplified constructor that does not require the URL array for times when you are not adding anything to the classpath, is most of the time.
GreedyClassLoader(URL[] urls)
          Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader.
GreedyClassLoader(URL[] urls, ClassLoader secondaryParent)
          Constructs a new URLClassLoader for the given URLs.
 
Method Summary
 void addURL(URL url)
          This exposes a protected method that adds a URL to the classpath used for loading, defining and reconciling classes.
 void addURLs(URL[] urls)
          This takes an array of URLs and iterates over them adding them to the classpath.
 String getName()
           
 Class<?> loadClass(String name)
          Here, we override the main class so we can call the secondary loader second.
 void setName(String name)
           
 
Methods inherited from class java.net.URLClassLoader
findResource, findResources, getURLs, newInstance, newInstance
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GreedyClassLoader

public GreedyClassLoader()
This is an empty constructor that adds nothing to the classpath, and has no parent loader.


GreedyClassLoader

public GreedyClassLoader(URL[] urls)
Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.

Parameters:
urls - the URLs from which to load classes and resources

GreedyClassLoader

public GreedyClassLoader(ClassLoader secondaryParent)
This is a simplified constructor that does not require the URL array for times when you are not adding anything to the classpath, is most of the time.

Parameters:
secondaryParent - This is a reference to the parent of the loader, setting this to 'null' will bypass all class loaders and default to the System loader as the parent (as defined by the VM).

GreedyClassLoader

public GreedyClassLoader(URL[] urls,
                         ClassLoader secondaryParent)
Constructs a new URLClassLoader for the given URLs. The URLs will be searched in the order specified for classes and resources FIRST and then will search in the specified parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.

Parameters:
urls - the URLs from which to load classes and resources
secondaryParent - the parent class loader for delegation
Method Detail

loadClass

public final Class<?> loadClass(String name)
                         throws ClassNotFoundException
Here, we override the main class so we can call the secondary loader second.

Overrides:
loadClass in class ClassLoader
Parameters:
name - The name of the class to load.
Returns:
returns a reference to the class that was loaded.
Throws:
ClassNotFoundException

addURLs

public final void addURLs(URL[] urls)
This takes an array of URLs and iterates over them adding them to the classpath.

Parameters:
urls - The array of URLs to be added to the classpath.

addURL

public void addURL(URL url)
This exposes a protected method that adds a URL to the classpath used for loading, defining and reconciling classes.

Overrides:
addURL in class URLClassLoader
Parameters:
url - The URL to be added to the classpath.

getName

public String getName()

setName

public void setName(String name)


Copyright © 2010. All Rights Reserved.