1 57 58 package clients; 59 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Map ; 64 import java.util.StringTokenizer ; 65 66 import javax.wsdl.Definition; 67 import javax.wsdl.Input; 68 import javax.wsdl.Operation; 69 import javax.wsdl.Output; 70 import javax.wsdl.Part; 71 import javax.wsdl.Port; 72 import javax.wsdl.PortType; 73 import javax.wsdl.Service; 74 import javax.xml.namespace.QName ; 75 76 import org.apache.wsif.WSIFException; 77 import org.apache.wsif.WSIFMessage; 78 import org.apache.wsif.WSIFOperation; 79 import org.apache.wsif.WSIFPort; 80 import org.apache.wsif.WSIFService; 81 import org.apache.wsif.WSIFServiceFactory; 82 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis; 83 import org.apache.wsif.util.WSIFPluggableProviders; 84 import org.apache.wsif.util.WSIFUtils; 85 86 95 96 public class DynamicInvoker { 97 private static void usage() { 98 System.err.println( 99 "Usage: java " 100 + DynamicInvoker.class.getName() 101 + " wsdlLocation " 102 + "operationName[(portName)]:[inputMessageName]:[outputMessageName] " 103 + "[soap|axis] [argument1 ...]"); 104 System.exit(1); 105 } 106 107 public static void main(String [] args) throws Exception { 108 if (args.length < 2) 109 usage(); 110 111 String wsdlLocation = args.length > 0 ? args[0] : null; 112 String operationKey = args.length > 1 ? args[1] : null; 113 String portName = null; 114 String operationName = null; 115 String inputName = null; 116 String outputName = null; 117 118 StringTokenizer st = new StringTokenizer (operationKey, ":"); 119 int tokens = st.countTokens(); 120 int specType = 0; 121 if (tokens == 2) { 122 specType = operationKey.endsWith(":") ? 1 : 2; 123 } else if (tokens != 1 && tokens != 3) 124 usage(); 125 126 while (st.hasMoreTokens()) { 127 if (operationName == null) 128 operationName = st.nextToken(); 129 else if (inputName == null && specType != 2) 130 inputName = st.nextToken(); 131 else if (outputName == null) 132 outputName = st.nextToken(); 133 else 134 break; 135 } 136 137 try { 138 portName = 139 operationName.substring(operationName.indexOf("(")+1, operationName.indexOf(")")); 140 operationName = operationName.substring(0, operationName.indexOf("(")); 141 } catch (Exception ignored) { 142 } 143 144 String protocol = args.length > 2 ? args[2] : ""; 145 int shift = 2; 146 if (protocol.equals("soap") || protocol.equals("axis")) 147 shift = 3; 148 149 HashMap map = 150 invokeMethod( 151 wsdlLocation, 152 operationName, 153 inputName, 154 outputName, 155 portName, 156 protocol, 157 args, 158 shift); 159 160 System.out.println("Result:"); 162 for (Iterator it = map.keySet().iterator(); it.hasNext();) { 163 String name = (String ) it.next(); 164 System.out.println(name + "=" + map.get(name)); 165 } 166 System.out.println("\nDone!"); 167 168 } 169 170 public static HashMap invokeMethod( 171 String wsdlLocation, 172 String operationName, 173 String inputName, 174 String outputName, 175 String portName, 176 String protocol, 177 String [] args, 178 int argShift) 179 throws Exception { 180 181 String serviceNS = null; 182 String serviceName = null; 183 String portTypeNS = null; 184 String portTypeName = null; 185 186 if ("axis".equals(protocol)) { 189 WSIFPluggableProviders.overrideDefaultProvider( 190 "http://schemas.xmlsoap.org/wsdl/soap/", 191 new WSIFDynamicProvider_ApacheAxis()); 192 } 193 194 System.out.println("Reading WSDL document from '" + wsdlLocation + "'"); 195 Definition def = WSIFUtils.readWSDL(null, wsdlLocation); 196 197 System.out.println("Preparing WSIF dynamic invocation"); 198 199 Service service = WSIFUtils.selectService(def, serviceNS, serviceName); 200 201 Map portTypes = WSIFUtils.getAllItems(def, "PortType"); 202 if (portTypes.size() > 1 && portName != null) { 205 for (Iterator i=portTypes.keySet().iterator(); i.hasNext(); ) { 206 QName qn = (QName ) i.next(); 207 if (portName.equals(qn.getLocalPart())) { 208 portTypeName = qn.getLocalPart(); 209 portTypeNS = qn.getNamespaceURI(); 210 break; 211 } 212 } 213 } 214 PortType portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName); 215 216 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 217 WSIFService dpf = factory.getService(def, service, portType); 218 WSIFPort port = null; 219 if (portName == null) 220 port = dpf.getPort(); 221 else 222 port = dpf.getPort(portName); 223 224 if (inputName == null && outputName == null) { 225 List operationList = portType.getOperations(); 227 228 boolean found = false; 230 for (Iterator i = operationList.iterator(); i.hasNext();) { 231 Operation op = (Operation) i.next(); 232 String name = op.getName(); 233 if (!name.equals(operationName)) { 234 continue; 235 } 236 if (found) { 237 throw new RuntimeException ( 238 "Operation '" 239 + operationName 240 + "' is overloaded. " 241 + "Please specify the operation in the form " 242 + "'operationName:inputMessageName:outputMesssageName' to distinguish it"); 243 } 244 found = true; 245 Input opInput = op.getInput(); 246 inputName = (opInput.getName() == null) ? null : opInput.getName(); 247 Output opOutput = op.getOutput(); 248 outputName = (opOutput.getName() == null) ? null : opOutput.getName(); 249 } 250 } 251 252 WSIFOperation operation = 253 port.createOperation(operationName, inputName, outputName); 254 WSIFMessage input = operation.createInputMessage(); 255 WSIFMessage output = operation.createOutputMessage(); 256 WSIFMessage fault = operation.createFaultMessage(); 257 258 List operationList = portType.getOperations(); 260 261 boolean found = false; 263 String [] outNames = new String [0]; 264 Class [] outTypes = new Class [0]; 265 for (Iterator i = operationList.iterator(); i.hasNext();) { 266 Operation op = (Operation) i.next(); 267 String name = op.getName(); 268 if (!name.equals(operationName)) { 269 continue; 270 } 271 if (found) { 272 throw new RuntimeException ("overloaded operations are not supported in this sample"); 273 } 274 found = true; 275 276 Input opInput = op.getInput(); 278 279 String [] inNames = new String [0]; 281 Class [] inTypes = new Class [0]; 282 if (opInput != null) { 283 List parts = opInput.getMessage().getOrderedParts(null); 284 unWrapIfWrappedDocLit(parts, name, def); 285 int count = parts.size(); 286 inNames = new String [count]; 287 inTypes = new Class [count]; 288 retrieveSignature(parts, inNames, inTypes); 289 } 290 292 for (int pos = 0; pos < inNames.length; ++pos) { 293 String arg = args[pos + argShift]; 294 Object value = null; 295 Class c = inTypes[pos]; 296 if (c.equals(String .class)) { 297 value = arg; 298 } else if (c.equals(Double.TYPE)) { 299 value = new Double (arg); 300 } else if (c.equals(Float.TYPE)) { 301 value = new Float (arg); 302 } else if (c.equals(Integer.TYPE)) { 303 value = new Integer (arg); 304 } else if (c.equals(Boolean.TYPE)) { 305 value = new Boolean (arg); 306 } else { 307 throw new RuntimeException ("not know how to convert '" + arg + "' into " + c); 308 } 309 310 input.setObjectPart(inNames[pos], value); 311 } 312 313 Output opOutput = op.getOutput(); 314 if (opOutput != null) { 315 List parts = opOutput.getMessage().getOrderedParts(null); 316 unWrapIfWrappedDocLit(parts, name+"Response", def); 317 int count = parts.size(); 318 outNames = new String [count]; 319 outTypes = new Class [count]; 320 retrieveSignature(parts, outNames, outTypes); 321 } 322 323 } 324 if (!found) { 325 throw new RuntimeException ( 326 "no operation " 327 + operationName 328 + " was found in port type " 329 + portType.getQName()); 330 } 331 332 System.out.println("Executing operation " + operationName); 333 operation.executeRequestResponseOperation(input, output, fault); 334 335 HashMap map = new HashMap (); 336 for (int pos = 0; pos < outNames.length; ++pos) { 337 String name = outNames[pos]; 338 map.put(name, output.getObjectPart(name)); 339 } 340 341 return map; 342 } 343 344 private static void retrieveSignature( 345 List parts, 346 String [] names, 347 Class [] types) { 348 for (int i = 0; i < names.length; ++i) { 350 Part part = (Part) parts.get(i); 351 names[i] = part.getName(); 352 QName partType = part.getTypeName(); 353 if (partType == null) { 354 partType = part.getElementName(); 355 } 356 if (partType == null) { 357 throw new RuntimeException ( 358 "part " + names[i] + " must have type name declared"); 359 } 360 String s = partType.getLocalPart(); 363 if ("string".equals(s)) { 364 types[i] = String .class; 365 } else if ("double".equals(s)) { 366 types[i] = Integer.TYPE; 367 } else if ("float".equals(s)) { 368 types[i] = Float.TYPE; 369 } else if ("int".equals(s)) { 370 types[i] = Integer.TYPE; 371 } else if ("boolean".equals(s)) { 372 types[i] = Boolean.TYPE; 373 } else { 374 throw new RuntimeException ( 375 "part type " + partType + " not supported in this sample"); 376 } 377 } 378 } 379 380 383 private static void unWrapIfWrappedDocLit(List parts, String operationName, Definition def) throws WSIFException { 384 Part p = WSIFUtils.getWrappedDocLiteralPart(parts, operationName); 385 if (p != null) { 386 List unWrappedParts = WSIFUtils.unWrapPart(p, def); 387 parts.remove(p); 388 parts.addAll(unWrappedParts); 389 } 390 } 391 392 } 393
| Popular Tags
|