1 17 package org.apache.geronimo.axis.builder; 18 19 import java.io.File ; 20 import java.lang.reflect.Method ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Map ; 27 import javax.wsdl.Binding; 28 import javax.wsdl.BindingInput; 29 import javax.wsdl.BindingOperation; 30 import javax.wsdl.BindingOutput; 31 import javax.wsdl.Definition; 32 import javax.wsdl.Input; 33 import javax.wsdl.Message; 34 import javax.wsdl.Operation; 35 import javax.wsdl.OperationType; 36 import javax.wsdl.Output; 37 import javax.wsdl.Part; 38 import javax.wsdl.Port; 39 import javax.wsdl.PortType; 40 import javax.wsdl.WSDLException; 41 import javax.wsdl.extensions.ExtensionRegistry; 42 import javax.wsdl.extensions.soap.SOAPAddress; 43 import javax.wsdl.extensions.soap.SOAPBinding; 44 import javax.wsdl.extensions.soap.SOAPBody; 45 import javax.wsdl.extensions.soap.SOAPOperation; 46 import javax.wsdl.factory.WSDLFactory; 47 import javax.wsdl.xml.WSDLReader; 48 import javax.xml.namespace.QName ; 49 50 import org.apache.geronimo.testsupport.TestSupport; 51 52 import org.apache.axis.constants.Style; 53 import org.apache.axis.soap.SOAPConstants; 54 import org.apache.geronimo.axis.builder.bookquote.BookQuote; 55 import org.apache.geronimo.axis.builder.bookquote.BookQuoteService; 56 import org.apache.geronimo.axis.builder.interop.InteropLab; 57 import org.apache.geronimo.axis.builder.interop.InteropTestPortType; 58 import org.apache.geronimo.axis.builder.mock.MockPort; 59 import org.apache.geronimo.axis.builder.mock.MockService; 60 import org.apache.geronimo.axis.client.OperationInfo; 61 import org.apache.geronimo.axis.client.AxisServiceReference; 62 import org.apache.geronimo.common.DeploymentException; 63 import org.apache.geronimo.deployment.DeploymentContext; 64 import org.apache.geronimo.deployment.util.UnpackedJarFile; 65 import org.apache.geronimo.gbean.AbstractName; 66 import org.apache.geronimo.j2ee.deployment.EJBModule; 67 import org.apache.geronimo.j2ee.deployment.Module; 68 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 69 import org.apache.geronimo.kernel.Jsr77Naming; 70 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 71 import org.apache.geronimo.kernel.config.ConfigurationManager; 72 import org.apache.geronimo.kernel.config.SimpleConfigurationManager; 73 import org.apache.geronimo.kernel.repository.Artifact; 74 import org.apache.geronimo.kernel.repository.Environment; 75 import org.apache.geronimo.kernel.repository.ArtifactManager; 76 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 77 import org.apache.geronimo.kernel.repository.ArtifactResolver; 78 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 79 import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType; 80 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingDocument; 81 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType; 82 import org.apache.geronimo.xbeans.j2ee.PackageMappingType; 83 import org.apache.geronimo.webservices.builder.SchemaInfoBuilder; 84 85 88 public class ServiceReferenceTest 89 extends TestSupport 90 { 91 public final static String NAMESPACE = "http://geronimo.apache.org/axis/mock"; 92 private File tmpbasedir; 93 private Environment environment = new Environment(); 94 private Artifact configID = new Artifact("group", "test", "1", "car"); 95 private DeploymentContext context; 96 private ClassLoader isolatedCl = new URLClassLoader (new URL [0], this.getClass().getClassLoader()); 97 private final String operationName = "doMockOperation"; 98 private final File wsdlDir = new File (BASEDIR, "src/test/resources/interop"); 99 private final File wsdlFile = new File (wsdlDir, "interop.wsdl"); 100 private List handlerInfos = new ArrayList (); 101 private GerServiceRefType gerServiceRefType = null; 102 103 private Module module; 104 105 private boolean runExternalWSTest; 106 107 protected void setUp() throws Exception { 108 super.setUp(); 109 tmpbasedir = File.createTempFile("car", "tmp"); 110 tmpbasedir.delete(); 111 tmpbasedir.mkdirs(); 112 environment.setConfigId(configID); 113 Jsr77Naming naming = new Jsr77Naming(); 114 ArtifactManager artifactManager = new DefaultArtifactManager(); 115 ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.EMPTY_SET, null); 116 ConfigurationManager configurationManager = new SimpleConfigurationManager(Collections.EMPTY_SET, artifactResolver, Collections.EMPTY_SET); 117 AbstractName moduleName = naming.createRootName(configID, "testejb", NameFactory.EJB_MODULE); 118 context = new DeploymentContext(tmpbasedir, null, environment, moduleName, ConfigurationModuleType.CAR, naming, configurationManager, Collections.EMPTY_SET); 119 120 File moduleLocation = new File (tmpbasedir, "ejb"); 121 moduleLocation.mkdirs(); 122 module = new EJBModule(true, moduleName, environment, new UnpackedJarFile(moduleLocation), "ejb", null, null, null, null); 123 124 runExternalWSTest = System.getProperty("geronimo.run.external.webservicetest", "false").equals("true"); 125 } 126 127 protected void tearDown() throws Exception { 128 recursiveDelete(tmpbasedir); 129 super.tearDown(); 130 } 131 132 148 149 167 168 public void testBuildOperationInfo() throws Exception { 169 AxisBuilder builder = new AxisBuilder(null); 170 OperationInfo operationInfo = buildOperationInfoForMockOperation(builder); 171 assertNotNull(operationInfo); 172 } 173 174 public void testBuildFullServiceProxy() throws Exception { 175 Definition definition = buildDefinition(); 176 SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition); 177 JavaWsdlMappingType mapping = buildLightweightMappingType(); 178 QName serviceQName = new QName (NAMESPACE, "MockService"); 179 AxisBuilder builder = new AxisBuilder(null); 180 Object reference = builder.createService(MockService.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl); 181 assertNotNull(reference); 182 assertTrue(reference instanceof AxisServiceReference); 183 AxisServiceReference claReference = (AxisServiceReference) reference; 184 claReference.setClassLoader(isolatedCl); 185 Object proxy = claReference.getContent(); 186 assertTrue(proxy instanceof MockService); 187 MockPort mockPort = ((MockService) proxy).getMockPort(); 188 assertNotNull(mockPort); 189 } 190 191 public void testBuildBookQuoteProxy() throws Exception { 192 File wsdl = new File (BASEDIR, "src/test/resources/BookQuote.wsdl"); 193 WSDLFactory factory = WSDLFactory.newInstance(); 194 WSDLReader reader = factory.newWSDLReader(); 195 Definition definition = reader.readWSDL(wsdl.toURI().toString()); 196 SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition); 197 File jaxrpcMapping = new File (BASEDIR, "src/test/resources/BookQuote.xml"); 198 JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory.parse(jaxrpcMapping); 199 JavaWsdlMappingType mapping = mappingDocument.getJavaWsdlMapping(); 200 QName serviceQName = new QName ("http://www.Monson-Haefel.com/jwsbook/BookQuote", "BookQuoteService"); 201 AxisBuilder builder = new AxisBuilder(null); 202 Object reference = builder.createService(BookQuoteService.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl); 203 assertNotNull(reference); 204 assertTrue(reference instanceof AxisServiceReference); 205 AxisServiceReference claReference = (AxisServiceReference) reference; 206 claReference.setClassLoader(isolatedCl); 207 Object proxy = claReference.getContent(); 208 assertTrue(proxy instanceof BookQuoteService); 209 BookQuote bookQuote = ((BookQuoteService) proxy).getBookQuotePort(); 210 assertNotNull(bookQuote); 211 } 212 213 public void xtestBuildInteropProxy() throws Exception { 215 WSDLFactory factory = WSDLFactory.newInstance(); 216 WSDLReader reader = factory.newWSDLReader(); 217 Definition definition = reader.readWSDL(wsdlFile.toURI().toString()); 218 SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition); 219 File jaxrpcMapping = new File (BASEDIR, "src/test/resources/interop/interop-jaxrpcmapping.xml"); 220 JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory.parse(jaxrpcMapping); 221 JavaWsdlMappingType mapping = mappingDocument.getJavaWsdlMapping(); 222 QName serviceQName = new QName ("http://tempuri.org/4s4c/1/3/wsdl/def/interopLab", "interopLab"); 223 AxisBuilder builder = new AxisBuilder(null); 224 Object proxy = builder.createService(InteropLab.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl); 225 assertNotNull(proxy); 226 assertTrue(proxy instanceof InteropLab); 227 InteropTestPortType interopTestPort = ((InteropLab) proxy).getinteropTestPort(); 228 assertNotNull(interopTestPort); 229 testInteropPort(interopTestPort); 230 } 231 232 private void testInteropPort(InteropTestPortType interopTestPort) throws java.rmi.RemoteException { 233 if (runExternalWSTest) { 234 log.debug("Running external ws test"); 235 int result = interopTestPort.echoInteger(1); 236 assertEquals(result, 1); 237 } else { 238 log.debug("Skipping external ws test"); 239 } 240 } 241 242 public void testBuildComplexTypeMap() throws Exception { 243 WSDLFactory factory = WSDLFactory.newInstance(); 244 WSDLReader reader = factory.newWSDLReader(); 245 Definition definition = reader.readWSDL(wsdlFile.toURI().toString()); 246 SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition); 247 Map complexTypeMap = schemaInfoBuilder.getComplexTypesInWsdl(); 248 assertEquals(7, complexTypeMap.size()); 249 } 250 251 252 private OperationInfo buildOperationInfoForMockOperation(AxisBuilder builder) throws NoSuchMethodException , DeploymentException, WSDLException { 253 Class portClass = MockPort.class; 254 Method method = portClass.getDeclaredMethod("doMockOperation", new Class []{String .class}); 255 WSDLFactory factory = WSDLFactory.newInstance(); 256 Definition definition = factory.newDefinition(); 257 ExtensionRegistry extensionRegistry = factory.newPopulatedExtensionRegistry(); 258 BindingOperation bindingOperation = buildBindingOperation(definition, extensionRegistry); 259 260 Style defaultStyle = Style.DOCUMENT; 261 262 OperationInfo operationInfo = builder.buildOperationInfoLightweight(method, bindingOperation, defaultStyle, SOAPConstants.SOAP11_CONSTANTS); 263 return operationInfo; 264 } 265 266 private Definition buildDefinition() throws WSDLException { 267 WSDLFactory factory = WSDLFactory.newInstance(); 268 Definition definition = factory.newDefinition(); 269 definition.setDocumentBaseURI("META-INF/wsdl/fake.wsdl"); 270 ExtensionRegistry extensionRegistry = factory.newPopulatedExtensionRegistry(); 271 BindingOperation bindingOperation = buildBindingOperation(definition, extensionRegistry); 272 Binding binding = definition.createBinding(); 273 binding.setQName(new QName (NAMESPACE, "MockPortBinding")); 274 SOAPBinding soapBinding = (SOAPBinding) extensionRegistry.createExtension(Binding.class, new QName ("http://schemas.xmlsoap.org/wsdl/soap/", "binding")); 276 soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http"); 277 soapBinding.setStyle("rpc"); 278 binding.addExtensibilityElement(soapBinding); 279 binding.addBindingOperation(bindingOperation); 280 PortType portType = definition.createPortType(); 281 portType.setQName(new QName (NAMESPACE, "MockPort")); 282 portType.addOperation(bindingOperation.getOperation()); 283 binding.setPortType(portType); 284 Port port = definition.createPort(); 285 port.setName("MockPort"); 286 SOAPAddress soapAddress = (SOAPAddress) extensionRegistry.createExtension(Port.class, new QName ("http://schemas.xmlsoap.org/wsdl/soap/", "address")); 288 soapAddress.setLocationURI("http://127.0.0.1:8080/foo"); 289 port.addExtensibilityElement(soapAddress); 290 port.setBinding(binding); 291 javax.wsdl.Service service = definition.createService(); 292 service.setQName(new QName (NAMESPACE, "MockService")); 293 service.addPort(port); 294 definition.addService(service); 295 return definition; 296 } 297 298 private BindingOperation buildBindingOperation(Definition definition, ExtensionRegistry extensionRegistry) throws WSDLException { 299 Operation operation = definition.createOperation(); 300 operation.setName(operationName); 301 operation.setStyle(OperationType.REQUEST_RESPONSE); 302 Input input = definition.createInput(); 303 Message inputMessage = definition.createMessage(); 304 Part inputPart = definition.createPart(); 305 inputPart.setName("string"); 306 inputPart.setTypeName(new QName ("http://www.w3.org/2001/XMLSchema", "string")); 307 inputMessage.addPart(inputPart); 308 operation.setInput(input); 309 input.setMessage(inputMessage); 310 Output output = definition.createOutput(); 311 Message outputMessage = definition.createMessage(); 312 operation.setOutput(output); 313 output.setMessage(outputMessage); 314 BindingOperation bindingOperation = definition.createBindingOperation(); 315 SOAPOperation soapOperation = (SOAPOperation) extensionRegistry.createExtension(BindingOperation.class, new QName ("http://schemas.xmlsoap.org/wsdl/soap/", "operation")); 316 soapOperation.setSoapActionURI("actionURI"); 317 soapOperation.setStyle("rpc"); 318 bindingOperation.addExtensibilityElement(soapOperation); 319 bindingOperation.setOperation(operation); 320 bindingOperation.setName(operation.getName()); 321 BindingInput bindingInput = definition.createBindingInput(); 322 SOAPBody inputBody = (SOAPBody) extensionRegistry.createExtension(BindingInput.class, new QName ("http://schemas.xmlsoap.org/wsdl/soap/", "body")); 323 inputBody.setUse("encoded"); 324 bindingInput.addExtensibilityElement(inputBody); 325 bindingOperation.setBindingInput(bindingInput); 326 BindingOutput bindingOutput = definition.createBindingOutput(); 327 bindingOutput.addExtensibilityElement(inputBody); 328 bindingOperation.setBindingOutput(bindingOutput); 329 return bindingOperation; 330 } 331 332 private JavaWsdlMappingType buildLightweightMappingType() { 333 JavaWsdlMappingType mapping = JavaWsdlMappingType.Factory.newInstance(); 334 PackageMappingType packageMapping = mapping.addNewPackageMapping(); 335 packageMapping.addNewNamespaceURI().setStringValue(NAMESPACE); 336 packageMapping.addNewPackageType().setStringValue("org.apache.geronimo.axis.builder.mock"); 337 return mapping; 338 } 339 340 private void recursiveDelete(File file) { 341 if (file.isDirectory()) { 342 File [] files = file.listFiles(); 343 for (int i = 0; i < files.length; i++) { 344 recursiveDelete(files[i]); 345 } 346 } 347 file.delete(); 348 } 349 } 350 | Popular Tags |