001 package toolbus.adapter.java; 002 003 import java.net.InetAddress; 004 005 import toolbus.ToolBus; 006 import toolbus.adapter.AbstractTool; 007 008 /** 009 * This class facilitates the functions a tool needs to be able to functions. 010 * 011 * @author Arnold Lankamp 012 */ 013 public abstract class AbstractJavaTool extends AbstractTool{ 014 015 /** 016 * Default constructor. 017 */ 018 public AbstractJavaTool(){ 019 super(); 020 } 021 022 /** 023 * Connects to the ToolBus. 024 * NOTE: For thread-safety reasons this method must NOT be called before the constructor of the 025 * AbstractJavaTool completes. This means that implementing classes should not put the connect 026 * call in their constructor. 027 * 028 * @param args 029 * The arguments that contain the required information for running a tool (name + id 030 * and additionally the host + port of the ToolBus, depending on how this tool is 031 * connected to the ToolBus). 032 * @throws Exception 033 * Thrown when something goes wrong during the parsing of the arguments or the 034 * establishing of the connection. 035 */ 036 public void connect(String[] args) throws Exception{ 037 if(toolBridge != null) throw new RuntimeException("ToolBridge has already been connected."); 038 039 String toolName = null; 040 int toolID = -1; 041 042 InetAddress host = null; 043 int port = -1; 044 045 for(int i = 0; i < args.length; i++){ 046 String arg = args[i]; 047 if(arg.equals("-TB_TOOL_NAME")){ 048 toolName = args[++i]; 049 }else if(arg.equals("-TB_TOOL_ID")){ 050 toolID = Integer.parseInt(args[++i]); 051 }else if(arg.equals("-TB_HOST")){ 052 host = InetAddress.getByName(args[++i]); 053 }else if(arg.equals("-TB_PORT")){ 054 port = Integer.parseInt(args[++i]); 055 } 056 } 057 058 if(toolName == null) throw new RuntimeException("Missing tool identification."); 059 060 toolBridge = new JavaToolBridge(termFactory, this, toolName, toolID, host, port); 061 toolBridge.run(); 062 } 063 064 /** 065 * Connects to the ToolBus directly (instead of through TCP/IP). 066 * 067 * @param toolbus 068 * The toolbus to connect to. 069 * @param toolName 070 * The name of the tool. 071 * @param toolID 072 * The ID of the tool. 073 * @throws Exception 074 * When connecting to the toolbus directly failed. 075 */ 076 public void connectDirectly(ToolBus toolbus, String toolName, int toolID) throws Exception{ 077 if(toolName == null) throw new RuntimeException("Missing tool identification."); 078 079 if(toolBridge != null) throw new RuntimeException("ToolBridge has already been connected."); 080 081 toolBridge = new JavaToolBridge(termFactory, this, toolName, toolID, toolbus); 082 toolBridge.run(); 083 } 084 }