1 package org.jbpm.bpel.wsdl.util; 2 3 import java.io.*; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import javax.wsdl.Definition; 9 import javax.wsdl.Message; 10 import javax.wsdl.Part; 11 import javax.wsdl.WSDLException; 12 import javax.wsdl.extensions.ExtensibilityElement; 13 import javax.wsdl.factory.WSDLFactory; 14 import javax.xml.namespace.QName ; 15 16 import org.apache.commons.collections.Predicate; 17 import org.apache.commons.collections.iterators.EmptyIterator; 18 import org.apache.commons.collections.iterators.FilterIterator; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.xml.sax.InputSource ; 22 23 import org.jbpm.bpel.def.Import; 24 import org.jbpm.bpel.def.Import.Type; 25 import org.jbpm.bpel.wsdl.def.PartnerLinkType; 26 import org.jbpm.bpel.wsdl.def.Property; 27 import org.jbpm.bpel.wsdl.xml.WsdlFactoryImpl; 28 29 34 public class WsdlUtil { 35 36 private static final WSDLFactory factory = new WsdlFactoryImpl(); 37 private static final Log log = LogFactory.getLog(WsdlUtil.class); 38 39 40 private WsdlUtil() { 41 } 42 43 public static WSDLFactory getFactory() { 44 return factory; 45 } 46 47 public static Definition readText(String text) throws WSDLException { 48 return getFactory().newWSDLReader().readWSDL(null, new InputSource (new StringReader(text))); 49 } 50 51 public static void writeFile(Definition def, File file) throws WSDLException { 52 OutputStream sink = null; 53 try { 54 sink = new FileOutputStream(file); 56 Writer writer; 57 try { 58 writer = new OutputStreamWriter(sink, "UTF-8"); 60 } 61 catch (UnsupportedEncodingException e) { 62 log.warn("UTF-8 encoding not supported", e); 63 writer = new OutputStreamWriter(sink); 65 } 66 getFactory().newWSDLWriter().writeWSDL(def, new BufferedWriter(writer)); 68 } 69 catch (FileNotFoundException e) { 70 log.error(e); 71 throw new WSDLException(WSDLException.OTHER_ERROR, 72 "could not open file for writing: " + file, e); 73 } 74 finally { 75 if (sink != null) { 76 try { 77 sink.close(); 78 } 79 catch (IOException e) { 80 log.warn("could not close file: " + file, e); 81 } 82 } 83 } 84 } 85 86 public static PartnerLinkType getPartnerLinkType(Definition def, QName name) { 87 List extensions = def.getExtensibilityElements(); 89 for (int i = 0, n = extensions.size(); i < n; i++) { 90 Object extension = extensions.get(i); 91 if (extension instanceof PartnerLinkType) { 92 PartnerLinkType plinkType = (PartnerLinkType) extension; 93 if (plinkType.getQName().equals(name)) return plinkType; 94 } 95 } 96 List imports = def.getImports(name.getNamespaceURI()); 98 if (imports != null) { 99 for (int i = 0, n = imports.size(); i < n; i++) { 100 javax.wsdl.Import imp = (javax.wsdl.Import) imports.get(i); 101 PartnerLinkType plinkType = getPartnerLinkType(imp.getDefinition(), name); 102 if (plinkType != null) return plinkType; 103 } 104 } 105 return null; 106 } 107 108 public static Property getProperty(Definition def, QName name) { 109 List extensions = def.getExtensibilityElements(); 111 for (int i = 0, n = extensions.size(); i < n; i++) { 112 Object extension = extensions.get(i); 113 if (extension instanceof Property) { 114 Property property = (Property) extension; 115 if (property.getQName().equals(name)) return property; 116 } 117 } 118 List imports = def.getImports(name.getNamespaceURI()); 120 if (imports != null) { 121 for (int i = 0, n = imports.size(); i < n; i++) { 122 javax.wsdl.Import imp = (javax.wsdl.Import) imports.get(i); 123 Property property = getProperty(imp.getDefinition(), name); 124 if (property != null) return property; 125 } 126 } 127 return null; 128 } 129 130 public static ExtensibilityElement getExtension(List extensions, QName extensionType) { 131 if (extensions != null) { 132 Iterator extensionIt = extensions.iterator(); 133 while (extensionIt.hasNext()) { 134 ExtensibilityElement extension = (ExtensibilityElement) extensionIt.next(); 135 if (ExtensionTypePredicate.evaluate(extension, extensionType)) { 136 return extension; 137 } 138 } 139 } 140 return null; 141 } 142 143 public static Iterator getExtensions(List extensions, QName extensionType) { 144 return extensions != null ? 145 new FilterIterator(extensions.iterator(), new ExtensionTypePredicate(extensionType)) 146 : EmptyIterator.INSTANCE; 147 } 148 149 public static QName getDocLitElementName(Message message) { 150 Map parts = message.getParts(); 151 return parts.size() == 1 ? ((Part) parts.values().iterator().next()).getElementName() : null; 152 } 153 154 public static Import createImport(Definition def) { 155 Import imp = new Import(); 156 imp.setNamespace(def.getTargetNamespace()); 157 imp.setType(Type.WSDL); 158 imp.setDocument(def); 159 return imp; 160 } 161 162 private static class ExtensionTypePredicate implements Predicate { 163 164 private final QName extensionType; 165 166 ExtensionTypePredicate(QName type) { 167 extensionType = type; 168 } 169 170 public boolean evaluate(Object arg) { 171 return evaluate((ExtensibilityElement) arg, extensionType); 172 } 173 174 public static boolean evaluate(ExtensibilityElement extension, QName extensionType) { 175 return extension.getElementType().equals(extensionType); 176 } 177 } 178 } 179 | Popular Tags |