HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Combining RMI with SOAP"
Vol. 7, Issue 6, p. 24

	


Listing 1

POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
       <m:GetLastTradePrice xmlns:m="Some-URI">
           <symbol>DIS</symbol>
       </m:GetLastTradePrice>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Listing 2

HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   <SOAP-ENV:Body>
       <m:GetLastTradePriceResponse xmlns:m="Some-URI">
           <Price>34.5</Price>
       </m:GetLastTradePriceResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Listing 3

//construct a Properties object and load with it the properties file specified by the URL
            Properties prop = new Properties();
            prop.load(url.openConnection().getInputStream());
            //construct a Document objet from the Call object entries
            doc = buildDocument();
            //obtain the name and address of the rmi server frm the properties file
            serverURI = prop.getProperty("serverLocation");
            serverName = prop.getProperty("serverName");
            System.out.println("The address of the rmi server:"+"
            rmi://"+serverURI+"/"+serverName);
            //get the rmi server URI
            System.out.println("Target obj:	"+this.getTargetObjectURI());
            //obtain the reference to the rmi server object by the target object URI
            SoapRMIInterface obj =(SoapRMIInterface)java.rmi.Naming.lookup
			("rmi://"+serverURI+"/urn:+serverName);


Listing 4

//create a RMICallObject
            RMICallObject call = new RMICallObject();
            //set the target remote service name
            call.setTargetObjectURI("urn:addressserver");
            //set the name of the remote method to be invoked
            call.setMethodName("getAddress");
            //set the encoding style to be used
            call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
            //creating the parameters to be passed to the remote method
            Vector params = new Vector();
            params.addElement(new Parameter("name1", String.class,"Samudra", null));
            params.addElement(new Parameter("name2", String.class,"Gupta", null));
            call.setParams(params);
            //obtaining the current working directory
            String workingDir = System.getProperty("user.dir");
            String url ="file://localhost/"+workingDir+"/conf.txt";
            System.out.println("Invoking..."+url);
            
            //invoking the remote service
            Response res = call.invoke(new URL(url), "");


Listing 5

public Address getAddress(String personName, String surName)
    {
        Address add = new Address();
        StringBuffer buffer = new StringBuffer(personName);
        buffer.append(" ");
        buffer.append(surName);
        add.setPerson(buffer.toString());
        add.setCity("London");
        add.setPost("SS1 2RQ");
        System.out.println("Returning address for:"+add.getPerson()+" "+surName);
        return add;
    }

	
	

Additional Source Code Listing 1: RMICallObject.java public class RMICallObject extends Call { /** Creates new RMICallObject */ public RMICallObject() { super(); } public Response invoke(URL url, String s) throws SOAPException { try { //construct a Properties object and load with it the properties file specified by the URL Properties prop = new Properties(); prop.load(url.openConnection().getInputStream()); System.out.println ("LOADED THE PROPERTIES AND THE SERVER NAME: "+prop.getProperty("serverName")); //construct a Document objet from the Call object entries doc = buildDocument(); //obtain the name and address of the rmi server frm the properties file serverURI = prop.getProperty("serverLocation"); serverName = prop.getProperty("serverName"); System.out.println ("The address of the rmi server: "+"rmi://"+serverURI+"/"+serverName); //get the rmi server URI System.out.println("Target obj: "+this.getTargetObjectURI()); //obtain the reference to the rmi server object by the target object URI SoapRMIInterface obj = (SoapRMIInterface)java.rmi.Naming.lookup ("rmi://"+serverURI+"/urn:"+serverName); //SoapRMIInterface obj = (SoapRMIInterface)java.rmi.Naming.lookup(serverName); //calling the invoke method responseDoc = obj.invokeMethod(doc); //construct the response object res = buildResponse(responseDoc); if(res.getFault()!=null) System.out.println ("The fault code: "+res.getFault().getFaultCode()); }catch(java.rmi.RemoteException e) { throw new SOAPException(Constants.FAULT_CODE_SERVER, "Could not invoke the server. Please try later."); }catch(Exception gene) { throw new SOAPException(Constants.FAULT_CODE_CLIENT, gene.toString()); } return res; } } //end of class Listing 2: SoapRMIServer.java /** * This is the class managing all the soap services deployed as RMI. This class reads all the deployed services from the deployment *descriptor and make the exposed methods available for any SOAP client to use them. This class is also responsible for parsing the SOAP *request and sending back a SOAP response back to the client along with any fault-code generated. * @author default * @version 1.0 */ public class SoapRMIServer extends UnicastRemoteObject implements SoapRMIInterface { /** This method receives the SOAP Document and parse it, invokes the requried * remote method, constructs a SOAP response Document and sends it back to * the caller * @param doc the SOAP request Document * @throws RemoteException the RemoteException * @throws RMISoapException The Custom RMISOAPException * @return the SOAP response Document */ public Document invokeMethod(Document doc) throws RemoteException, RMISoapException { try { soapDoc = doc; //finding the body element envElement = doc.getRootElement(); bodyElement = (Element)envElement.getChildren().get(0); System.out.println("Body element: "+bodyElement); //finding the method element and method name and service id out of the method child of the body element methodElement = (Element)(bodyElement.getChildren().get(0)); methodName = methodElement.getName(); serviceID = methodElement.getNamespaceURI(); System.out.println("The method name is: "+methodName); System.out.println("The service id..."+ serviceID); //obtaining the paramteres passed String[] params = new String[methodElement.getChildren().size()]; String[] paramValues = new String[methodElement.getChildren().size()]; int size = methodElement.getChildren().size(); System.out.println ("The number of paramteres to the method: "+methodElement.getChildren().size()); for(int i=0; i<size; i++) { Element el = (Element)(methodElement.getChildren().get(i)); params[i] = el.getName(); paramValues[i] = el.getText(); } //obtain the deployment descriptor for the service desc = ConfigDescriptor.getDescriptor(serviceID); //now call the specified method if(checkMethod(desc, methodName)) { Class c = Class.forName(desc.getProviderClass()); Method[] methods = c.getMethods(); for(int i=0; i<methods.length; i++) { if(methods[i].getName().equals(methodName)) { System.out.println ("Invoking method.."+methodName+" and the declaring class: "+c.getName()); System.out.println ("The parameters getting passed: "+paramValues.toString()+" and the no of params: "+paramValues.length); Object obj = methods[i].invoke(c.newInstance(), paramValues); //construct the response out of the return value response = constructResponse(doc, obj); } } }else { System.out.println("Throwing soap exception..."); //throw a RMISoapException //throw new RMISoapException (Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not invoke the specified method. Please check if it is deployed correctly."); //throw new RemoteException("Problem...", new RMISoapException("faultcode" , "faultstring")); response = constructFaultDocument (doc, Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not invoke the specified method"); } }catch(RemoteException re) { re.printStackTrace(); throw new RemoteException("Exception "); }catch(InvocationTargetException ite) { //RMISoapException se = new RMISoapException (Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not invoke the specified method. Please check if it is deployed correctly."); response = constructFaultDocument (doc, Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not invoke the specified method"); //throw se; }catch(IllegalAccessException iae) { }catch(ClassNotFoundException cnfe) { //RMISoapException se = new RMISoapException (Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not match the target object. Please check if it is deployed correctly."); response = constructFaultDocument (doc,Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, "Could not match the target object. Please ensure it is deployed correctly."); }catch(InstantiationException inste) { //RMISoapException se = new RMISoapException (Constants.FAULT_CODE_SERVER, "Could not instantiate the specified target object.."); response = constructFaultDocument (doc, Constants.FAULT_CODE_SERVER, "Could not instantiate the specified target object."); }catch(Exception e) { e.printStackTrace(); //RMISoapException se = new RMISoapException (Constants.FAULT_CODE_SERVER, "General problem encountered in the server. Please restart the server."); response = constructFaultDocument (doc, Constants.FAULT_CODE_SERVER, General problem in the server. Please restart the server."); } return response; } } Listing 3: SoapServerManager.java package rmi.server; /** This class is the manager for reading all the SOAP exposed RMI services from the * deploymnet descriptor file and binds all the available services to the rmiregistry * against the name specified in the deployment descriptor file. * * This class also facilitates starting and stopping of individual services or all the services. */ public class SoapServerManager extends Object { /** This is the main method which loads the services and bind them to the registry. * @param args the command line arguments */ public static void main (String args[]) { String serverName = null; String action = null; //create an instance of the manager SoapServerManager soapManager = new SoapServerManager(); System.out.println("Args: "+args.length); try { if(args.length==0) { //load all the available services soapManager.loadAllServices(); }else { System.out.println("Usage: java SoapServerManager"); } }catch(RemoteException re) { System.out.println("There is an exception: "+re.toString()); } } /** This method reads the deployment descriptor file and loads all the specified * services and bind them to the registry for the RMI use * @throws RemoteException RemoteException */ public void loadAllServices() throws RemoteException { ConfigDescriptor configManager = null; //the configmanager List ddList = null; //list of DeploymentDescriptors Iterator iterator = null; //iterator for the list DeploymentDescriptor descriptor = null; //the deployment descriptor String serviceName = null; //the name of the service String providerClass = null; //the provider class String configFileName = "c:\\dev\\src\\lib\\soap.xml"; try { //intialising the config manager configManager = ConfigDescriptor.getInstance(); //obtain all the descriptors ddList = configManager.getAllDeploymentDescriptor(); iterator = ddList.iterator(); while(iterator.hasNext()) { descriptor = (DeploymentDescriptor)iterator.next(); serviceName = descriptor.getID(); providerClass = descriptor.getProviderClass(); startServer(serviceName,providerClass); } }catch(Exception e) { e.printStackTrace(); } } /** This method binds the specified RMI service to the registry * @param serverName the name of the service as to be bound with the RMI reg. * @param providerClass the implmentation class * @throws RemoteException RemoteException */ public void startServer(String serverName, String providerClass) throws RemoteException { System.out.println("About to start the service with the name: "+serverName); try { //installing a new security manager System.setSecurityManager(new RMISecurityManager()); //creating an instance of the service specified Naming.rebind(serverName,(Remote)Class.forName(providerClass).newInstance()); }catch(Exception e) { e.printStackTrace(); throw new RemoteException("There is a problem in starting up the server"); } System.out.println("Successfully started the server : "+serverName); } }

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.