org/objectweb/proactive/ext/webservices/soap/ProActiveProvider.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.webservices.soap;
00032 
00033 import java.io.StringWriter;
00034 import java.util.Hashtable;
00035 
00036 import javax.servlet.http.HttpServlet;
00037 import javax.servlet.http.HttpSession;
00038 
00039 import org.apache.soap.Constants;
00040 import org.apache.soap.Envelope;
00041 import org.apache.soap.SOAPException;
00042 import org.apache.soap.rpc.Call;
00043 import org.apache.soap.rpc.Response;
00044 import org.apache.soap.rpc.SOAPContext;
00045 import org.apache.soap.server.DeploymentDescriptor;
00046 import org.apache.soap.server.RPCRouter;
00047 import org.apache.soap.util.Provider;
00048 import org.objectweb.fractal.api.Component;
00049 import org.objectweb.proactive.core.ProActiveException;
00050 import org.objectweb.proactive.core.body.http.util.HttpMarshaller;
00051 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00052 import org.objectweb.proactive.ext.webservices.WSConstants;
00053 
00054 
00059 public class ProActiveProvider extends WSConstants implements Provider {
00060 
00061     static {
00062         System.setSecurityManager(new java.rmi.RMISecurityManager());
00063         try {
00064             RuntimeFactory.getDefaultRuntime();
00065         } catch (ProActiveException e) {
00066             e.printStackTrace();
00067         }
00068     }
00069 
00070     private DeploymentDescriptor dd;
00071     private Envelope envelope;
00072     private Call call;
00073     private String methodName;
00074     private String targetObjectURI;
00075     private HttpServlet servlet;
00076     private HttpSession session;
00077     private Object targetObject;
00078 
00084     public void locate(DeploymentDescriptor dd, Envelope env, Call call,
00085         String methodName, String targetObjectURI, SOAPContext reqContext)
00086         throws SOAPException {
00087         //Set some properties to the context    
00088         HttpServlet servlet = (HttpServlet) reqContext.getProperty(Constants.BAG_HTTPSERVLET);
00089         HttpSession session = (HttpSession) reqContext.getProperty(Constants.BAG_HTTPSESSION);
00090 
00091         System.out.println("================================================");
00092         System.out.println("In ProActiveProvider.locate()");
00093         System.out.println("Method: " + methodName);
00094         System.out.println("URI: " + targetObjectURI);
00095         System.out.println("DD.ServiceClass: " + dd.getServiceClass());
00096         System.out.println("DD.ProviderClass: " + dd.getProviderClass());
00097         System.out.println("Call.MethodName: " + call.getMethodName());
00098 
00099         Hashtable props = dd.getProps();
00100         String className = dd.getProviderClass();
00101 
00102         this.dd = dd;
00103         this.envelope = env;
00104         this.call = call;
00105         this.methodName = methodName;
00106         this.targetObjectURI = targetObjectURI;
00107         this.servlet = servlet;
00108         this.session = session;
00109 
00110         if (!RPCRouter.validCall(dd, call)) {
00111             System.err.println("It's not a valid call");
00112 
00113             SOAPException e = new SOAPException(Constants.FAULT_CODE_CLIENT,
00114                     "It's not a  valid call");
00115             throw e;
00116         }
00117 
00118         byte[] serObj = (byte[]) props.get("Stub");
00119 
00120         boolean isInterfaceComponent = ((String) props.get(ProActiveDeployer.COMPONENT_INTERFACE)).equals(
00121                 "true");
00122 
00123         try {
00124             if (!isInterfaceComponent) {
00125                 targetObject = HttpMarshaller.unmarshallObject(serObj);
00126             } else {
00127                 Object o = HttpMarshaller.unmarshallObject(serObj);
00128                 String actualName = targetObjectURI.substring(targetObjectURI.lastIndexOf(
00129                             '_') + 1);
00130                 Component c = (Component) o;
00131                 targetObject = c.getFcInterface(actualName);
00132             }
00133         } catch (Exception e) {
00134             System.out.println("Exception : " + e.getMessage());
00135             e.printStackTrace(System.out);
00136         }
00137     }
00138 
00144     public void invoke(SOAPContext reqContext, SOAPContext resContext)
00145         throws SOAPException {
00146         System.out.println("=============================================");
00147         System.out.println("In ProActiveProvider.invoke()");
00148 
00149         //  System.out.println("targetObject = " + targetObject);
00150         String reponse = null;
00151 
00152         //dd.setProviderClass(targetObject.getClass().getName());
00153         // Add logic to invoke the service and get back the result here
00154         try {
00155             Response resp = RPCRouter.invoke(dd, call, targetObject,
00156                     reqContext, resContext);
00157 
00158             //build the enveloppe that contains the response    
00159             Envelope env = resp.buildEnvelope();
00160             System.out.println(env);
00161             StringWriter sw = new StringWriter();
00162             env.marshall(sw, call.getSOAPMappingRegistry(), resContext);
00163             resContext.setRootPart(sw.toString(),
00164                 Constants.HEADERVAL_CONTENT_TYPE_UTF8);
00165         } catch (Exception e) {
00166             System.out.println("--- >exception ! " + e.getMessage() + " -- " +
00167                 e.getClass().getName());
00168             e.printStackTrace(System.out);
00169 
00170             SOAPException ex = new SOAPException(Constants.FAULT_CODE_SERVER,
00171                     e.getMessage());
00172             System.out.println(
00173                 "An error has occured when trying to invoke the method on the object");
00174             throw ex;
00175         }
00176     }
00177 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1