com.myarch.reloader
Class Reloader

java.lang.Object
  |
  +--com.myarch.reloader.Reloader

public class Reloader
extends java.lang.Object

Performs dynamic class loading (reloading).

Uses controller object to figure out what classes to load.
When reload() is called, loads the class passed as the parameter and, depending on the controller, called classes.

For every reload() call creates the new class loader to clear the cache of loaded classes.

Author:
Alexander Ananiev

Field Summary
static java.lang.String RELOAD_ENABLE_PROPERTY
          System property to turn reloading on/off.
 
Constructor Summary
Reloader()
          Constructs a new Reloader with default settings.
Reloader(java.lang.ClassLoader parent)
          Constructs a new Reloader and sets parent class loader for URLClassLoader that will be used for reload() call.
Reloader(LoadController ctl)
          Constructs a new Reloader for the specified controller.
Reloader(java.net.URL[] urls)
          Constructs a new Reloader for the specified URLs and default controller.
Reloader(java.net.URL[] urls, java.lang.ClassLoader parent)
          Constructs a new Reloader for the specified URLs, parent class loader and default controller.
Reloader(java.net.URL[] urls, LoadController ctl)
          Constructs a new Reloader for the specified URLs and controller.
 
Method Summary
 java.lang.ClassLoader getParent()
          Returns currently set parent class loader.
 java.net.URL[] getURLs()
          Returns the current array of URLs for class loader.
 java.lang.Class reload(java.lang.String name)
          Loads the class and classes used by this class depending on the controller.
static boolean reloadEnabled()
           
 void setLoadController(LoadController ctl)
          Sets the controller object for Reloader.
 void setLogger(LoadActionListener logger)
          Sets custom logger.
 void setParent(java.lang.ClassLoader parent)
          Sets parent class loader for URLClassLoader that will be used for reload() call.
static void setReloadOff()
          Turns reloading off for all instances of Reloader.
static void setReloadOn()
          Turns reloading on for all instances of Reloader.
 void setURLs(java.net.URL[] urls)
          Sets URLs for Reloader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RELOAD_ENABLE_PROPERTY

public static final java.lang.String RELOAD_ENABLE_PROPERTY
System property to turn reloading on/off.
On: -Dreloader.reload=yes (or any value other than "no")
Off: -Dreloader.reload=no
On is the default.
Constructor Detail

Reloader

public Reloader()
Constructs a new Reloader with default settings. ReloadMainController is the default, so only main class (the class passed to reload() as a parameter) will be reloaded.

Reloader

public Reloader(java.net.URL[] urls)
Constructs a new Reloader for the specified URLs and default controller.
Reloader uses class loader inherited from URLClassLoader, so URLs are passed to the URLClassLoader during reload() call.
Set URLs only if classes should be loaded from the custom class path outside of JVM class path.
Parameters:
urls - the URLs from which to load classes

Reloader

public Reloader(java.net.URL[] urls,
                java.lang.ClassLoader parent)
Constructs a new Reloader for the specified URLs, parent class loader and default controller.
Set URLs only if classes should be loaded from the custom class path outside of JVM class path.
System class loader (ClassLoader.getSystemClassLoader()) is used as the default parent.
Parameters:
urls - the URLs from which to load classes
parent - the parent class loader for delegation

Reloader

public Reloader(LoadController ctl)
Constructs a new Reloader for the specified controller.
Parameters:
ctl - controller object. Controller provides Reloader with the list of classes for loading.

Reloader

public Reloader(java.net.URL[] urls,
                LoadController ctl)
Constructs a new Reloader for the specified URLs and controller.
Set URLs only if classes should be loaded from the custom class path outside of JVM class path.
Parameters:
urls - the URLs from which to load classes
ctl - controller object. Controller provides Reloader with the list of classes for loading.

Reloader

public Reloader(java.lang.ClassLoader parent)
Constructs a new Reloader and sets parent class loader for URLClassLoader that will be used for reload() call. Normally, it should be the loader of the calling class, i.e.
reloader = new Reloader( this.getClass().getClassLoader() );
URLs (loading classpath) will be taken from this parent loader if they are not specified using setURLs().
Parameters:
parent - the parent class loader for delegation
Method Detail

setReloadOn

public static void setReloadOn()
Turns reloading on for all instances of Reloader.
On is the default, so use it after setReloadOff().

setReloadOff

public static void setReloadOff()
Turns reloading off for all instances of Reloader.
Same with setting -Dreloader.reload=no.
Do it for production system, when there is no need to frequently update the code.

reloadEnabled

public static boolean reloadEnabled()
Returns:
true if reloading is on

setLoadController

public void setLoadController(LoadController ctl)
Sets the controller object for Reloader. Controller provides Reloader with the list of classes for loading.
Parameters:
ctl - controller object.

setURLs

public void setURLs(java.net.URL[] urls)
Sets URLs for Reloader.
Reloader uses class loader inherited from URLClassLoader, so URLs are passed to the URLClassLoader during reload() call.
Set URLs only if classes should be loaded from the custom class path outside of JVM class path.
Parameters:
urls - the URLs from which to load classes

getURLs

public java.net.URL[] getURLs()
Returns the current array of URLs for class loader.
Returns:
urls the URLs from which to load classes

setParent

public void setParent(java.lang.ClassLoader parent)
Sets parent class loader for URLClassLoader that will be used for reload() call. Normally, it should be the loader of the calling class, i.e.
reloader = new Reloader( this.getClass().getClassLoader() );
Parameters:
parent - the parent class loader for delegation

getParent

public java.lang.ClassLoader getParent()
Returns currently set parent class loader.
Parameters:
parent - the parent class loader for delegation

setLogger

public void setLogger(LoadActionListener logger)
Sets custom logger. The default logger is ReloadLogger. To create custom logger, implement LoadActionListener interface.
Parameters:
logger - custom logger

reload

public java.lang.Class reload(java.lang.String name)
                       throws java.lang.ClassNotFoundException
Loads the class and classes used by this class depending on the controller.
Delegates loading to the new class loader (subclass of URLClassLoader). For each class that should be loaded, the class loader calls the controller. If the controller permits to load the class, the class loader tries to load it. If it can not load the class, it calls its parent class loader.
Parameters:
name - class name to load
Throws:
java.lang.ClassNotFoundException - if class loader or its parent did not find the class


Copyright © 2001 Alexander Ananiev & MyArch.com. All Rights Reserved.