openamf/src/java/org/openamf/invoker WebServiceInvoker.java,1.5,1.6

[email protected]
Newsgroups gmane.comp.java.openamf.cvs
Message-ID <[email protected]>
Update of /cvsroot/openamf/openamf/src/java/org/openamf/invoker
In directory sc8-pr-cvs1:/tmp/cvs-serv20481/src/java/org/openamf/invoker

Modified Files:
	WebServiceInvoker.java 
Log Message:


Index: WebServiceInvoker.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/invoker/WebServiceInvoker.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** WebServiceInvoker.java	30 Apr 2003 01:02:05 -0000	1.5
--- WebServiceInvoker.java	30 Apr 2003 21:26:02 -0000	1.6
***************
*** 1,32 ****
  package org.openamf.invoker;
  
! import org.apache.axis.Constants;
! import org.apache.axis.encoding.ser.SimpleDeserializer;
! import org.apache.axis.wsdl.gen.Parser;
! import org.apache.axis.wsdl.symbolTable.BaseType;
! import org.apache.axis.wsdl.symbolTable.BindingEntry;
! import org.apache.axis.wsdl.symbolTable.Parameter;
! import org.apache.axis.wsdl.symbolTable.Parameters;
! import org.apache.axis.wsdl.symbolTable.ServiceEntry;
! import org.apache.axis.wsdl.symbolTable.SymTabEntry;
! import org.apache.axis.wsdl.symbolTable.SymbolTable;
! import org.apache.axis.wsdl.symbolTable.TypeEntry;
  
! import javax.wsdl.Binding;
! import javax.wsdl.Operation;
! import javax.wsdl.Port;
! import javax.wsdl.Service;
! import javax.wsdl.extensions.soap.SOAPAddress;
! import javax.xml.namespace.QName;
! import javax.xml.rpc.Call;
! import javax.xml.rpc.encoding.Deserializer;
! import javax.xml.rpc.encoding.DeserializerFactory;
  import java.util.HashMap;
- import java.util.Iterator;
  import java.util.List;
- import java.util.Map;
- import java.util.Vector;
- import javax.wsdl.extensions.soap.*;
- import javax.wsdl.*;
  
  import org.apache.commons.logging.Log;
--- 1,15 ----
  package org.openamf.invoker;
  
! import org.apache.axis.encoding.*;
  
! import org.openamf.ASObject;
! import java.beans.BeanInfo;
! import java.beans.IntrospectionException;
! import java.beans.Introspector;
! import java.beans.PropertyDescriptor;
! import java.lang.reflect.InvocationTargetException;
! import java.lang.reflect.Method;
  import java.util.HashMap;
  import java.util.List;
  
  import org.apache.commons.logging.Log;
***************
*** 34,421 ****
  import org.openamf.ServiceRequest;
  
! /*
!  * 
!  * 
!  * @author Adrian Roston
!  *
!  * Notes:
!  * What I did to exclude non-primitive types for parameters:
!  * returned null from getParamData and included this line:
!  * if(map==null)return new String("Server does not support complex types");
!  * 
!  */
! 
! public class WebServiceInvoker extends ServiceInvoker {
! 
! 	private static Log log = LogFactory.getLog(WebServiceInvoker.class);
! 	private Parser wsdlParser = null;
! 	private SimpleWebServiceObject webserviceobject = null;
! 	private String operationName = null;
! 
! 	public WebServiceInvoker(
! 		ServiceRequest request,
! 		javax.servlet.ServletContext ctx) {
! 		super(request, ctx);
! 	}
! 
! 	public Object invokeService() throws ServiceInvocationException {
! 
! 		HashMap map = null;
! 		webserviceobject = new SimpleWebServiceObject();
! 		SimpleWSObjManager manager = null;
! 		String wsdlLocation = request.getServiceName();
! 		operationName = request.getServiceMethodName();
! 		try {
! 
! 			manager =
! 				(SimpleWSObjManager) servletContext.getAttribute(
! 					"SimpleWSObjManager");
! 			log.debug("manager is null:" + (manager == null));
! 			if (manager == null) {
! 				servletContext.setAttribute(
! 					"SimpleWSObjManager",
! 					new SimpleWSObjManager());
! 				manager =
! 					(SimpleWSObjManager) servletContext.getAttribute(
! 						"SimpleWSObjManager");
! 			}
! 			log.debug("manager is null:" + (manager == null));
! 			SimpleWebServiceObject storedObj =
! 				manager.getSimpleWebservice(wsdlLocation + "#" + operationName);
! 			log.debug("storedObj is null:" + (storedObj == null));
! 			if (storedObj != null) {
! 				log.debug(
! 					"........Doing operation from stored object!....................");
! 				org.apache.axis.client.Service service =
! 					new org.apache.axis.client.Service();
! 				org.apache.axis.client.Call call =
! 					(org.apache.axis.client.Call) service.createCall();
! 				call.setTargetEndpointAddress(
! 					new java.net.URL(storedObj.getEndpoint()));
! 				call.setOperationName(
! 					new QName(storedObj.getNamespaceURI(), operationName));
! 				return call.invoke(storedObj.getArgs());
! 			}
! 
! 			List parameters = request.getParameters();
! 			String portName = null;
! 			portName = null;
! 			wsdlParser = new Parser();
! 			wsdlParser.run(wsdlLocation);
  
! 			String[] p = new String[parameters.size()];
! 			for (int i = 0; i < parameters.size(); ++i) {
! 				p[i] = (parameters.get(i).toString());
! 			}
  
! 			map = invokeMethod(operationName, portName, p);
  
! 			if (map == null)
! 				return new String("Server does not support complex types");
  
! 			webserviceobject.setArgs(p);
! 			// print result
! 			log.debug("Result:");
! 			for (Iterator it = map.keySet().iterator(); it.hasNext();) {
! 				String name = (String) it.next();
! 				log.debug(name + "=" + map.get(name));
! 			}
! 			log.debug("\nDone!");
  
! 		} catch (Exception e) {
! 			log.debug(e.toString());
  
  		}
  
- 		//if here, store new object w/sanity check
- 
- 		log.debug(webserviceobject.getArgs().toString());
- 		log.debug(webserviceobject.getEndpoint());
- 		log.debug(webserviceobject.getNamespaceURI());
  
! 		if (webserviceobject.getArgs() != null
! 			&& webserviceobject.getEndpoint() != null
! 			&& webserviceobject.getNamespaceURI() != null) {
! 			manager.addSimpleWebservice(
! 				wsdlLocation + "#" + operationName,
! 				webserviceobject);
! 			servletContext.setAttribute("SimpleWSObjManager", manager);
! 		}
  
- 		Object response = map;
- 		return response;
- 	}
- 	/**
- 		 * Method invokeMethod
- 		 *
- 		 * @param wsdlLocation
- 		 * @param operationName
- 		 * @param inputName
- 		 * @param outputName
- 		 * @param portName
- 		 * @param args
- 		 *
- 		 * @return
- 		 *
- 		 * @throws Exception
- 		 */
- 	public HashMap invokeMethod(
- 		String operationName,
- 		String portName,
- 		String[] args)
- 		throws Exception {
- 		String serviceNS = null;
- 		String serviceName = null;
- 		String operationQName = null;
  
! 		System.out.println("Preparing Axis dynamic invocation");
! 		Service service = selectService(serviceNS, serviceName);
! 		Operation operation = null;
! 		org.apache.axis.client.Service dpf =
! 			new org.apache.axis.client.Service(wsdlParser, service.getQName());
! 
! 		Vector inputs = new Vector();
! 		Port port = selectPort(service.getPorts(), portName);
! 		if (portName == null) {
! 			portName = port.getName();
! 		}
! 		Binding binding = port.getBinding();
  
! 		/* this is just to get the namespace uri
! 				 * 
! 				 * 
! 				 */
  
- 		log.debug("port:" + port);
- 		for (Iterator i = port.getBinding().getBindingOperations().iterator();
- 			i.hasNext();
- 			) {
- 			BindingOperation o = (BindingOperation) i.next();
- 			for (Iterator ees =
- 				o.getBindingInput().getExtensibilityElements().iterator();
- 				ees.hasNext();
- 				) {
- 				SOAPBody b = (SOAPBody) ees.next();
- 				webserviceobject.setNamespaceURI(b.getNamespaceURI());
- 				log.debug("setting namespaceuri:" + b.getNamespaceURI());
- 			}
- 		}
  
! 		Call call =
! 			dpf.createCall(
! 				QName.valueOf(portName),
! 				QName.valueOf(operationName));
  
! 		// Output types and names
! 		Vector outNames = new Vector();
  
- 		// Input types and names
- 		Vector inNames = new Vector();
- 		Vector inTypes = new Vector();
- 		SymbolTable symbolTable = wsdlParser.getSymbolTable();
- 		BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
- 		Parameters parameters = null;
- 		Iterator i = bEntry.getParameters().keySet().iterator();
  
! 		while (i.hasNext()) {
! 			Operation o = (Operation) i.next();
! 			if (o.getName().equals(operationName)) {
! 				operation = o;
! 				parameters = (Parameters) bEntry.getParameters().get(o);
  
- 				log.debug("PARAMETERS..........\n" + parameters.toString());
- 				break;
- 			}
- 		}
- 		if ((operation == null) || (parameters == null)) {
- 			throw new RuntimeException(operationName + " was not found.");
- 		}
  
- 		// loop over paramters and set up in/out params
- 		for (int j = 0; j < parameters.list.size(); ++j) {
- 			Parameter p = (Parameter) parameters.list.get(j);
  
! 			if (p.getMode() == 1) { // IN
! 				inNames.add(p.getQName().getLocalPart());
! 				inTypes.add(p);
! 			} else if (p.getMode() == 2) { // OUT
! 				outNames.add(p.getQName().getLocalPart());
! 			} else if (p.getMode() == 3) { // INOUT
! 				inNames.add(p.getQName().getLocalPart());
! 				inTypes.add(p);
! 				outNames.add(p.getQName().getLocalPart());
! 			}
! 		}
  
! 		// set output type
! 		if (parameters.returnParam != null) {
! 			// Get the QName for the return Type
! 			QName returnType =
! 				org.apache.axis.wsdl.toJava.Utils.getXSIType(
! 					parameters.returnParam);
! 			QName returnQName = parameters.returnParam.getQName();
  
! 			outNames.add(returnQName.getLocalPart());
! 		}
  
! 		if (inNames.size() != args.length)
! 			throw new RuntimeException(
! 				"Need " + inNames.size() + " arguments!!!");
  
- 		for (int pos = 0; pos < inNames.size(); ++pos) {
- 			String arg = args[pos];
- 			Parameter p = (Parameter) inTypes.get(pos);
- 			Object obj =
- 				getParamData((org.apache.axis.client.Call) call, p, arg);
- 			if (obj == null)
- 				return null;
- 			inputs.add(obj);
- 		}
- 		System.out.println(
- 			"Executing operation " + operationName + " with parameters:");
- 		for (int j = 0; j < inputs.size(); j++) {
- 			System.out.println(inNames.get(j) + "=" + inputs.get(j));
- 		}
- 		Object ret = call.invoke(inputs.toArray());
- 		Map outputs = call.getOutputParams();
- 		HashMap map = new HashMap();
  
- 		for (int pos = 0; pos < outNames.size(); ++pos) {
- 			String name = (String) outNames.get(pos);
- 			Object value = outputs.get(name);
  
! 			if ((value == null) && (pos == 0)) {
! 				map.put(name, ret);
! 			} else {
! 				map.put(name, value);
! 			}
  		}
! 		return map;
! 	}
  
! 	/**
! 	 * Method getParamData
! 	 *
! 	 * @param c
! 	 * @param arg
! 	 */
! 	private Object getParamData(
! 		org.apache.axis.client.Call c,
! 		Parameter p,
! 		String arg)
! 		throws Exception {
! 		// Get the QName representing the parameter type
! 		QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
  
! 		TypeEntry type = p.getType();
! 		if (type instanceof BaseType && ((BaseType) type).isBaseType()) {
! 			DeserializerFactory factory =
! 				c.getTypeMapping().getDeserializer(paramType);
! 			Deserializer deserializer =
! 				factory.getDeserializerAs(Constants.AXIS_SAX);
! 			if (deserializer instanceof SimpleDeserializer) {
! 				return ((SimpleDeserializer) deserializer).makeValue(arg);
  			}
- 		}
- 		//throw new RuntimeException("not know how to convert '" + arg
- 		//						   + "' into " + c);
- 		return null;
- 	}
  
! 	/**
! 	 * Method selectService
! 	 *
! 	 * @param def
! 	 * @param serviceNS
! 	 * @param serviceName
! 	 *
! 	 * @return
! 	 *
! 	 * @throws Exception
! 	 */
! 	public Service selectService(String serviceNS, String serviceName)
! 		throws Exception {
! 		QName serviceQName =
! 			(((serviceNS != null) && (serviceName != null))
! 				? new QName(serviceNS, serviceName)
! 				: null);
! 		ServiceEntry serviceEntry =
! 			(ServiceEntry) getSymTabEntry(serviceQName, ServiceEntry.class);
! 		return serviceEntry.getService();
! 	}
  
! 	/**
! 	 * Method getSymTabEntry
! 	 *
! 	 * @param qname
! 	 * @param cls
! 	 *
! 	 * @return
! 	 */
! 	public SymTabEntry getSymTabEntry(QName qname, Class cls) {
! 		HashMap map = wsdlParser.getSymbolTable().getHashMap();
! 		Iterator iterator = map.entrySet().iterator();
  
! 		while (iterator.hasNext()) {
! 			Map.Entry entry = (Map.Entry) iterator.next();
! 			QName key = (QName) entry.getKey();
! 			Vector v = (Vector) entry.getValue();
  
- 			if ((qname == null) || qname.equals(qname)) {
- 				for (int i = 0; i < v.size(); ++i) {
- 					SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
  
! 					if (cls.isInstance(symTabEntry)) {
! 						return symTabEntry;
! 					}
! 				}
! 			}
  		}
- 		return null;
- 	}
  
! 	/**
! 	 * Method selectPort
! 	 *
! 	 * @param ports
! 	 * @param portName
! 	 *
! 	 * @return
! 	 *
! 	 * @throws Exception
! 	 */
! 	public Port selectPort(Map ports, String portName) throws Exception {
! 		Iterator valueIterator = ports.keySet().iterator();
! 		while (valueIterator.hasNext()) {
! 			String name = (String) valueIterator.next();
! 
! 			if ((portName == null) || (portName.length() == 0)) {
! 				Port port = (Port) ports.get(name);
  
! 				List list = port.getExtensibilityElements();
  
! 				for (int i = 0;(list != null) && (i < list.size()); i++) {
! 					Object obj = list.get(i);
! 					if (obj instanceof SOAPAddress) {
! 						SOAPAddress soapAddress = (SOAPAddress) obj;
! 						String serviceEndpoint = soapAddress.getLocationURI();
  
! 						log.debug("..............Endpoint:" + serviceEndpoint);
! 						webserviceobject.setEndpoint(serviceEndpoint);
  
! 						return port;
! 					}
! 				}
! 			} else if ((name != null) && name.equals(portName)) {
! 				return (Port) ports.get(name);
! 			}
! 		}
! 		return null;
  	}
! 
  	public boolean supports(ServiceRequest request) {
  		return (
  			request.getServiceName().toLowerCase().lastIndexOf("wsdl") != -1);
  	}
  
  }
--- 17,248 ----
  import org.openamf.ServiceRequest;
  
! //import org.apache.axis.client.Call;
! import org.apache.axis.client.Service;
! //import javax.xml.namespace.QName;
  
! import org.apache.tools.ant.*;
! import java.beans.*;
! import java.io.*;
! import java.lang.reflect.*;
! import java.net.*;
  
! public class WebServiceInvoker extends JavaServiceInvoker {
  
! 	private static Log log = LogFactory.getLog(WebServiceInvoker.class);
  
! public WebServiceInvoker(ServiceRequest request, javax.servlet.ServletContext ctx) {
  
! 			super(request, ctx);
  
  		}
  
  
! public  Object invokeService() throws ServiceInvocationException{
  
  
! /*
!  * TODO: acquire className and requestedMethodName
!  * 
!  * look to see if mapping exists in context
!  * if not, have to do the whole whammie
!  * 
!  * if so, we should probably store the two classnames so we 
!  * dont have to do the introspection, maybe store ALL the method names?
!  * 
!  * look closer, maybe all we want to do is invoke()
!  * 
!  * actually looks like all we need is the package name,
!  * which we make up and then provide the ant task.
!  * 
!  * 
!  */
  
! String serviceName=request.getServiceName();
! log.debug("SERVICE NAME: "+serviceName);
! String x=request.getServiceMethodName();
! log.debug("SERVICE METHOD NAME: "+x);
! String y=request.getTarget();
! log.debug("TARGET: "+y);
  
  
! Object r=null;
! String requestedMethodName=request.getServiceMethodName();
  
! String packageFolder="amazon";
  
  
! packageFolder=serviceName.substring(serviceName.lastIndexOf('/')+1, serviceName.lastIndexOf('.'));
! //remove -'s and .'s
! packageFolder=removeChar(packageFolder, '-');
! packageFolder=removeChar(packageFolder, '.');
! log.debug("PACKAGE NAME:" +packageFolder);
  
  
  
! //TODO: something to think about, we dont want to start creating stubs if there is a bad method name
  
! try{
! 		
! 	
! 	 	
! 	String webservicesDir=servletContext.getRealPath("/WEB-INF/classes");
! 	
! 	
! 	File testDir=new File(webservicesDir+"/"+packageFolder);
! 	if(!testDir.exists())
! 	{
! 	
! //		ANT stuff 
! 		Project project = new Project();
! 		project.fireBuildStarted();
! 		project.init();
! 		project.addBuildListener( new ServletLogger( ));
! 		File buildFile = new File(webservicesDir+"/build.xml");
  
! 		log.debug("buildfile exists: "+buildFile.exists());
! 		
! 		project.setProperty("endpoint", serviceName);
! 		project.setProperty("packagename", packageFolder);
! 		
! 		ProjectHelper.configureProject( project, buildFile);
! 		project.executeTarget( project.getDefaultTarget());
! 		project.executeTarget("fetch-wsdl");
! 		project.executeTarget("import-wsdl");
! 		project.executeTarget("compile");
! 	
! 	//end ant stuff	
! 	}
! 		Class bindingClass=null;
! 		Class locatorClass=null;
  
! 		ClassLoader classLoader=null;
  
  
  
! 		if (new File(webservicesDir).exists())
! 		{
! 			classLoader = new URLClassLoader(
! 		 		new URL[]{new File(webservicesDir).toURL()}, Thread.currentThread().getContextClassLoader());
! 	 	}else {
! 		log.debug("installFolder does not exist!");
  		}
! 	File classDir=new File(webservicesDir+"/"+packageFolder+"/");
! 	String []fileList=classDir.list();
! 	
! 	String locatorFileName="";
! 	String bindingFileName="";
  
! 		 for(int i=0;i<fileList.length;++i)
! 			{
! 				//System.out.println(fileList[i]);
! 				String temp=fileList[i];
! 				if(temp.indexOf(".class")==-1)continue;
  
! 				if(temp.indexOf("ServiceLocator")!=-1)locatorFileName=packageFolder+"."+temp;
! 				if(temp.indexOf("BindingStub")!=-1)bindingFileName=packageFolder+"."+temp;
  			}
  
! 	locatorFileName=locatorFileName.substring(0, locatorFileName.lastIndexOf('.'));
! 	bindingFileName=bindingFileName.substring(0, bindingFileName.lastIndexOf('.'));
  
! 	log.debug("locatorFileName="+locatorFileName);
! 	log.debug("bindingFileName="+bindingFileName);
  
! 	locatorClass=classLoader.loadClass(locatorFileName);
! 	bindingClass=classLoader.loadClass(bindingFileName);
  
  
! 		 
! 		
! 		 
! 	Method[] lcMethods=locatorClass.getMethods();
! 	Method getPortAddress=null;
! 	String methodName=null;
! 	for(int i=0;i<lcMethods.length;++i)
! 		{
! 			methodName=lcMethods[i].getName();
! 			if(methodName.indexOf("PortAddress")!=-1)
! 			 {
! 				 log.debug("...............got port address locator..........");
! 				 getPortAddress=lcMethods[i];
! 			 }
! 		 }
! 		
! 	
! 	
! 		Constructor constructor = locatorClass.getConstructor(null);
! 		Object locatorObj=constructor.newInstance(null);
! 				
! 		String endpoint=getPortAddress.invoke(locatorObj,null).toString();
! 	
! 		Class[] argTypes=new Class[]{java.net.URL.class, javax.xml.rpc.Service.class};
! 		Constructor stubConstructor = bindingClass.getConstructor(argTypes);
! 		Service service = null;
! 		Object[] args=new Object[]{new java.net.URL(endpoint), service};
! 	
! 		BeanInfo bi2=Introspector.getBeanInfo(bindingClass);
! 		MethodDescriptor md2[]=bi2.getMethodDescriptors();
! 		Method requestedMethod=null;
! 	
! 		//Class returnType=null;
! 		 for(int i=0;i<md2.length;++i)
! 			 {
! 				 methodName=md2[i].getName();
! 				 if(methodName.equalsIgnoreCase(requestedMethodName))
! 				 {
! 					 System.out.println("...............got "+requestedMethodName+"..........");
! 					 requestedMethod=md2[i].getMethod();
! 				 }
! 			 }
! 	
! 		Object parameterObj=null;
! 		Constructor pc=null;
! 		Class []argTypes2=requestedMethod.getParameterTypes();
! 		System.out.println("Method Name:"+requestedMethod.getName());
! 		System.out.println("REQUESTED METHOD NUM PARMS:" +argTypes2.length);
! 		
! 		for(int j=0;j<argTypes2.length;++j)
! 		{
! 			System.out.println(argTypes2[j].getName());
! 			pc=argTypes2[j].getConstructor(null);
  		}
  
! 		parameterObj=pc.newInstance(null);
! 		List parms=request.getParameters();
! 		
! 		//TODO: is this always valid?
! 		ASObject asobj=(ASObject)parms.get(0);
  
! 		parameterObj=convertHashMapToBean(asobj,parameterObj.getClass());
  
! 		Object stubObj=stubConstructor.newInstance(args);
! 		Object [] p = new Object[] { parameterObj };
! 		
! 	
! 		 //returnType=requestedMethod.getReturnType();
  
! 		r=requestedMethod.invoke(stubObj,p);
! 		System.out.println("here it comes!!");
  
! 	
! 	
! 	 }catch(Exception e)
! 	  {
! 		 e.printStackTrace();
! 	  }
! 	return r;
  	}
! 	public  String removeChar(String s, char c) {
! 	   String r = "";
! 	   for (int i = 0; i < s.length(); i ++) {
! 		  if (s.charAt(i) != c) r += s.charAt(i);
! 		  }
! 	   return r;
! 	   }
  	public boolean supports(ServiceRequest request) {
  		return (
  			request.getServiceName().toLowerCase().lastIndexOf("wsdl") != -1);
  	}
+ 
  
  }




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.