The Meta-Environment API

toolbus.adapter.java
Class JavaToolBridge

java.lang.Object
  extended by toolbus.adapter.ToolBridge
      extended by toolbus.adapter.java.JavaToolBridge
All Implemented Interfaces:
java.lang.Runnable, IDataHandler, IOperations

public class JavaToolBridge
extends ToolBridge

This class is a specialized ToolBridge version for Java tools. It supplies an interface for the tool to enable it to communicate with the ToolBus and it handles the invokation of methods on the tool.

Author:
Arnold Lankamp

Nested Class Summary
protected static class JavaToolBridge.CallableMethodSignature
          This class represents a signature of a method (Name + return type + params).
private static class JavaToolBridge.MethodInvokationException
          Custom exception.
 
Field Summary
private  java.util.Map<JavaToolBridge.CallableMethodSignature,java.lang.reflect.Method> callableFunctions
           
private  aterm.pure.PureFactory termFactory
           
private  AbstractTool tool
           
 
Fields inherited from interface toolbus.IOperations
ACKDO, ACKEVENT, CONNECT, DEBUGPERFORMANCESTATS, DISCONNECT, DO, END, EVAL, EVENT, PERFORMANCESTATS, REQUEST, RESPONSE, TERMINATE, UNDEFINED, VALUE
 
Constructor Summary
JavaToolBridge(aterm.pure.PureFactory termFactory, AbstractTool tool, java.lang.String toolName, int toolID, java.net.InetAddress host, int port)
          Constructor.
JavaToolBridge(aterm.pure.PureFactory termFactory, AbstractTool tool, java.lang.String toolName, int toolID, ToolBus toolbus)
          Constructor.
 
Method Summary
 boolean checkSignature(aterm.ATerm signatures)
          Checks if the tool associated with this tool bridge supplies the given signature.
private  java.lang.Object convertArgument(aterm.ATerm aTerm)
          Converts the given aterm to its corresponding Java type object (if needed).
private  java.lang.Class<?> determainTermType(aterm.ATerm aTerm)
          Determain the class type of the given aterm (depending on its signature).
 void doDo(aterm.ATerm aTerm)
          Executes a DO operation with the given ATerm.
 aterm.ATerm doEval(aterm.ATerm aTerm)
          Executes an EVAL operation with the given ATerm.
 aterm.ATerm doGetPerformanceStats()
          Retrieves performance statistic information from the tool (if possible).
 void doReceiveAckEvent(aterm.ATerm aTerm)
          Executes a RECACKEVENT operation with the given ATerm.
 void doTerminate(aterm.ATerm aTerm)
          Executes a TERMINATE operation with the given ATerm.
private  java.lang.reflect.Method findMethod(byte operation, java.lang.String methodName, java.lang.Class<?>[] parameters)
          Returns the tool method that matches the given name, arguments and operation.
private  aterm.ATerm getPerformanceStats()
          Gathers performance statistics about this tool, like memory usage and the user-/system-time spend per thread.
private  void initCallableMethods()
          Reads all the callable methods of the tool associated with this tool bridge.
private  aterm.ATerm invokeMethod(byte operation, aterm.ATerm aTerm)
          Invokes the method associated with the given term and operation.
protected static boolean isImplementationOf(java.lang.Class<?> clazz, java.lang.Class<?> superInterface)
          Checks the the given class satisfies the given interface.
private static java.lang.String toMethodName(java.lang.String str)
           
 
Methods inherited from class toolbus.adapter.ToolBridge
exceptionOccured, getFactory, getHost, getPort, getToolID, getToolName, getType, postEvent, postRequest, receive, run, send, setIOHandler, setToolID, shutDown, terminate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

termFactory

private final aterm.pure.PureFactory termFactory

tool

private final AbstractTool tool

callableFunctions

private final java.util.Map<JavaToolBridge.CallableMethodSignature,java.lang.reflect.Method> callableFunctions
Constructor Detail

JavaToolBridge

public JavaToolBridge(aterm.pure.PureFactory termFactory,
                      AbstractTool tool,
                      java.lang.String toolName,
                      int toolID,
                      java.net.InetAddress host,
                      int port)
Constructor.

Parameters:
tool - The tool this bridge needs to be associated with.
toolName - The name of the with this bridge associated tool.
toolID - The id of the with this bridge associated tool.
host - The host on which the ToolBus is running.
port - The port on which the ToolBus is running.

JavaToolBridge

public JavaToolBridge(aterm.pure.PureFactory termFactory,
                      AbstractTool tool,
                      java.lang.String toolName,
                      int toolID,
                      ToolBus toolbus)
Constructor.

Parameters:
type - The type of the tool (Remote of direct).
tool - The tool this bridge needs to be associated with.
toolName - The name of the with this bridge associated tool.
toolID - The id of the with this bridge associated tool.
classLoader - The classLoader to use for loading classes.
toolbus - The toolbus to link this bridge to.
Method Detail

doDo

public void doDo(aterm.ATerm aTerm)
Description copied from class: ToolBridge
Executes a DO operation with the given ATerm.

Specified by:
doDo in class ToolBridge
Parameters:
aTerm - The ATerm that contains the necessary data to complete the DO request.
See Also:
ToolBridge.doDo(ATerm)

doEval

public aterm.ATerm doEval(aterm.ATerm aTerm)
Description copied from class: ToolBridge
Executes an EVAL operation with the given ATerm.

Specified by:
doEval in class ToolBridge
Parameters:
aTerm - The ATerm that contains the necessary data to complete the EVAL request.
Returns:
The result the EVAL request produced (may not be null).
See Also:
ToolBridge.doEval(ATerm)

doReceiveAckEvent

public void doReceiveAckEvent(aterm.ATerm aTerm)
Description copied from class: ToolBridge
Executes a RECACKEVENT operation with the given ATerm.

Specified by:
doReceiveAckEvent in class ToolBridge
Parameters:
aTerm - The ATerm that can potentially contain callback data.
See Also:
ToolBridge.doReceiveAckEvent(ATerm)

doTerminate

public void doTerminate(aterm.ATerm aTerm)
Description copied from class: ToolBridge
Executes a TERMINATE operation with the given ATerm.

Specified by:
doTerminate in class ToolBridge
Parameters:
aTerm - The ATerm that can potentially contain background informantion about the termination request.
See Also:
ToolBridge.doTerminate(ATerm)

doGetPerformanceStats

public aterm.ATerm doGetPerformanceStats()
Description copied from class: ToolBridge
Retrieves performance statistic information from the tool (if possible).

Specified by:
doGetPerformanceStats in class ToolBridge
Returns:
The gathered performance information (may not be null).
See Also:
ToolBridge.doGetPerformanceStats()

isImplementationOf

protected static boolean isImplementationOf(java.lang.Class<?> clazz,
                                            java.lang.Class<?> superInterface)
Checks the the given class satisfies the given interface.

Parameters:
clazz - The class from which we want to know if it satisfies the given interface.
superInterface - The interface to satisfy.
Returns:
True if clazz implements the specified interface; false otherwise.

initCallableMethods

private void initCallableMethods()
Reads all the callable methods of the tool associated with this tool bridge.


checkSignature

public boolean checkSignature(aterm.ATerm signatures)
Description copied from class: ToolBridge
Checks if the tool associated with this tool bridge supplies the given signature.

Specified by:
checkSignature in class ToolBridge
Returns:
True if the tool supplies the expected interface; false otherwise.
See Also:
ToolBridge.checkSignature(ATerm)

invokeMethod

private aterm.ATerm invokeMethod(byte operation,
                                 aterm.ATerm aTerm)
                          throws JavaToolBridge.MethodInvokationException
Invokes the method associated with the given term and operation.

Parameters:
operation - Indicates the return type of the method (DO = void, EVAL = ATerm).
aTerm - The ATerm that indicates with method should be invoked.
Returns:
The ATerm the invoked method returned; null in case it returned void.
Throws:
JavaToolBridge.MethodInvokationException - Thrown when something goes wrong.

toMethodName

private static java.lang.String toMethodName(java.lang.String str)

findMethod

private java.lang.reflect.Method findMethod(byte operation,
                                            java.lang.String methodName,
                                            java.lang.Class<?>[] parameters)
Returns the tool method that matches the given name, arguments and operation.

Parameters:
operation - The operation associated with the given term.
methodName - The name of the method we want to find.
parameters - The list of parameters that the method needs be able to accept.
Returns:
The method corresponding to the given term.

determainTermType

private java.lang.Class<?> determainTermType(aterm.ATerm aTerm)
Determain the class type of the given aterm (depending on its signature).

Parameters:
aTerm - The aterm of which we need to determain the type.
Returns:
The Java class type the aterm corresponds to.

convertArgument

private java.lang.Object convertArgument(aterm.ATerm aTerm)
Converts the given aterm to its corresponding Java type object (if needed).

Parameters:
aTerm - The aterm to convert.
Returns:
The resulting Java type object.

getPerformanceStats

private aterm.ATerm getPerformanceStats()
Gathers performance statistics about this tool, like memory usage and the user-/system-time spend per thread.

Returns:
Performance statictics.

The Meta-Environment API