org/objectweb/proactive/ext/webservices/soap/ProActiveDeployer.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.lang.reflect.Method;
00034 import java.net.MalformedURLException;
00035 import java.net.URL;
00036 import java.util.Enumeration;
00037 import java.util.Hashtable;
00038 import java.util.StringTokenizer;
00039 import java.util.Vector;
00040 
00041 import org.apache.soap.SOAPException;
00042 import org.apache.soap.server.DeploymentDescriptor;
00043 import org.apache.soap.server.ServiceManagerClient;
00044 import org.apache.soap.server.TypeMapping;
00045 import org.apache.soap.util.xml.QName;
00046 import org.objectweb.fractal.api.Component;
00047 import org.objectweb.fractal.api.Interface;
00048 import org.objectweb.fractal.api.control.LifeCycleController;
00049 import org.objectweb.proactive.core.body.http.util.HttpMarshaller;
00050 import org.objectweb.proactive.core.component.type.ProActiveInterfaceTypeImpl;
00051 import org.objectweb.proactive.ext.webservices.WSConstants;
00052 import org.objectweb.proactive.ext.webservices.wsdl.WSDLGenerator;
00053 
00054 
00060 public class ProActiveDeployer extends WSConstants {
00061 
00069     public static void deploy(String urn, String url, Object o, String[] methods) {
00070         deploy(urn, url, o, null, methods, false);
00071     }
00072 
00081     public static void deployComponent(String componentName, String url,
00082         Component component) {
00083         Object[] interfaces = component.getFcInterfaces();
00084 
00085         for (int i = 0; i < interfaces.length; i++) {
00086             Interface interface_ = ((Interface) interfaces[i]);
00087 
00088             /* only expose server interfaces and not the lifecycle controller */
00089             if (!(interface_ instanceof LifeCycleController)) {
00090                 if (!((ProActiveInterfaceTypeImpl) interface_.getFcItfType()).isFcClientItf()) {
00091                     String name = interface_.getFcItfName();
00092 
00093                     /* get all the public methods */
00094                     Method[] methods = interface_.getClass().getMethods();
00095                     Vector meths = new Vector();
00096 
00097                     for (int j = 0; j < methods.length; j++) {
00098                         String methodName = methods[j].getName();
00099 
00100                         if (isAllowedMethod(methodName)) {
00101                             meths.addElement(methodName);
00102                         }
00103                     }
00104 
00105                     String[] methsArray = new String[meths.size()];
00106                     meths.toArray(methsArray);
00107                     deploy(componentName + "_" + name, url, interface_,
00108                         component, methsArray, true);
00109                 }
00110             }
00111         }
00112     }
00113 
00119     public static void undeploy(String urn, String url) {
00120         ServiceManagerClient serviceManagerClient = null;
00121 
00122         try {
00123             serviceManagerClient = new ServiceManagerClient(new URL(url +
00124                         ROUTER));
00125             serviceManagerClient.undeploy(urn);
00126         } catch (MalformedURLException e) {
00127             e.printStackTrace();
00128         } catch (SOAPException e1) {
00129             e1.printStackTrace();
00130         }
00131     }
00132 
00139     public static void undeployComponent(String componentName, String url,
00140         Component component) {
00141         Object[] interfaces = component.getFcInterfaces();
00142 
00143         for (int i = 0; i < interfaces.length; i++) {
00144             Interface inter = (Interface) interfaces[i];
00145 
00146             if (!(inter instanceof LifeCycleController)) {
00147                 if (!((ProActiveInterfaceTypeImpl) inter.getFcItfType()).isFcClientItf()) {
00148                     String serviceName = componentName + "_" +
00149                         inter.getFcItfName();
00150                     undeploy(serviceName, url);
00151                 }
00152             }
00153         }
00154     }
00155 
00156     /*
00157      * deploy services.
00158      */
00159     private static void deploy(String urn, String url, Object o, Component c,
00160         String[] methods, boolean componentInterface) {
00161 
00162         /* first we need to generate a WSDL description of the object we want to deploy */
00163         String wsdl = WSDLGenerator.getWSDL(o.getClass().getSuperclass(), urn,
00164                 url + ROUTER, DOCUMENTATION, methods);
00165 
00166         /*For deploying an active object we need a ServiceManagerClient that will contact the Serlvet */
00167         ServiceManagerClient serviceManagerClient = null;
00168 
00169         try {
00170             serviceManagerClient = new ServiceManagerClient(new URL(url +
00171                         ROUTER));
00172         } catch (MalformedURLException e) {
00173             e.printStackTrace();
00174         }
00175 
00176         /* The informations about services will be put in a deployment descriptor */
00177         DeploymentDescriptor dd = new DeploymentDescriptor();
00178         dd.setID(urn);
00179         dd.setProviderType(DeploymentDescriptor.PROVIDER_USER_DEFINED);
00180 
00181         dd.setServiceClass(PROACTIVE_PROVIDER);
00182 
00183         dd.setIsStatic(false);
00184 
00185         /* Get the type mapping for customized returned types */
00186         TypeMapping[] tms = getMappings(o.getClass(), methods);
00187         dd.setMappings(tms);
00188 
00189         if (methods != null) {
00190             dd.setMethods(methods);
00191         } else {
00192             Method[] ms = o.getClass().getDeclaredMethods();
00193             Vector mv = new Vector();
00194             for (int i = 0; i < ms.length; i++)
00195                 if (!disallowedMethods.contains(ms[i].getName())) {
00196                     mv.addElement(ms[i].getName());
00197                 }
00198             methods = new String[mv.size()];
00199             Enumeration e = mv.elements();
00200             int j = 0;
00201             while (e.hasMoreElements())
00202                 methods[j++] = (String) e.nextElement();
00203 
00204             dd.setMethods(methods);
00205         }
00206 
00207         dd.setProviderClass(o.getClass().getName());
00208 
00209         /* Here we put the serialized stub into a dd property */
00210         Hashtable props = new Hashtable();
00211 
00212         props.put(WSDL_FILE, wsdl);
00213 
00214         if (componentInterface) {
00215             props.put(COMPONENT_INTERFACE, "true");
00216             props.put(PROACTIVE_STUB, HttpMarshaller.marshallObject(c));
00217         } else {
00218             props.put(COMPONENT_INTERFACE, "false");
00219             props.put(PROACTIVE_STUB, HttpMarshaller.marshallObject(o));
00220         }
00221 
00222         dd.setProps(props);
00223 
00224         try {
00225             serviceManagerClient.deploy(dd);
00226         } catch (SOAPException e1) {
00227             e1.printStackTrace();
00228         }
00229     }
00230 
00231     /*
00232      * check if a method can be exposed as WS
00233      */
00234     private static boolean isAllowedMethod(String method) {
00235         return !disallowedMethods.contains(method);
00236     }
00237 
00238     /*
00239      *  Gets the types mapping for custonize returned types
00240      * Only Java Beans are supported
00241      */
00242     private static TypeMapping[] getMappings(Class c, String[] methods) {
00243         Vector tms = new Vector();
00244         Vector sMethods = new Vector();
00245 
00246         if (methods != null) {
00247             for (int i = 0; i < methods.length; i++) {
00248                 sMethods.addElement(methods[i]);
00249             }
00250 
00251             Vector mMethods = new Vector();
00252             Method[] ms = c.getDeclaredMethods();
00253 
00254             for (int i = 0; i < ms.length; i++) {
00255                 mMethods.addElement(ms[i]);
00256             }
00257 
00258             Enumeration e = mMethods.elements();
00259 
00260             while (e.hasMoreElements()) {
00261                 Method m = (Method) e.nextElement();
00262 
00263                 if (sMethods.contains(m.getName())) {
00264                     Class[] parameters = m.getParameterTypes();
00265 
00266                     for (int j = 0; j < parameters.length; j++) {
00267                         if (!supportedTypes.contains(parameters[j])) {
00268                             String pname = extractName(parameters[j]);
00269 
00270                             TypeMapping tm = new TypeMapping("http://schemas.xmlsoap.org/soap/encoding/",
00271                                     new QName("http://" + pname,
00272                                         getSimpleName(parameters[j])),
00273                                     parameters[j].getName(),
00274                                     "org.apache.soap.encoding.soapenc.BeanSerializer",
00275                                     "org.apache.soap.encoding.soapenc.BeanSerializer");
00276 
00277                             tms.addElement(tm);
00278                         }
00279                     }
00280                 }
00281             }
00282         } else { // methods == null
00283             Method[] ms = c.getDeclaredMethods();
00284 
00285             for (int i = 0; i < ms.length; i++) {
00286                 if (!disallowedMethods.contains(ms[i].getName())) {
00287                     Class[] parameters = ms[i].getParameterTypes();
00288                     for (int j = 0; j < parameters.length; j++) {
00289                         if (!supportedTypes.contains(parameters[j])) {
00290                             String pname = extractName(parameters[j]);
00291 
00292                             TypeMapping tm = new TypeMapping("http://schemas.xmlsoap.org/soap/encoding/",
00293                                     new QName("http://" + pname,
00294                                         getSimpleName(parameters[j])),
00295                                     parameters[j].getName(),
00296                                     "org.apache.soap.encoding.soapenc.BeanSerializer",
00297                                     "org.apache.soap.encoding.soapenc.BeanSerializer");
00298 
00299                             tms.addElement(tm);
00300                         }
00301                     }
00302                 }
00303             }
00304         }
00305 
00306         TypeMapping[] tmsArray = new TypeMapping[tms.size()];
00307         Enumeration e = tms.elements();
00308 
00309         int i = 0;
00310 
00311         while (e.hasMoreElements()) {
00312             tmsArray[i++] = (TypeMapping) e.nextElement();
00313         }
00314 
00315         return tmsArray;
00316     }
00317 
00318     private static String getSimpleName(Class c) {
00319         String cName = c.getName();
00320         StringTokenizer st = new StringTokenizer(cName, ".");
00321         String result = "";
00322 
00323         while (st.hasMoreTokens())
00324             result = st.nextToken();
00325 
00326         return result;
00327     }
00328 
00329     /*
00330      * Utility to construct the namespace of the type mapping
00331      */
00332     private static String extractName(Class c) {
00333         String result = new String();
00334 
00335         Package p = c.getPackage();
00336 
00337         if (p != null) {
00338             String packageName = p.getName();
00339             StringTokenizer st = new StringTokenizer(packageName, ".");
00340             int nbTokens = st.countTokens();
00341             String[] tmp = new String[nbTokens];
00342             int n = 0;
00343 
00344             while (st.hasMoreTokens())
00345                 tmp[n++] = st.nextToken();
00346 
00347             for (int i = nbTokens - 1; i > -1; i--)
00348                 result += (tmp[i] + ".");
00349 
00350             return result.substring(0, result.length() - 1);
00351         }
00352 
00353         return "DefaultNamespace";
00354     }
00355 }

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