1 7 package org.jboss.webservice.metadata; 8 9 11 import org.jboss.logging.Logger; 12 import org.jboss.webservice.ServiceDeployer; 13 import org.jboss.webservice.metadata.wsdl.WSDL11DefinitionFactory; 14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping; 15 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMappingFactory; 16 17 import javax.wsdl.Definition; 18 import javax.wsdl.Port; 19 import javax.wsdl.Service; 20 import javax.wsdl.WSDLException; 21 import javax.wsdl.extensions.soap.SOAPAddress; 22 import javax.xml.rpc.JAXRPCException ; 23 import java.net.MalformedURLException ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 38 public class WebserviceDescriptionMetaData 39 { 40 private static final Logger log = Logger.getLogger(WebserviceDescriptionMetaData.class); 42 43 private WebservicesMetaData webservices; 45 46 private String webserviceDescriptionName; 48 private String wsdlFile; 50 private String jaxrpcMappingFile; 52 private ArrayList portComponents = new ArrayList (); 54 55 private String wsdlPublishLocation; 57 58 public WebserviceDescriptionMetaData(WebservicesMetaData webservices) 59 { 60 this.webservices = webservices; 61 } 62 63 public WebservicesMetaData getWebservices() 64 { 65 return webservices; 66 } 67 68 public void addPortComponent(PortComponentMetaData portComponent) 69 { 70 portComponents.add(portComponent); 71 } 72 73 public PortComponentMetaData[] getPortComponents() 74 { 75 PortComponentMetaData[] array = new PortComponentMetaData[portComponents.size()]; 76 portComponents.toArray(array); 77 return array; 78 } 79 80 86 public PortComponentMetaData getPortComponentByWsdlPort(String name) 87 { 88 ArrayList pcNames = new ArrayList (); 89 Iterator it = portComponents.iterator(); 90 while (it.hasNext()) 91 { 92 PortComponentMetaData pc = (PortComponentMetaData)it.next(); 93 String wsdlPortName = pc.getWsdlPort().getLocalPart(); 94 if (wsdlPortName.equals(name)) 95 { 96 return pc; 97 } 98 pcNames.add(wsdlPortName); 99 } 100 101 log.error("Cannot get port component name '" + name + "', we have: " + pcNames); 102 return null; 103 } 104 105 111 public PortComponentMetaData getPortComponentByName(String name) 112 { 113 ArrayList pcNames = new ArrayList (); 114 Iterator it = portComponents.iterator(); 115 while (it.hasNext()) 116 { 117 PortComponentMetaData pc = (PortComponentMetaData)it.next(); 118 String portName = pc.getPortComponentName(); 119 if (portName.equals(name)) 120 { 121 return pc; 122 } 123 pcNames.add(portName); 124 } 125 126 log.error("Cannot get port component name '" + name + "', we have: " + pcNames); 127 return null; 128 } 129 130 public String getWebserviceDescriptionName() 131 { 132 return webserviceDescriptionName; 133 } 134 135 public void setWebserviceDescriptionName(String webserviceDescriptionName) 136 { 137 this.webserviceDescriptionName = webserviceDescriptionName; 138 } 139 140 public String getWsdlFile() 141 { 142 return wsdlFile; 143 } 144 145 public void setWsdlFile(String wsdlFile) 146 { 147 this.wsdlFile = wsdlFile; 148 } 149 150 public String getWsdlPublishLocation() 151 { 152 return wsdlPublishLocation; 153 } 154 155 public void setWsdlPublishLocation(String wsdlPublishLocation) 156 { 157 this.wsdlPublishLocation = wsdlPublishLocation; 158 } 159 160 public String getJaxrpcMappingFile() 161 { 162 return jaxrpcMappingFile; 163 } 164 165 public JavaWsdlMapping getJavaWsdlMapping() 166 { 167 JavaWsdlMapping javaWsdlMapping = (JavaWsdlMapping)webservices.jaxrpcMappingFileMap.get(jaxrpcMappingFile); 168 if (javaWsdlMapping == null) 169 { 170 try 171 { 172 URL location = webservices.getResourceLoader().findResource(jaxrpcMappingFile); 174 JavaWsdlMappingFactory mappingFactory = JavaWsdlMappingFactory.newInstance(); 175 javaWsdlMapping = mappingFactory.parse(location); 176 webservices.jaxrpcMappingFileMap.put(jaxrpcMappingFile, javaWsdlMapping); 177 } 178 catch (Exception e) 179 { 180 throw new JAXRPCException ("Cannot unmarshal jaxrpc-mapping-file: " + jaxrpcMappingFile, e); 181 } 182 } 183 return javaWsdlMapping; 184 } 185 186 public void setJaxrpcMappingFile(String jaxrpcMappingFile) 187 { 188 this.jaxrpcMappingFile = jaxrpcMappingFile; 189 } 190 191 194 public Definition getWsdlDefinition() 195 { 196 WSDLMapEntry wsdlEntry = (WSDLMapEntry)webservices.wsdlFileMap.get(wsdlFile); 197 if (wsdlEntry == null) 198 { 199 try 200 { 201 URL wsdlLocation = webservices.getResourceLoader().findResource(wsdlFile); 202 if (wsdlLocation == null) 203 throw new IllegalArgumentException ("Cannot find wsdl in deployment: " + wsdlFile); 204 205 WSDL11DefinitionFactory factory = WSDL11DefinitionFactory.newInstance(); 206 Definition wsdlDefinition = factory.parse(wsdlLocation); 207 208 wsdlEntry = new WSDLMapEntry(wsdlDefinition); 209 webservices.wsdlFileMap.put(wsdlFile, wsdlEntry); 210 } 211 catch (WSDLException e) 212 { 213 throw new IllegalStateException ("Cannot obtain WSDL definition, cause: " + e.toString()); 214 } 215 } 216 217 return wsdlEntry.wsdlDefinition; 218 } 219 220 public static boolean isValidServiceUrl(String url) 221 { 222 URI uri; 223 try 224 { 225 uri = new URI (url); 226 } 227 catch (URISyntaxException e) 228 { 229 return false; 230 } 231 232 String scheme = uri.getScheme(); 233 if (! "http".equals(scheme) && ! "https".equals(scheme)) 234 return false; 235 236 if (uri.getHost() == null) 237 return false; 238 239 return true; 240 } 241 242 245 public void updateServiceAddress(ServiceDeployer.ServiceLocationResolver locationResolver) 246 { 247 Definition wsdlDefinition = getWsdlDefinition(); 249 250 WSDLMapEntry wsdlEntry = (WSDLMapEntry)webservices.wsdlFileMap.get(wsdlFile); 251 if (wsdlEntry.locationReplaced == false) 252 { 253 replaceAddressLocations(wsdlDefinition, locationResolver); 254 wsdlEntry.locationReplaced = true; 255 } 256 } 257 258 261 private void replaceAddressLocations(Definition wsdlDefinition, ServiceDeployer.ServiceLocationResolver locationResolver) 262 { 263 WebserviceDescriptionMetaData[] wsdArr = webservices.getWebserviceDescriptions(); 265 for (int i = 0; i < wsdArr.length; i++) 266 { 267 WebserviceDescriptionMetaData wsdMetaData = wsdArr[i]; 268 if (wsdlFile.equals(wsdMetaData.wsdlFile)) 269 { 270 PortComponentMetaData[] pcarr = wsdMetaData.getPortComponents(); 271 for (int pcIndex = 0; pcIndex < pcarr.length; pcIndex++) 272 { 273 PortComponentMetaData portComponent = pcarr[pcIndex]; 274 String pcWsdlPortName = portComponent.getWsdlPort().getLocalPart(); 275 Port wsdlPort = null; 276 277 Iterator itServices = wsdlDefinition.getServices().values().iterator(); 278 while (wsdlPort == null && itServices.hasNext()) 279 { 280 Service wsdlService = (Service)itServices.next(); 281 282 Map wsdlPorts = wsdlService.getPorts(); 283 Iterator itPorts = wsdlPorts.keySet().iterator(); 284 while (wsdlPort == null && itPorts.hasNext()) 285 { 286 String wsdlPortName = (String )itPorts.next(); 287 if (wsdlPortName.equals(pcWsdlPortName)) 288 { 289 wsdlPort = (Port)wsdlPorts.get(wsdlPortName); 290 Iterator itElements = wsdlPort.getExtensibilityElements().iterator(); 291 while (itElements.hasNext()) 292 { 293 Object obj = itElements.next(); 294 if (obj instanceof SOAPAddress) 295 { 296 SOAPAddress address = (SOAPAddress)obj; 297 String wsdlURI = address.getLocationURI(); 298 String addressUrl = null; 299 String schema = null; 300 301 if (wsdlURI.startsWith("http://") || wsdlURI.startsWith("https://")) 302 schema = wsdlURI.substring(0, wsdlURI.indexOf("://") + 3); 303 304 String endpointUrl = locationResolver.getServiceLocation(schema, portComponent); 305 306 if (isValidServiceUrl(wsdlURI) && ! locationResolver.alwaysResolve()) 307 addressUrl = wsdlURI; 308 else 309 addressUrl = endpointUrl; 310 311 try 312 { 313 portComponent.setServiceEndpointURL(new URL (endpointUrl)); 314 log.debug("Replace port location '" + wsdlURI + "' with '" + addressUrl + "'"); 315 address.setLocationURI(addressUrl); 316 } 317 catch (MalformedURLException e) 318 { 319 log.error("Invalid service URL: " + endpointUrl); 320 } 321 } 322 } 323 } 324 } 325 } 326 327 if (wsdlPort == null) 328 throw new IllegalArgumentException ("Cannot find port with name '" + pcWsdlPortName + "' in wsdl document"); 329 } 330 } 331 } 332 } 333 334 340 class WSDLMapEntry 341 { 342 Definition wsdlDefinition; 343 boolean locationReplaced; 344 345 public WSDLMapEntry(Definition wsdlDefinition) 346 { 347 this.wsdlDefinition = wsdlDefinition; 348 } 349 } 350 } 351 | Popular Tags |