PageRenderTime 81ms CodeModel.GetById 29ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/web/DRCP/src/org/wix/wsCaller/DynamicInvoker.java

http://drcp.googlecode.com/
Java | 255 lines | 224 code | 28 blank | 3 comment | 28 complexity | a96b714f0af2bcb6ce5086f03a966880 MD5 | raw file
  1package org.wix.wsCaller;
  2
  3import java.util.HashMap;
  4import java.util.Iterator;
  5import java.util.Map;
  6import java.util.Set;
  7import java.util.Vector;
  8
  9import javax.wsdl.Binding;
 10import javax.wsdl.Operation;
 11import javax.wsdl.Port;
 12import javax.wsdl.Service;
 13import javax.xml.namespace.QName;
 14import javax.xml.rpc.Call;
 15import javax.xml.rpc.encoding.Deserializer;
 16import javax.xml.rpc.encoding.DeserializerFactory;
 17
 18import org.apache.axis.Constants;
 19import org.apache.axis.encoding.ser.SimpleDeserializer;
 20import org.apache.axis.wsdl.gen.Parser;
 21import org.apache.axis.wsdl.symbolTable.BaseType;
 22import org.apache.axis.wsdl.symbolTable.BindingEntry;
 23import org.apache.axis.wsdl.symbolTable.Parameter;
 24import org.apache.axis.wsdl.symbolTable.Parameters;
 25import org.apache.axis.wsdl.symbolTable.ServiceEntry;
 26import org.apache.axis.wsdl.symbolTable.SymTabEntry;
 27import org.apache.axis.wsdl.symbolTable.SymbolTable;
 28import org.apache.axis.wsdl.symbolTable.TypeEntry;
 29
 30// Web Service Dynamic Invoker
 31public class DynamicInvoker {
 32	private Parser wsdlParser = null;
 33
 34	private Map services = null;
 35
 36	public DynamicInvoker(String wsdlURL) throws Exception {
 37		wsdlParser = new Parser();
 38		wsdlParser.run(wsdlURL);
 39		services = enumSymTabEntry(ServiceEntry.class);
 40	}
 41
 42	public Map invoke(String serviceName, String portName,
 43			String operationName, Vector parameterValues) throws Exception {
 44		Vector inputs = new Vector();
 45		String returnName = null;
 46
 47		ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
 48		Service service = serviceEntry.getService();
 49		org.apache.axis.client.Service clientService = new org.apache.axis.client.Service(
 50				wsdlParser, service.getQName());
 51
 52		Call call = clientService.createCall(QName.valueOf(portName), QName
 53				.valueOf(operationName));
 54		((org.apache.axis.client.Call) call).setTimeout(new Integer(15 * 1000));
 55
 56		BindingEntry bindingEntry = getBindingEntry(serviceName, portName);
 57		Operation o = getOperation(bindingEntry, operationName);
 58		Parameters parameters = bindingEntry.getParameters(o);
 59
 60		if (parameters.returnParam != null) {
 61			QName returnType = org.apache.axis.wsdl.toJava.Utils
 62					.getXSIType(parameters.returnParam);
 63			QName returnQName = parameters.returnParam.getQName();
 64			returnName = returnQName.getLocalPart();
 65		}
 66
 67		int size = parameters.list.size();
 68		for (int i = 0; i < size; i++) {
 69			Parameter p = (Parameter) parameters.list.get(i);
 70			switch (p.getMode()) {
 71			case Parameter.IN:
 72				inputs.add(getParamData((org.apache.axis.client.Call) call, p,
 73						(String) parameterValues.elementAt(i)));
 74				break;
 75			case Parameter.OUT:
 76				break;
 77			case Parameter.INOUT:
 78				inputs.add(getParamData((org.apache.axis.client.Call) call, p,
 79						(String) parameterValues.elementAt(i)));
 80				break;
 81			}
 82		}
 83
 84		Object ret = call.invoke(inputs.toArray());
 85		Map outputs = call.getOutputParams();
 86		HashMap map = new HashMap();
 87		if (ret != null && returnName != null) {
 88			map.put(returnName, ret);
 89		}
 90		if (outputs != null) {
 91			for (Iterator i = outputs.keySet().iterator(); i.hasNext();) {
 92				Object obj = i.next();
 93				String name;
 94				Object value;
 95				if (obj.getClass().getName().equals("java.lang.String")) {
 96					name = (String) obj;
 97				} else {
 98					name = ((QName) obj).getLocalPart();
 99				}
100				value = outputs.get(obj);
101				map.put(name, value);
102			}
103		}
104		return map;
105	}
106
107	public Vector enumServiceNames() {
108		Vector v = new Vector();
109		Iterator i = services.keySet().iterator();
110		while (i.hasNext()) {
111			String s = (String) i.next();
112			v.addElement(s);
113		}
114		return v;
115	}
116
117	public Vector enumPortNames(String serviceName) {
118		Vector v = new Vector();
119		ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
120		Map ports = serviceEntry.getService().getPorts();
121		Iterator i = ports.keySet().iterator();
122		while (i.hasNext()) {
123			String s = (String) i.next();
124			v.addElement(s);
125		}
126		return v;
127	}
128
129	public Vector enumOperationNames(String serviceName, String portName) {
130		Vector v = new Vector();
131		BindingEntry entry = getBindingEntry(serviceName, portName);
132		Set operations = entry.getOperations();
133		Iterator i = operations.iterator();
134		while (i.hasNext()) {
135			Operation o = (Operation) i.next();
136			String s = o.getName();
137			v.addElement(s);
138		}
139		return v;
140	}
141
142	public Parameters enumParameters(String serviceName, String portName,
143			String operationName) {
144		BindingEntry entry = getBindingEntry(serviceName, portName);
145		Operation o = getOperation(entry, operationName);
146		Parameters parameters = entry.getParameters(o);
147		return parameters;
148	}
149
150	public String getParameterModeString(Parameter p) {
151		String ret = null;
152		switch (p.getMode()) {
153		case Parameter.IN:
154			ret = "[IN]";
155			break;
156		case Parameter.INOUT:
157			ret = "[IN, OUT]";
158			break;
159		case Parameter.OUT:
160			ret = "[OUT]";
161			break;
162		}
163		return ret;
164	}
165
166	private BindingEntry getBindingEntry(String serviceName, String portName) {
167		ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
168		Port port = serviceEntry.getService().getPort(portName);
169		Binding binding = port.getBinding();
170		SymbolTable table = wsdlParser.getSymbolTable();
171		return table.getBindingEntry(binding.getQName());
172	}
173
174	private Operation getOperation(BindingEntry entry, String operationName) {
175		Set operations = entry.getOperations();
176		Iterator i = operations.iterator();
177		while (i.hasNext()) {
178			Operation o = (Operation) i.next();
179			if (operationName.equals(o.getName())) {
180				return o;
181			}
182		}
183		return null;
184	}
185
186	// return Map of <QName.getLocalPart, SymTabEntry>
187	private Map enumSymTabEntry(Class cls) {
188		HashMap ret = new HashMap();
189		HashMap map = wsdlParser.getSymbolTable().getHashMap();
190		Iterator iterator = map.entrySet().iterator();
191		while (iterator.hasNext()) {
192			Map.Entry entry = (Map.Entry) iterator.next();
193			QName key = (QName) entry.getKey();
194			Vector v = (Vector) entry.getValue();
195			int size = v.size();
196			for (int i = 0; i < size; ++i) {
197				SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
198				if (cls.isInstance(symTabEntry)) {
199					ret.put(key.getLocalPart(), symTabEntry);
200				}
201			}
202		}
203		return ret;
204	}
205
206	private Object getParamData(org.apache.axis.client.Call c, Parameter p,
207			String arg) throws Exception {
208		// Get the QName representing the parameter type
209		QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
210
211		TypeEntry type = p.getType();
212		if (type instanceof BaseType && ((BaseType) type).isBaseType()) {
213			DeserializerFactory factory = c.getTypeMapping().getDeserializer(
214					paramType);
215			Deserializer deserializer = factory
216					.getDeserializerAs(Constants.AXIS_SAX);
217			if (deserializer instanceof SimpleDeserializer) {
218				return ((SimpleDeserializer) deserializer).makeValue(arg);
219			}
220		}
221		throw new RuntimeException("not know how to convert '" + arg
222				+ "' into " + c);
223	}
224
225	public static void main(String[] args) {
226		String location = "http://mercuryhz/ims/ims.asmx?wsdl";
227		try {
228			DynamicInvoker invoker = new DynamicInvoker(location);
229			Vector paramList = new Vector();
230			paramList.add(new String("hz00260"));
231			paramList.add(new String("UTs54121b"));
232			
233			Map returnList = invoker.invoke("ImsWebService","ImsWebServiceSoap","UTADAuthentication",paramList);
234			for(Iterator i = returnList.keySet().iterator();i.hasNext();)
235			{
236				String name = (String) i.next();
237				Object value = returnList.get(name);
238				String line;
239				if (value != null)
240				{
241					line = name + "=" + returnList.get(name).toString() + "\n";
242				}
243				else
244				{
245					line = name + "=(null)\n";
246				}
247				System.out.println(line);
248				
249			}
250			
251		} catch (Exception e) {
252		}
253
254	}
255}