| 1 57 58 package org.apache.wsif.util; 59 60 import java.io.IOException ; 61 import java.io.Reader ; 62 import java.io.StringReader ; 63 import java.io.Writer ; 64 import java.lang.reflect.Method ; 65 import java.net.MalformedURLException ; 66 import java.net.URL ; 67 import java.security.AccessController ; 68 import java.security.PrivilegedAction ; 69 import java.util.ArrayList ; 70 import java.util.HashMap ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 import java.util.Map ; 74 import java.util.Set ; 75 import java.util.StringTokenizer ; 76 77 import javax.wsdl.Binding; 78 import javax.wsdl.BindingOperation; 79 import javax.wsdl.Definition; 80 import javax.wsdl.Import; 81 import javax.wsdl.Message; 82 import javax.wsdl.Operation; 83 import javax.wsdl.Part; 84 import javax.wsdl.PortType; 85 import javax.wsdl.Service; 86 import javax.wsdl.WSDLException; 87 import javax.wsdl.extensions.ExtensibilityElement; 88 import javax.wsdl.extensions.ExtensionRegistry; 89 import javax.wsdl.factory.WSDLFactory; 90 import javax.wsdl.xml.WSDLReader; 91 import javax.wsdl.xml.WSDLWriter; 92 import javax.xml.namespace.QName ; 93 94 import org.apache.wsif.WSIFConstants; 95 import org.apache.wsif.WSIFException; 96 import org.apache.wsif.WSIFService; 97 import org.apache.wsif.base.WSIFServiceImpl; 98 import org.apache.wsif.compiler.schema.tools.Schema2Java; 99 import org.apache.wsif.format.WSIFFormatHandler; 100 import org.apache.wsif.logging.MessageLogger; 101 import org.apache.wsif.logging.Trc; 102 import org.apache.wsif.schema.ComplexType; 103 import org.apache.wsif.schema.ElementType; 104 import org.apache.wsif.schema.Parser; 105 import org.apache.wsif.schema.SequenceElement; 106 import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl; 107 import org.apache.wsif.wsdl.extensions.format.TypeMapping; 108 import org.w3c.dom.Document ; 109 import org.w3c.dom.Element ; 110 import org.xml.sax.InputSource ; 111 112 import com.ibm.wsdl.Constants; 113 import com.ibm.wsdl.PartImpl; 114 115 127 public class WSIFUtils { 128 private static Class initContextClass; 129 private static final String SLASH = "/"; 130 private static final String DOT = "."; 131 private static final String FORMAT_HANDLER = "FormatHandler"; 132 private static final String ELEMENT_FORMAT_HANDLER = "ElementFormatHandler"; 133 private static final String PHYSICALREP = "physicalrep/"; 134 private static final String FORMATBINDING = "formatbinding/"; 135 private static final String XMLSEPARATORS = 136 "\u002D\u002E\u003A\u00B7\u0387\u06DD\u06DE\u30FB"; 137 private static final String XMLSEPARATORS_NODOT = 138 "\u002D\u003A\u00B7\u0387\u06DD\u06DE"; 139 private static final String UNDERSCORE = "_"; 140 private static final String WWW = "www"; 141 private static final String lookupPrefix = "java:comp/env/"; 142 private static final String emptyString = ""; 143 144 private static Boolean providersInitialized = new Boolean (false); 145 private static Boolean simpleTypesMapCreated = new Boolean (false); 146 private static HashMap simpleTypesMap = new HashMap (); 147 private static HashMap keywordMap = null; 148 149 155 public static boolean isJNDIAvailable() { 156 Trc.entry(null); 157 initContextClass = 158 (Class ) AccessController.doPrivileged(new PrivilegedAction () { 159 public Object run() { 160 try { 161 return Class.forName( 162 "javax.naming.InitialContext", 163 true, 164 Thread.currentThread().getContextClassLoader()); 165 } catch (Throwable ignored) { 166 Trc.ignoredException(ignored); 167 } 168 return null; 169 } 170 }); 171 172 boolean b = true; 173 if (initContextClass == null) 174 b = false; 175 Trc.exit(b); 176 return b; 177 } 178 179 183 public static WSIFService lookupFactoryFromJNDI( 184 String serviceNS, 185 String serviceName, 186 String portTypeNS, 187 String portTypeName) 188 throws WSIFException { 189 Trc.entry(null, serviceNS, serviceName, portTypeNS, portTypeName); 190 if (serviceNS == null) 191 throw new IllegalArgumentException ("service namespace can not be null"); 192 if (serviceName == null) 193 throw new IllegalArgumentException ("service name can not be null"); 194 if (portTypeNS == null) 195 throw new IllegalArgumentException ("port type namespace can not be null"); 196 if (portTypeName == null) 197 throw new IllegalArgumentException ("port type name can not be null"); 198 199 WSIFService ws = null; 200 try { 201 if (initContextClass == null) { 202 initContextClass = 203 Class.forName( 204 "javax.naming.InitialContext", 205 true, 206 WSIFUtils.class.getClassLoader()); 207 } 208 Object ic = initContextClass.newInstance(); 209 Class [] lookupSig = new Class [] { String .class }; 210 Object [] lookupArgs = 211 new String [] { 212 serviceNS 213 + "::" 214 + serviceName 215 + "::" 216 + portTypeNS 217 + "::" 218 + portTypeName }; 219 Method m = initContextClass.getMethod("lookup", lookupSig); 220 ws = (WSIFService) m.invoke(ic, lookupArgs); 221 } catch (Exception e) { 222 Trc.exception(e); 223 throw new WSIFException( 224 "Exception while looking up JNDI factory: " + e.getMessage(), 225 e); 226 } 227 Trc.exit(ws); 228 return ws; 229 } 230 231 public static Service selectService( 232 Definition def, 233 String serviceNS, 234 String serviceName) 235 throws WSIFException { 236 Trc.entry(null, def, serviceNS, serviceName); 237 Map services = getAllItems(def, "Service"); 238 QName serviceQName = 239 ((serviceNS != null && serviceName != null) 240 ? new QName (serviceNS, serviceName) 241 : null); 242 Service service = 243 (Service) getNamedItem(services, serviceQName, "Service"); 244 245 Trc.exit(service); 246 return service; 247 } 248 249 public static PortType selectPortType( 250 Definition def, 251 String portTypeNS, 252 String portTypeName) 253 throws WSIFException { 254 Trc.entry(null, def, portTypeNS, portTypeName); 255 Map portTypes = getAllItems(def, "PortType"); 256 QName portTypeQName = 257 ((portTypeNS != null && portTypeName != null) 258 ? new QName (portTypeNS, portTypeName) 259 : null); 260 PortType portType = 261 (PortType) getNamedItem(portTypes, portTypeQName, "PortType"); 262 263 Trc.exit(portType); 264 return portType; 265 } 266 267 public static void addDefinedItems( 268 Map fromItems, 269 String itemType, 270 Map toItems) { 271 Trc.entry(null, fromItems, itemType, toItems); 272 273 if (fromItems != null) { 274 Iterator entryIterator = fromItems.entrySet().iterator(); 275 276 if (itemType.equals("Message")) { 277 while (entryIterator.hasNext()) { 278 Map.Entry entry = (Map.Entry ) entryIterator.next(); 279 Message message = (Message) entry.getValue(); 280 281 if (!message.isUndefined()) { 282 toItems.put(entry.getKey(), message); 283 } 284 } 285 } else if (itemType.equals("Operation")) { 286 while (entryIterator.hasNext()) { 287 Map.Entry entry = (Map.Entry ) entryIterator.next(); 288 Operation operation = (Operation) entry.getValue(); 289 290 if (!operation.isUndefined()) { 291 toItems.put(entry.getKey(), operation); 292 } 293 } 294 } else if (itemType.equals("PortType")) { 295 while (entryIterator.hasNext()) { 296 Map.Entry entry = (Map.Entry ) entryIterator.next(); 297 PortType portType = (PortType) entry.getValue(); 298 299 if (!portType.isUndefined()) { 300 toItems.put(entry.getKey(), portType); 301 } 302 } 303 } else if (itemType.equals("Binding")) { 304 while (entryIterator.hasNext()) { 305 Map.Entry entry = (Map.Entry ) entryIterator.next(); 306 Binding binding = (Binding) entry.getValue(); 307 308 if (!binding.isUndefined()) { 309 toItems.put(entry.getKey(), binding); 310 } 311 } 312 } else if (itemType.equals("Service")) { 313 while (entryIterator.hasNext()) { 314 Map.Entry entry = (Map.Entry ) entryIterator.next(); 315 Service service = (Service) entry.getValue(); 316 317 toItems.put(entry.getKey(), service); 318 } 319 } 320 } 321 Trc.exit(); 322 } 323 324 private static void getAllItems( 325 Definition def, 326 String itemType, 327 Map toItems) { 328 Trc.entry(null, def, itemType, toItems); 329 Map items = null; 330 331 if (itemType.equals("PortType")) { 332 items = def.getPortTypes(); 333 } else if (itemType.equals("Service")) { 334 items = def.getServices(); 335 } else { 336 throw new IllegalArgumentException ( 337 "Don't know how to find all " + itemType + "s."); 338 } 339 340 addDefinedItems(items, itemType, toItems); 341 342 Map imports = def.getImports(); 343 344 if (imports != null) { 345 Iterator valueIterator = imports.values().iterator(); 346 347 while (valueIterator.hasNext()) { 348 List importList = (List ) valueIterator.next(); 349 350 if (importList != null) { 351 Iterator importIterator = importList.iterator(); 352 353 while (importIterator.hasNext()) { 354 Import tempImport = (Import) importIterator.next(); 355 356 if (tempImport != null) { 357 Definition importedDef = tempImport.getDefinition(); 358 359 if (importedDef != null) { 360 getAllItems(importedDef, itemType, toItems); 361 } 362 } 363 } 364 } 365 } 366 } 367 Trc.exit(); 368 } 369 370 public static Map getAllItems(Definition def, String itemType) { 371 Trc.entry(null, def, itemType); 372 Map ret = new HashMap (); 373 374 getAllItems(def, itemType, ret); 375 376 Trc.exit(ret); 377 return ret; 378 } 379 380 public static Object getNamedItem(Map items, QName qname, String itemType) 381 throws WSIFException { 382 Trc.entry(null, items, qname, itemType); 383 if (qname != null) { 384 Object item = items.get(qname); 385 386 if (item != null) { 387 Trc.exit(item); 388 return item; 389 } else { 390 throw new WSIFException( 391 itemType 392 + " '" 393 + qname 394 + "' not found. Choices are: " 395 + getCommaListFromQNameMap(items)); 396 } 397 } else { 398 int size = items.size(); 399 400 if (size == 1) { 401 Iterator valueIterator = items.values().iterator(); 402 403 Object o = valueIterator.next(); 404 Trc.exit(o); 405 return o; 406 } else if (size == 0) { 407 throw new WSIFException( 408 "WSDL document contains no " + itemType + "s."); 409 } else { 410 throw new WSIFException( 411 "Please specify a " 412 + itemType 413 + ". Choices are: " 414 + getCommaListFromQNameMap(items)); 415 } 416 } 417 } 418 419 private static String getCommaListFromQNameMap(Map qnameMap) { 420 StringBuffer strBuf = new StringBuffer ("{"); 421 Set keySet = qnameMap.keySet(); 422 Iterator keyIterator = keySet.iterator(); 423 int index = 0; 424 425 while (keyIterator.hasNext()) { 426 QName key = (QName ) keyIterator.next(); 427 428 strBuf.append((index > 0 ? ", " : "") + key); 429 index++; 430 } 431 432 strBuf.append("}"); 433 434 return strBuf.toString(); 435 } 436 437 442 public static Definition readWSDL(String contextURL, String wsdlLoc) 443 throws WSDLException { 444 Trc.entry(null, contextURL, wsdlLoc); 445 446 initializeProviders(); 447 448 WSDLFactory factory = WSDLFactory.newInstance( 449 WSIFConstants.WSIF_WSDLFACTORY); 450 WSDLReader wsdlReader = factory.newWSDLReader(); 451 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 452 try { 453 Definition def = wsdlReader.readWSDL(contextURL, wsdlLoc); 454 Trc.exitExpandWsdl(def); 455 return def; 456 } catch (WSDLException e) { 457 Trc.exception(e); 458 MessageLogger.log("WSIF.0002E", wsdlLoc); 459 throw e; 460 } 461 } 462 463 469 public static Definition readWSDL( 470 URL documentBase, 471 Reader reader, 472 ClassLoader cl) 473 throws WSDLException { 474 String base = (documentBase == null) ? null : documentBase.toString(); 475 return readWSDL(base, reader, cl); 476 } 477 478 484 public static Definition readWSDL( 485 String documentBase, 486 Reader reader, 487 ClassLoader cl) 488 throws WSDLException { 489 Trc.entry(null, documentBase, reader, cl); 490 491 initializeProviders(); 492 493 WSDLFactory factory = WSDLFactory.newInstance( 494 WSIFConstants.WSIF_WSDLFACTORY); 495 WSDLReader wsdlReader = factory.newWSDLReader(); 496 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 497 WSIFWSDLLocatorImpl lo = null; 498 try { 499 lo = new WSIFWSDLLocatorImpl(documentBase, reader, cl); 500 Definition def = wsdlReader.readWSDL(lo); 501 Trc.exitExpandWsdl(def); 502 return def; 503 } catch (WSDLException e) { 504 Trc.exception(e); 505 MessageLogger.log("WSIF.0002E", documentBase); 506 throw e; 507 } finally { 508 try { 509 if (lo != null) lo.close(); 510 } catch (IOException ioe) { 511 Trc.ignoredException(ioe); 513 } 514 } 515 } 516 517 523 public static Definition readWSDL( 524 URL contextURL, 525 String wsdlLoc, 526 ClassLoader cl) 527 throws WSDLException { 528 Trc.entry(null, contextURL, wsdlLoc, cl); 529 530 initializeProviders(); 531 532 WSDLFactory factory = WSDLFactory.newInstance( 533 WSIFConstants.WSIF_WSDLFACTORY); 534 WSDLReader wsdlReader = factory.newWSDLReader(); 535 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 536 WSIFWSDLLocatorImpl lo = null; 537 538 try { 539 String url = (contextURL == null) ? null : contextURL.toString(); 540 lo = new WSIFWSDLLocatorImpl(url, wsdlLoc, cl); 541 Definition def = wsdlReader.readWSDL(lo); 542 Trc.exitExpandWsdl(def); 543 return def; 544 } catch (WSDLException e) { 545 Trc.exception(e); 546 MessageLogger.log("WSIF.0002E", wsdlLoc); 547 throw e; 548 } finally { 549 try { 550 if (lo != null) lo.close(); 551 } catch (IOException ioe) { 552 Trc.ignoredException(ioe); 554 } 555 } 556 } 557 558 563 public static Definition readWSDL(String contextURL, Reader reader) 564 throws WSDLException { 565 Trc.entry(null, contextURL, reader); 566 567 initializeProviders(); 568 569 WSDLFactory factory = WSDLFactory.newInstance( 570 WSIFConstants.WSIF_WSDLFACTORY); 571 WSDLReader wsdlReader = factory.newWSDLReader(); 572 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 573 Definition def = 574 wsdlReader.readWSDL(contextURL, new InputSource (reader)); 575 Trc.exitExpandWsdl(def); 576 return def; 577 } 578 579 584 public static Definition readWSDL(String contextURL, Document wsdlDocument) 585 throws WSDLException { 586 Trc.entry(null, contextURL, wsdlDocument); 587 588 initializeProviders(); 589 590 WSDLFactory factory = WSDLFactory.newInstance( 591 WSIFConstants.WSIF_WSDLFACTORY); 592 WSDLReader wsdlReader = factory.newWSDLReader(); 593 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 594 Definition def = wsdlReader.readWSDL(contextURL, wsdlDocument); 595 596 Trc.exitExpandWsdl(def); 597 return def; 598 } 599 600 605 public static Definition readWSDL( 606 String contextURL, 607 Element wsdlServicesElement) 608 throws WSDLException { 609 Trc.entry(null, contextURL, wsdlServicesElement); 610 611 initializeProviders(); 612 613 WSDLFactory factory = WSDLFactory.newInstance( 614 WSIFConstants.WSIF_WSDLFACTORY); 615 WSDLReader wsdlReader = factory.newWSDLReader(); 616 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 617 Definition def = wsdlReader.readWSDL(contextURL, wsdlServicesElement); 618 619 Trc.exitExpandWsdl(def); 620 return def; 621 } 622 623 628 public static void writeWSDL(Definition def, Writer sink) 629 throws WSDLException { 630 Trc.entry(null, def, sink); 631 632 WSDLFactory factory = WSDLFactory.newInstance( 633 WSIFConstants.WSIF_WSDLFACTORY); 634 WSDLWriter wsdlWriter = factory.newWSDLWriter(); 635 wsdlWriter.writeWSDL(def, sink); 636 637 Trc.exit(); 638 } 639 640 public static Definition getDefinitionFromLocation( 641 String contextURL, 642 String location) 643 throws WSIFException { 644 Trc.entry(null, contextURL, location); 645 646 if (location == null) { 647 throw new WSIFException("WSDL location must not be null."); 648 } 649 650 Definition def = null; 651 try { 652 def = WSIFUtils.readWSDL(contextURL, location); 653 } catch (WSDLException e) { 654 Trc.exception(e); 655 throw new WSIFException("Problem reading WSDL document.", e); 656 } 657 Trc.exitExpandWsdl(def); 658 return def; 659 } 660 661 public static Definition getDefinitionFromContent( 662 String contextURL, 663 String content) 664 throws WSIFException { 665 Trc.entry(null, contextURL, content); 666 if (content == null) { 667 throw new WSIFException("WSDL content must not be null."); 668 } 669 670 Definition def = null; 671 try { 672 def = WSIFUtils.readWSDL(contextURL, new StringReader (content)); 673 } catch (WSDLException e) { 674 Trc.exception(e); 675 throw new WSIFException("Problem reading WSDL document.", e); 676 } 677 Trc.exitExpandWsdl(def); 678 return def; 679 } 680 681 687 public static void initializeProviders() { 688 synchronized (providersInitialized) { 689 if (!providersInitialized.booleanValue()) { 690 WSIFPluggableProviders.getProvider("/"); 691 providersInitialized = new Boolean (true); 692 } 693 } 694 } 695 696 699 public static void createSimpleTypesMap() { 700 synchronized (simpleTypesMapCreated) { 701 if (!simpleTypesMapCreated.booleanValue()) { 702 new Schema2Java( 703 WSIFConstants.NS_URI_1999_SCHEMA_XSD).getRegistry( 704 simpleTypesMap); 705 new Schema2Java( 706 WSIFConstants.NS_URI_2000_SCHEMA_XSD).getRegistry( 707 simpleTypesMap); 708 new Schema2Java( 709 WSIFConstants.NS_URI_2001_SCHEMA_XSD).getRegistry( 710 simpleTypesMap); 711 simpleTypesMapCreated = new Boolean (true); 712 } 713 } 714 } 715 716 720 public static Map getSimpleTypesMap() { 721 if (!simpleTypesMapCreated.booleanValue()) { 722 createSimpleTypesMap(); 723 } 724 return simpleTypesMap; 725 } 726 727 public static WSIFFormatHandler getFormatHandler( 729 Part part, 730 Definition definition, 731 javax.wsdl.Binding binding) 732 throws 733 java.lang.InstantiationException , 734 java.lang.IllegalAccessException , 735 java.lang.ClassNotFoundException { 736 Trc.entry(null, part, definition, binding); 737 WSIFFormatHandler formatHandler = null; 738 javax.xml.namespace.QName partTypeQName = part.getTypeName(); 739 if (partTypeQName == null) 740 partTypeQName = part.getElementName(); 741 if (partTypeQName == null) 742 throw new ClassNotFoundException (part.getName()); 743 744 String typePackageName = 745 getPackageNameFromNamespaceURI(partTypeQName.getNamespaceURI()); 746 String formatHandlerName = typePackageName; 747 748 String bindingShortName = 749 getPackageNameFromXMLName( 750 definition.getPrefix(getBindingNamespace(binding))); 751 if (bindingShortName != null)
|