irssibot.core
Class ModuleHandler

java.lang.Object
  |
  +--irssibot.core.ModuleHandler

public class ModuleHandler
extends java.lang.Object

Manages modules for IrssiBot. the modules can be loaded and added to handler at startup (from config file) or dynamically from IRC using core->loadModule().

All calls to modules are wrapped inside a tight try - catch in order to detect exceptions in the module and remove it in that case.

Version:
$Name: $ $Revision: 1.8 $
Author:
Matti Dahlbom

Field Summary
private  Core core
           
private static java.lang.String info
          Component info string to be returned by toString()
private  java.util.Hashtable loadedModules
           
private  AbstractModule[] moduleTable
           
private  int numModules
           
 
Constructor Summary
ModuleHandler(Core core)
          Constructs.
 
Method Summary
 boolean addModule(java.lang.String moduleClassName, AbstractModule module)
          Adds a module.
 boolean forwardMessage(IrcMessage message, ServerConnection caller)
          Forwards an IrcMessage to all registered modules.
 AbstractModule[] getModuleTable()
          Returns the module table.
private  void handleModuleCrash(java.lang.Throwable t, AbstractModule module)
          Removes faulted module cleanly.
private  java.util.Properties loadModuleState(java.lang.String moduleClassName)
          Loads state of a module from disk as a Properties object.
private  void reconstructTable()
          for optimal access to modules, construct a table representation of the contents of the hash table and maintain the number of loaded modules in numModules
 boolean removeModule(java.lang.String moduleClassName)
          Removes a module.
 void saveModuleState(AbstractModule module)
          Saves module state to disk as a Properties object.
 java.lang.String toString()
          Returns component info.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

info

private static java.lang.String info
Component info string to be returned by toString()


loadedModules

private java.util.Hashtable loadedModules

moduleTable

private AbstractModule[] moduleTable

numModules

private int numModules

core

private Core core
Constructor Detail

ModuleHandler

public ModuleHandler(Core core)
Constructs.

Parameters:
core - reference to bot core
Method Detail

toString

public java.lang.String toString()
Returns component info.

Overrides:
toString in class java.lang.Object

getModuleTable

public AbstractModule[] getModuleTable()
Returns the module table.


handleModuleCrash

private void handleModuleCrash(java.lang.Throwable t,
                               AbstractModule module)
Removes faulted module cleanly.

Parameters:
t - cause to module fault
module - the module error occurred in

forwardMessage

public boolean forwardMessage(IrcMessage message,
                              ServerConnection caller)
Forwards an IrcMessage to all registered modules. All exceptions in the the module are caught and handled.

Parameters:
message - the IrcMessage to forward
caller - the ServerConnection the message came from
Returns:
true is successful. false indicates an error occurred in a module and it was removed from module table.

reconstructTable

private void reconstructTable()
for optimal access to modules, construct a table representation of the contents of the hash table and maintain the number of loaded modules in numModules


addModule

public boolean addModule(java.lang.String moduleClassName,
                         AbstractModule module)
                  throws java.lang.IllegalStateException
Adds a module. The module's onLoad() is called after addition, and it starts receiving message events immediately.

Parameters:
moduleClassName - Java class name of module
module - AbstractModule to add
Returns:
true if successful, or false if failed.
Throws:
java.lang.IllegalStateException - if module already loaded

removeModule

public boolean removeModule(java.lang.String moduleClassName)
Removes a module. The module's onUnload() is called on removal, and it stops receiving message events.

Parameters:
moduleClassName - name o module to remove
Returns:
true if successfully removed. false if could not remove

loadModuleState

private java.util.Properties loadModuleState(java.lang.String moduleClassName)
Loads state of a module from disk as a Properties object.

Parameters:
moduleClassName - the name of module class
Returns:
Module's state as a Properties object or a null if state file not found.

saveModuleState

public void saveModuleState(AbstractModule module)
Saves module state to disk as a Properties object.

Parameters:
module - module whose state to save


IrssiBot (c) 2000-2002 Matti Dahlbom