[MOBY-guts] biomoby commit

Paul Gordon gordonp at pub.open-bio.org
Sun Aug 7 23:46:12 UTC 2005


gordonp
Sun Aug  7 19:46:12 EDT 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory pub.open-bio.org:/tmp/cvs-serv28953/src/main/org/biomoby/client

Modified Files:
	MobyRequest.java 
Added Files:
	MobyRequestEvent.java MobyRequestEventHandler.java 
Log Message:
Update of code and documentation for asynchronous calls to MobyRequest

moby-live/Java/src/main/org/biomoby/client MobyRequestEvent.java,NONE,1.1 MobyRequestEventHandler.java,NONE,1.1 MobyRequest.java,1.14,1.15
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java	2005/07/22 05:23:00	1.14
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/MobyRequest.java	2005/08/07 23:46:12	1.15
@@ -21,20 +21,9 @@
 import org.apache.xpath.XPathContext;
 import org.apache.xpath.objects.XNodeSet;
 import org.apache.xpath.objects.XObject;
-import org.biomoby.shared.Central;
-import org.biomoby.shared.MobyData;
+import org.biomoby.shared.*;
 import org.biomoby.shared.data.*;
-import org.biomoby.shared.MobyException;
-import org.biomoby.shared.MobyPrefixResolver;
-import org.biomoby.shared.MobyService;
-import org.biomoby.shared.NoSuccessException;
-import org.biomoby.shared.SOAPException;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
+import org.w3c.dom.*;
 
 /**
  * This class handles the WSDL transaction to request a response
@@ -76,6 +65,12 @@
     private XPath simpleChildXPath;
     private XPath collectionChildXPath;
 
+    private int autoID = 0;
+
+    // Used as invocation callback if MobyRequest is acting as a server,
+    // or is executing services as a client asynchronously
+    private Vector eventHandlers; 
+
     /**
      * Default constructor.  You should have a Central instance around since you're going to 
      * be retrieving MobyServices to pass into here. Lets reuse it.
@@ -87,6 +82,8 @@
 	mobyCentral = central;
 	wsdlCache = new Hashtable();
 
+	eventHandlers = new Vector();
+
 	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 	dbf.setNamespaceAware(true);	
 	docBuilder = dbf.newDocumentBuilder();
@@ -252,7 +249,7 @@
      */
     public MobyContentInstance invokeService() throws Exception, MobyException, SOAPException, NoSuccessException{
 	if(mobyService == null){
-	    throw new MobyException("Tries to invoke null service from MobyRequest (call setService first)");
+	    throw new MobyException("Tried to invoke null service from MobyRequest (call setService first)");
 	}
 
         getServiceFromWSDL();
@@ -265,6 +262,61 @@
     }
 
     /**
+     * Asynchronous call to invokeService.  A callback to the passed-in handler will be made when 
+     * the response is ready, or there is an exception.
+     *
+     * @return the id that the callback event will return from getID(), allowing a client to distinguish between multiple concurrent invocation callbacks
+     */
+    public synchronized int invokeService(MobyRequestEventHandler handler){
+	int id = autoID++;
+
+	Thread t = new InvocationThread(this, handler, id);  // see internal class definition below
+	t.start();
+
+	return id;
+    }
+
+    // This is the class that asynchronously calls the service and does a callback to 
+    // the handler specified in the invocation.
+    class InvocationThread extends Thread {
+	MobyRequest mobyRequest; 
+	MobyRequestEventHandler handler;
+	int requestId;
+
+	InvocationThread(MobyRequest mr, MobyRequestEventHandler h, int id){
+	    mobyRequest = mr;
+	    handler = h;
+	    requestId = id;
+	}
+
+	public void run() {
+	    MobyContentInstance content = null;
+	    MobyRequestEvent responseEvent = null;
+	    try{
+		content = mobyRequest.invokeService();
+	    }catch(Exception e){
+		responseEvent = new MobyRequestEvent(content, mobyRequest, e, requestId);
+	    }
+	    if(responseEvent == null){
+		responseEvent = new MobyRequestEvent(content, mobyRequest, null, requestId);
+	    }
+	    handler.processEvent(responseEvent);
+	}
+    }
+
+    public void addEventHandler(MobyRequestEventHandler h){
+	eventHandlers.add(h);
+    }
+
+    public void removeEventHandler(MobyRequestEventHandler h){
+	eventHandlers.remove(h);
+    }
+
+    public void sendResponse(MobyRequestEvent mre){
+	// Not yet implemented, need to conform to some web.xml specification here...
+    }
+
+    /**
      * @throws MobyException if the number of input parameters was wrong, or the paramters were of the wrong type.
      */
     protected void verifyInput() throws MobyException{
@@ -644,7 +696,7 @@
     }
 
     /**
-     * A method that sets up the execution environm,ent for and runs a compiled XPath statement against a DOM node
+     * A method that sets up the execution environment for and runs a compiled XPath statement against a DOM node
      * You should call releaseXPath when you're done with the results
      * @return the list of Nodes that satisfy the XPath  in this Node's context
      */




More information about the MOBY-guts mailing list