Mail Archive Home | orchestra-commits List | July 2007 Index
| <-- Date Index --> | <-- Thread Index --> |
+Simple Invoker to JUnit test
--- branches/Orchestra_4_0/src/main/java/org/objectweb/orchestra/webservices/SimpleInvoker.java 2007-07-13 13:53:22 UTC (rev 699)
+++ branches/Orchestra_4_0/src/main/java/org/objectweb/orchestra/webservices/SimpleInvoker.java 2007-07-16 14:52:05 UTC (rev 700)
@@ -0,0 +1,405 @@
+/**
+ * Copyright (C) 2007 Bull S. A. S.
+ * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
+ * This library is free software; you can redistribute it and/or modify it under the terms
+ * of the GNU Lesser General Public License as published by the Free Software Foundation
+ * version 2.1 of the License.
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License along with this
+ * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
+ * Floor, Boston, MA 02110-1301, USA.
+ *
+ * Invoker.java
+ *
+ * @author Goulven Le Jeune, Charles Souillard, Candy Chlad, Stanislas Giraudet De Boudemange
+ *
+ * Created on : Jul 3, 2007
+ *
+ * $Id$
+ **/
+package org.objectweb.orchestra.webservices;
+
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.wsdl.Operation;
+import javax.wsdl.Part;
+import javax.wsdl.PortType;
+import javax.xml.namespace.QName;
+
+import org.apache.axis.constants.Style;
+import org.apache.axis.constants.Use;
+import org.apache.axis.message.SOAPBodyElement;
+import org.objectweb.orchestra.def.TypeOfMessage;
+import org.objectweb.orchestra.parsing.WsdlXmlParseResult;
+import org.objectweb.orchestra.run.RunTime;
+import org.objectweb.orchestra.xml.InformationItem;
+import org.objectweb.orchestra.xml.Message;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+
+/**
+ * SimpleInvoker.java.
+ * Used to perform tests.
+ * @author Goulven Le Jeune, Charles Souillard, Candy Chlad, Stanislas Giraudet De Boudemange
+ *
+ * Created on : Jul 3, 2007
+ */
+public class SimpleInvoker implements ServiceInvoker {
+
+
+ /**The QName of the soap binding element in WSDL.*/
+ private static final QName SOAP_BINDING=new QName("http://schemas.xmlsoap.org/wsdl/soap/", "binding");
+
+ /** The QName of the soap operation element in WSDL.*/
+ private static final QName SOAP_OPERATION=new QName("http://schemas.xmlsoap.org/wsdl/soap/", "operation");
+
+ /** The QName of the soap body element in WSDL.*/
+ private static final QName SOAP_BODY=new QName("http://schemas.xmlsoap.org/wsdl/soap/", "body");
+
+ /** Defaut style. */
+ private static final Style DEFAUT_STYLE=Style.DOCUMENT;
+
+ /** Defaut use. */
+ private static final Use DEFAUT_USE=Use.LITERAL;
+
+
+ /**
+ * True if the RMI is used to access waitingMessageClass.
+ */
+ private boolean useRMI;
+
+
+ /**
+ * ServiceReceiver of the process.
+ */
+ private ServiceReceiver receiver;
+
+ /**
+ * Idf of message send to the process, to find back the sender to give him the response.
+ */
+ private static Integer idf=0;
+
+ /**
+ * Port of the rmiregistry.
+ */
+ private int port;
+ /**
+ * Hash Map of sender.
+ * Keys:idf of sender
+ * Values: WaitingMessageClassName in the RMI registry
+ */
+ private HashMap<String, String> senders;
+
+ /**
+ * Hash Map of sender.
+ * Keys:idf of sender
+ * Values: WaitingMessageClassName
+ */
+ private HashMap<String, WaitingMessageClass> sendersWithoutRmi;
+
+
+
+
+ /**
+ *
+ * Identifier of an Invoke message.
+ */
+ public class InvokeMessageIdentifier{
+ public String endPoint;
+ public String operation;
+ public QName portType;
+ public InvokeMessageIdentifier(final String endPoint, final String operation, final QName portType){
+ this.endPoint=endPoint;
+ this.operation=operation;
+ this.portType=portType;
+ }
+ /**
+ * get the string for that object.
+ * @return the String value of the Object
+ */
+ public String toString(){
+ return "endPoint='"+this.endPoint+"'+ operation='"+this.operation+"' + portType='"+this.portType+"'";
+ }
+
+ /**
+ * calcul the hashCode of that object.
+ * @return the hashCode
+ */
+ public int hashCode(){
+ return toString().hashCode();
+ }
+
+
+ /**
+ * Compare the InvokeMessageIdentifier with another one.
+ * @param obj the InvokeMessageIdentifier to compare to.
+ * @return true if the InvokeMessageIdentifiers are equal.
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (!InvokeMessageIdentifier.class.isInstance(obj)){
+ return false;
+ }
+ if(obj==null){
+ return false;
+ }
+ InvokeMessageIdentifier imi=(InvokeMessageIdentifier) obj;
+ return (this.operation.equals(imi.operation)
+ && this.portType.equals(imi.portType)
+ && this.endPoint.equals(imi.endPoint));
+
+ }
+
+ }
+
+ /**
+ * Association of an invoke message and the runTime.
+ */
+ public class InvokeMessage{
+ public Message message;
+ public RunTime runTime;
+ public InvokeMessage(final Message message, final RunTime runTime){
+ this.message=message;
+ this.runTime=runTime;
+ }
+ }
+
+
+
+ /**
+ * HashMap to store messages send to partners with an invoke.
+ */
+ private HashMap<InvokeMessageIdentifier, InvokeMessage> invokeMessages;
+
+
+
+ /**
+ * private construtor. Only one instance of this class could be create.
+ * @param port : port of the rmiregistry.
+ * @param useRmi : True if the RMI is used to access waitingMessageClass.
+ * @param receiver : ServiceReceiver of the process.
+ */
+ public SimpleInvoker(final int port, final boolean useRmi, final ServiceReceiver receiver){
+ this.receiver=receiver;
+ this.port=port;
+ this.useRMI=useRmi;
+ invokeMessages=new HashMap<InvokeMessageIdentifier, InvokeMessage>();
+ if (useRmi){
+ senders=new HashMap<String, String>();
+ }else{
+ sendersWithoutRmi=new HashMap<String, WaitingMessageClass>();
+ }
+ }
+
+ /**
+ * Signal a new sender.
+ * @param waitingClassName The name of class that wait the response in the RMI registry.
+ * @return the idf
+ * @throws RemoteException - ex
+ */
+ public String newSender(final String waitingClassName)throws RemoteException{
+ if (useRMI){
+ String senderIdf=getIdf();
+ synchronized (senders) {
+ senders.put(senderIdf, waitingClassName);
+ }
+ return senderIdf;
+ }
+ return null;
+ }
+
+ /**
+ * Signal a new sender.
+ * @param waitingClass The class that wait the response.
+ * @return the idf
+ * @throws RemoteException - ex
+ */
+ public String newSender(final WaitingMessageClass waitingClass)throws RemoteException{
+ if (!useRMI){
+ String senderIdf=getIdf();
+ synchronized (sendersWithoutRmi) {
+ sendersWithoutRmi.put(senderIdf, waitingClass);
+ }
+ return senderIdf;
+ }
+ return null;
+ }
+
+ /**
+ * Sends a message to a partner during an invocation. This calls an operation of another Web Service.
+ * @param message message to send to the partner
+ * @param endPoint endPoint of the partner called
+ * @param operation operation of the partner
+ * @param portType PortType of the partner
+ * @param runTime runTime of the activity which is calling this method. It can be used to give
+ * an answer to that activity.
+ * @param wsdlDefinition Definition of the wsdl.
+ * @throws RemoteException -ex
+ */
+ public void invokeCall(final Message message, final String endPoint,
+ final Operation operation, final PortType portType,
+ final RunTime runTime, final WsdlXmlParseResult wsdlDefinition) throws RemoteException{
+
+
+ InvokeMessageIdentifier imi=new InvokeMessageIdentifier(endPoint, operation.getName(), portType.getQName());
+ InvokeMessage im=new InvokeMessage(message, runTime);
+ synchronized (invokeMessages) {
+ invokeMessages.put(imi, im);
+ }
+
+
+
+ }
+
+
+
+ /**
+ * Build the array of SOAPBodyElements which will be in the body element
+ * of the SOAP message, from the BLEP message and the operation used.
+ * To be compliant with the Basic profile, the body would have only one SOAPBodyElement.
+ * @param message BPEL message, which contains parts. (only one part to be compliant with the basic profile).
+ * @param operation WSDL operation.
+ * @return the params of the operation. This params should be an array of one SOAPBodyElement to be compliant
+ * with the basic profile.
+ */
+ public static Object[] buildSOAPDocumentLiteralParams(final Message message, final Operation operation){
+ Vector<Element> soapBodyElements=new Vector<Element>();
+ Map parts=operation.getInput().getMessage().getParts();
+ for (Object partObj:parts.values()){
+ soapBodyElements.add((Element) message.getPart(((Part) partObj).getName()).getRootElement());
+ }
+
+
+ //parameters of the SWDL operation
+ Object[] params=new Object[soapBodyElements.size()];
+ int i=0;
+ for (Element soapBodyElement:soapBodyElements){
+ params[i]=new SOAPBodyElement(soapBodyElement);
+ i++;
+ }
+
+ return params;
+ }
+
+ /**
+ * Build the PBEL Message from the result of the call for a document style
+ * and an literal use.
+ * To be compliant with the Basic profile, the result would have only one element.
+ * @param elementsOfResult Node elements which make the result of the operation call.
+ * @param operation WSDL operation.
+ * @return the new BPEL Message.
+ */
+ public static Message buildSOAPDocumentLiteralMessage(final Vector elementsOfResult, final Operation operation){
+ HashMap <String, InformationItem> parts=new HashMap<String, InformationItem>();
+ int index=0;
+ for (Object partObj:operation.getOutput().getMessage().getParts().values()){
+ String partName=((Part) partObj).getName();
+ InformationItem responsePart=InformationItem.createInstance((Node) elementsOfResult.get(index));
+ parts.put(partName, responsePart);
+ }
+ return new Message(parts);
+ }
+
+
+
+ /**
+ * Replies to a user that has sent a message. This method is called by
+ * a <reply> activity.
+ * @param message message of the response
+ * @param idf identifier of the user that has initially sent a message.
+ * The response will be sent to that identifier.
+ * @throws RemoteException - ex
+ */
+ public void reply(final Message message, final String idf) throws RemoteException{
+ if(useRMI){
+ if (senders.containsKey(idf)){
+ String className=senders.get(idf);
+ try{
+ if (System.getSecurityManager() == null) {
+ System.setSecurityManager(new SecurityManager());
+ }
+ Registry registry = LocateRegistry.getRegistry(this.port);
+ WaitingMessageClass waitingclass=(WaitingMessageClass) registry.lookup(className);
+ waitingclass.setMessageReturned(idf, message);
+ }catch(Exception ex){
+ ex.printStackTrace();
+ }
+ synchronized (senders) {
+ senders.remove(idf);
+ }
+ }
+ }else{
+ if (sendersWithoutRmi.containsKey(idf)){
+ System.out.println("We have found the partner idf");
+ WaitingMessageClass waitingClass=sendersWithoutRmi.get(idf);
+
+ waitingClass.setMessageReturned(idf, message);
+
+ synchronized (sendersWithoutRmi) {
+ sendersWithoutRmi.remove(idf);
+ }
+ }
+ }
+ }
+
+ /**
+ * @return the idf
+ */
+ private static String getIdf() {
+ synchronized (idf) {
+ idf++;
+ return idf.toString();
+ }
+
+ }
+
+
+ /**
+ * Get an invoke message. Return null if the message is still not received.
+ * @param endPoint endPoint of the invoke
+ * @param operation operation name of the invoke
+ * @param portType portType QName of the invoke.
+ * @return The InvokeMessage if received. Null else.
+ */
+ public InvokeMessage getInvokeMessage(final String endPoint, final String operation, final QName portType){
+ InvokeMessageIdentifier imi=new InvokeMessageIdentifier(endPoint, operation, portType);
+
+ synchronized (invokeMessages) {
+ if (invokeMessages.containsKey(imi)){
+ return invokeMessages.get(imi);
+ }
+ }
+
+ return null;
+ }
+
+
+ /**
+ * Remove an invoke message that correspond to the endpoint, the operation and the portType
+ * given in parameter.
+ * @param endPoint endPoint of the invoke
+ * @param operation operation name of the invoke
+ * @param portType portType QName of the invoke.
+ */
+ public void removeInvokeMessage(final String endPoint, final String operation, final QName portType){
+ InvokeMessageIdentifier imi=new InvokeMessageIdentifier(endPoint, operation, portType);
+
+
+ synchronized (invokeMessages) {
+ if (invokeMessages.containsKey(imi)){
+ invokeMessages.remove(imi);
+ }
+ }
+
+ }
+}
+
+
Property changes on: branches/Orchestra_4_0/src/main/java/org/objectweb/orchestra/webservices/SimpleInvoker.java
___________________________________________________________________
Name: svn:keywords
+ Id
| <-- Date Index --> | <-- Thread Index --> |
Powered by MHonArc.
Copyright © 2006-2007, OW2 Consortium | contact | webmaster.