pnuts.ext
Class ModuleBase

java.lang.Object
  extended by pnuts.ext.ModuleBase
All Implemented Interfaces:
java.io.Serializable, Executable
Direct Known Subclasses:
init

public abstract class ModuleBase
extends java.lang.Object
implements Executable, java.io.Serializable

Base class of modules. This class provides convenient autloading functions and an error reporting function. Modules may (or may not) subclass this class.

See Also:
Serialized Form

Constructor Summary
ModuleBase()
           
 
Method Summary
protected  void autoload(java.lang.String[] functionNames, java.lang.String file, Context context)
          Registers an autoloaded script for functionNames.
protected  void autoloadClass(java.lang.String javaPackage, java.lang.String name, Context context)
          Registers an autoloaded Class object.
protected  void autoloadFunction(java.lang.String functionName, Context context)
          Registers an autoloaded class for functionName.
protected  java.lang.Object execute(Context context)
          Subclasses should override this method, instead of run(Context), to define the initialization process.
protected  java.lang.String getClassName(Package pkg, java.lang.String name)
          Makes a class name for the specified package and the symbol's name.
protected  Package getPackage(Context context)
           
protected  java.lang.String getPrefix()
          Defines the prefix of script class (resource) name.
protected  java.lang.String[] getRequiredModules()
          This method is supposed to be redefined in a subclass to define a set of modules that are required to implement this module.
protected  java.lang.String[] getSubModules()
          This method is supposed to be redefined in a subclass to define a set of modules that this module provides in the caller's context.
protected  java.lang.Object newInstance(java.lang.Class cls)
          This method is redefined in subclasses so that package private classes can be used.
 java.lang.Object run(Context context)
          Defines ERROR and EXPORTS, and then call execute(Context).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModuleBase

public ModuleBase()
Method Detail

autoload

protected void autoload(java.lang.String[] functionNames,
                        java.lang.String file,
                        Context context)
Registers an autoloaded script for functionNames. Also, functionNames are automatically exported.

Parameters:
functionNames - the function names
file - a script file to be loaded when one of the functionNames is first resolved.
context - the context

autoloadFunction

protected void autoloadFunction(java.lang.String functionName,
                                Context context)
Registers an autoloaded class for functionName. Also, the functionName is automatically exported.

Parameters:
functionName - the function name
context - the context

autoloadClass

protected void autoloadClass(java.lang.String javaPackage,
                             java.lang.String name,
                             Context context)
Registers an autoloaded Class object.

Parameters:
javaPackage - Java package name, e.g. "java.util"
name - short Class name, e.g. "HashMap"
context - the context

getPrefix

protected java.lang.String getPrefix()
Defines the prefix of script class (resource) name. This method is overriden by subclasses.


getPackage

protected Package getPackage(Context context)

newInstance

protected java.lang.Object newInstance(java.lang.Class cls)
                                throws java.lang.IllegalAccessException,
                                       java.lang.InstantiationException
This method is redefined in subclasses so that package private classes can be used.

Parameters:
cls - the class to be instantiated
Returns:
an instance of the class
Throws:
java.lang.IllegalAccessException
java.lang.InstantiationException

getClassName

protected java.lang.String getClassName(Package pkg,
                                        java.lang.String name)
Makes a class name for the specified package and the symbol's name.

Parameters:
pkg - the package
name - the symbol
Returns:
the name of the class

run

public java.lang.Object run(Context context)
Defines ERROR and EXPORTS, and then call execute(Context).

Specified by:
run in interface Executable
Parameters:
context - the context
Returns:
the result of the execution

execute

protected java.lang.Object execute(Context context)
Subclasses should override this method, instead of run(Context), to define the initialization process. If neither getSubModules() nor getRequiredModules() are redefined to return non-null value, execute() method should be implemented as the following steps. 1. Call context.usePackage() to use the modules that this module provides 2. Call context.clearPackages() 3. Call context.usePackage() to use the module that this module requires 4. Define symbols (functions)

Parameters:
context - the context

getRequiredModules

protected java.lang.String[] getRequiredModules()
This method is supposed to be redefined in a subclass to define a set of modules that are required to implement this module. If this method returns an array of module names (non-null), Context.clearPackages() is called, then the modules are used()'d, before calling execute() method.


getSubModules

protected java.lang.String[] getSubModules()
This method is supposed to be redefined in a subclass to define a set of modules that this module provides in the caller's context. If this method returns an array of module names (non-null), they are use()'d, then Context.clearPackages() is called, before calling execute() method.