1 7 8 package org.jboss.webservice.util; 10 11 13 import org.jboss.logging.Logger; 14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping; 15 import org.jboss.webservice.metadata.jaxrpcmapping.ServiceEndpointInterfaceMapping; 16 import org.jboss.webservice.metadata.jaxrpcmapping.ServiceEndpointMethodMapping; 17 18 import javax.wsdl.Definition; 19 import javax.wsdl.Import; 20 import javax.wsdl.Operation; 21 import javax.wsdl.Port; 22 import javax.wsdl.PortType; 23 import javax.wsdl.Service; 24 import javax.wsdl.extensions.soap.SOAPAddress; 25 import javax.xml.rpc.ServiceException ; 26 import java.lang.reflect.Method ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 39 public final class WSDLUtilities 40 { 41 private static final Logger log = Logger.getLogger(WSDLUtilities.class); 43 44 public static final String WSDL_NAMESPACE_URI = "http://schemas.xmlsoap.org/wsdl/".intern(); 45 public static final String XSD_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema".intern(); 46 47 private WSDLUtilities() 49 { 50 } 51 52 55 public static void endorseServiceEndpointInterface(Definition wsdlDefinition, Class seiClass, JavaWsdlMapping jaxrpcMapping) 56 throws ServiceException 57 { 58 if (wsdlDefinition == null) 59 throw new IllegalArgumentException ("WSDL definition cannot be null"); 60 61 if (seiClass == null) 62 throw new IllegalArgumentException ("Service endpoint interface cannot be null"); 63 64 if (jaxrpcMapping == null) 65 throw new IllegalArgumentException ("JavaWsdlMapping cannot be null"); 66 67 if (java.rmi.Remote .class.isAssignableFrom(seiClass) == false) 68 throw new IllegalArgumentException ("The service endpoint interface does not implement java.rmi.Remote: " + seiClass.getName()); 69 70 List seiMethodNames = new ArrayList (); 72 Method [] seiMethods = seiClass.getDeclaredMethods(); 73 for (int i = 0; i < seiMethods.length; i++) 74 { 75 Method method = seiMethods[i]; 76 seiMethodNames.add(method.getName()); 77 } 78 79 Map semMap = new HashMap (); 81 ServiceEndpointInterfaceMapping[] seiMappings = jaxrpcMapping.getServiceEndpointInterfaceMappings(); 82 for (int i = 0; i < seiMappings.length; i++) 83 { 84 ServiceEndpointInterfaceMapping seiMapping = seiMappings[i]; 85 if (seiClass.getName().equals(seiMapping.getServiceEndpointInterface())) 86 { 87 ServiceEndpointMethodMapping[] semMappings = seiMapping.getServiceEndpointMethodMappings(); 88 for (int j = 0; j < semMappings.length; j++) 89 { 90 ServiceEndpointMethodMapping semMapping = semMappings[j]; 91 semMap.put(semMapping.getWsdlOperation(), semMapping.getJavaMethodName()); 92 } 93 } 94 } 95 96 Map portTypeOperationsMap = new HashMap (); 98 99 Iterator itPortType = getPortTypesWithImport(wsdlDefinition).iterator(); 101 while (itPortType.hasNext()) 102 { 103 List ptMethods = new ArrayList (); 104 PortType portType = (PortType)itPortType.next(); 105 Iterator itop = portType.getOperations().iterator(); 106 while (itop.hasNext()) 107 { 108 String wsdlOp = ((Operation)itop.next()).getName(); 109 String javaOp = (String )semMap.get(wsdlOp); 110 ptMethods.add(javaOp != null ? javaOp : wsdlOp); 111 } 112 portTypeOperationsMap.put(portType.getQName(), ptMethods); 113 } 114 115 boolean portTypeFound = false; 116 117 Iterator it = portTypeOperationsMap.values().iterator(); 118 while (it.hasNext()) 119 { 120 boolean seiOpInPortType = true; 121 List opList = (List )it.next(); 122 for (int i = 0; i < seiMethods.length; i++) 123 { 124 Method seiMethod = seiMethods[i]; 125 seiOpInPortType &= opList.contains(seiMethod.getName()); 126 } 127 128 if (seiOpInPortType == true) 130 { 131 portTypeFound = true; 132 break; 133 } 134 } 135 136 if (portTypeFound == false) 137 throw new ServiceException ("Cannot find SEI operations " + seiMethodNames + " in any wsdl port type, we have " + portTypeOperationsMap); 138 } 139 140 143 private static List getPortTypesWithImport(Definition wsdl) 144 { 145 ArrayList pTypes = new ArrayList (wsdl.getPortTypes().values()); 146 Iterator it = wsdl.getImports().values().iterator(); 147 while (it.hasNext()) 148 { 149 List imports = (List )it.next(); 150 for (int i = 0; i < imports.size(); i++) 151 { 152 Import imp = (Import)imports.get(i); 153 Definition subdef = imp.getDefinition(); 154 pTypes.addAll(subdef.getPortTypes().values()); 155 pTypes.addAll(getPortTypesWithImport(subdef)); 156 } 157 } 158 return pTypes; 159 } 160 161 164 public static String [] getServiceLocations(Definition wsdlDefinition) 165 { 166 ArrayList locations = new ArrayList (); 167 168 Service wsdlService = (Service)wsdlDefinition.getServices().values().iterator().next(); 169 170 Map wsdlPorts = wsdlService.getPorts(); 171 Iterator it = wsdlPorts.values().iterator(); 172 while (it.hasNext()) 173 { 174 Port wsdlPort = (Port)it.next(); 175 Iterator itElements = wsdlPort.getExtensibilityElements().iterator(); 176 while (itElements.hasNext()) 177 { 178 Object obj = itElements.next(); 179 if (obj instanceof SOAPAddress) 180 { 181 SOAPAddress address = (SOAPAddress)obj; 182 String wsdlURI = address.getLocationURI(); 183 locations.add(wsdlURI); 184 } 185 } 186 } 187 188 String [] arr = new String [locations.size()]; 189 locations.toArray(arr); 190 return arr; 191 } 192 } | Popular Tags |