[MOBY-l] Re: a question

Paul Gordon gordonp at ucalgary.ca
Thu Feb 3 22:47:43 UTC 2005


Benjamin Good wrote:

> Hi Paul,
>
> I'm just backing up a step for the moment.  In order to access the dom 
> element returned from service invocation I had to alter your class (to 
> unprotect it).  I'm working with your unaltered class now and am 
> running into a different problem - perhaps easier for you to help solve.
>
> Basically, how can I get the MobyDataSimpleInstance or 
> MobyDataSetInstance out of a response?  It seems that I am supposed to 
> use the Vector returned by invokeService() but I can't seem to get the 
> necessary casts to work.  Perhaps we could have it return 
> MobyDataSimpleInstances directly?
>
[I'm cc'ing the discussion list because the example code may be of 
interest to people who are looking to write Java clients]

Right, this is something I've just document a bit better in 
MobyRequest.  The Vector invokeServices() returns a list of 
MobyDataInstance[].  This is because a invokeService() may in fact be 
submitting more than one request at a time.  Each input request gets a 
corresponding response slot in the returned Vector.  For each response, 
there is an array (MobyDataInstance[]) respresenting the data returned 
(which can be any number of MobyDataSimple and MobyDataSet instances). 

I scratched my head for a while over why a cast to even the appropriate 
MobyDataInstance[] type caused a runtime cast exception.  It has to do 
with a fundamental type-safety behaviour of Java arrays, so I used an 
alternative Vector.toArray method in MobyRequest and now it works (this 
was committed to the repository a few minutes ago).

I've adjusted your client code below too, it now works:

-------------

import org.biomoby.client.CentralImpl;
import org.biomoby.client.MobyRequest;
import org.biomoby.shared.*;
import java.util.Iterator;
import java.util.Vector;

/**
 * @author benjamgo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TestRequest {

    public static void main(String[] args) {
        try{
      
            Central worker = new CentralImpl ();
            worker.setDebug(true);
//make template for finding services
            MobyService templateService = new MobyService("dummy");
            MobyDataType type = new MobyDataType("Object");
      
            MobyDataSimpleInstance input = new MobyDataSimpleInstance ("");
            input.setDataType (type);
            input.addNamespace (new MobyNamespace ("NCBI_gi"));
            input.setId("111076");  
      
            input.setXmlMode(MobyDataSimpleInstance.CENTRAL_XML_MODE);
            templateService.addInput(input);
            templateService.setName("MOBYSHoundGetGenBankff");
//        Find services that match this template service                   
          
            System.out.println("finding services matching template: 
"+templateService);
            MobyService[] validServices = 
worker.findService(templateService);
          
//        Make sure we have a service to run for this input
                if(validServices == null || validServices.length == 0){
                    System.err.println("Could not find any valid services");
                }
                else{
                    System.out.println("found some services");
                }
                input.setXmlMode(MobyDataSimpleInstance.SERVICE_XML_MODE);
                MobyDataSimpleInstance[] moby_in = new 
MobyDataSimpleInstance[1];
                moby_in[0] = input;
                MobyRequest mr = new MobyRequest(worker);

                mr.setDebugMode(true);
                mr.setService(validServices[0]);
                mr.setInput(moby_in);
                java.util.Vector output = mr.invokeService();
                String xresponse = mr.getResponseXML();
          
                String c = "";
                Iterator responses = output.iterator();
                // The service invocation may have had many requests, 
there is a response
                // object for each request
                while(responses.hasNext()){

                    Vector response = (Vector) responses.next();
                    Iterator resultObjects = response.iterator();

                    // The response for a request may have many objects
                    while(resultObjects.hasNext()){

                        MobyDataInstance resultObject = 
(MobyDataInstance) resultObjects.next();

                        // The response objects may either be simple 
data, or data sets
                        if(resultObject instanceof MobyDataSimpleInstance){
                            System.out.println("Plain simple instance is 
"+resultObject.toXML());
                        }

                        else if(resultObject instanceof 
MobyDataSetInstance){
                            System.out.println("Found a collection:");
                            MobyPrimaryDataSimple[] simples = 
((MobyDataSetInstance) resultObject).getElements();
                            for(int j = 0; j < simples.length; j++){
                                System.out.println("  Member instance is 
\n"+simples[j].toXML());
                            }
                        }
                    }
                }
            }catch(Exception e){
                System.out.println("Problem "+e);
            }
    }
}




More information about the moby-l mailing list