1 17 package org.apache.servicemix.components.wsif; 18 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.jbi.JBIException; 25 import javax.jbi.messaging.Fault; 26 import javax.jbi.messaging.MessageExchange; 27 import javax.jbi.messaging.MessagingException; 28 import javax.jbi.messaging.NormalizedMessage; 29 import javax.wsdl.Binding; 30 import javax.wsdl.Definition; 31 import javax.wsdl.WSDLException; 32 33 import org.apache.servicemix.components.util.TransformComponentSupport; 34 import org.apache.wsif.WSIFException; 35 import org.apache.wsif.WSIFMessage; 36 import org.apache.wsif.WSIFOperation; 37 import org.apache.wsif.WSIFService; 38 import org.apache.wsif.WSIFServiceFactory; 39 import org.apache.wsif.util.WSIFUtils; 40 import org.springframework.core.io.Resource; 41 42 48 public class WSIFBinding extends TransformComponentSupport { 49 50 private WSIFMarshaler marshaler = new WSIFMarshaler(); 51 private WSIFService serviceHelper; 52 private Definition definition; 53 private Resource definitionResource; 54 private WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 55 private WSIFOperationMap operationMap; 56 57 protected void init() throws JBIException { 58 try { 59 if (definition == null) { 60 if (definitionResource == null) { 61 throw new JBIException("You must specify a definition or definitionResource property"); 62 } 63 String uri = definitionResource.getURL().toString(); 64 InputStreamReader reader = new InputStreamReader (definitionResource.getInputStream()); 65 definition = WSIFUtils.readWSDL(uri, reader); 66 } 67 68 if (serviceHelper == null) { 69 serviceHelper = factory.getService(definition); 70 } 71 72 for (Iterator iter = serviceHelper.getAvailablePortNames(); iter.hasNext();) { 73 System.out.println("Available port name: " + iter.next()); 74 } 75 76 operationMap = new WSIFOperationMap(serviceHelper); 77 78 Map bindings = definition.getBindings(); 79 for (Iterator iter = bindings.entrySet().iterator(); iter.hasNext();) { 80 Map.Entry entry = (Map.Entry ) iter.next(); 81 Binding binding = (Binding) entry.getValue(); 82 operationMap.addBinding(binding); 83 } 84 } 85 catch (IOException e) { 86 throw new JBIException(e); 87 } 88 catch (WSDLException e) { 89 throw new JBIException(e); 90 } 91 } 92 93 96 public WSIFMarshaler getMarshaller() { 97 return marshaler; 98 } 99 100 103 public void setMarshaller(WSIFMarshaler marshaler) { 104 this.marshaler = marshaler; 105 } 106 107 public WSIFMarshaler getMarshaler() { 108 return marshaler; 109 } 110 111 public void setMarshaler(WSIFMarshaler marshaler) { 112 this.marshaler = marshaler; 113 } 114 115 public Definition getDefinition() { 116 return definition; 117 } 118 119 public void setDefinition(Definition definition) { 120 this.definition = definition; 121 } 122 123 public Resource getDefinitionResource() { 124 return definitionResource; 125 } 126 127 public void setDefinitionResource(Resource definitionResource) { 128 this.definitionResource = definitionResource; 129 } 130 131 public WSIFServiceFactory getFactory() { 132 return factory; 133 } 134 135 public void setFactory(WSIFServiceFactory factory) { 136 this.factory = factory; 137 } 138 139 public WSIFService getServiceHelper() { 140 return serviceHelper; 141 } 142 143 public void setServiceHelper(WSIFService serviceHelper) { 144 this.serviceHelper = serviceHelper; 145 } 146 147 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 150 try { 151 WSIFOperationInfo operationInfo = operationMap.getOperationForExchange(exchange); 152 153 WSIFOperation operation = operationInfo.createWsifOperation(); 154 WSIFMessage inMessage = operation.createInputMessage(); 155 Object body = getBody(in); 156 marshaler.fromNMS(operationInfo, inMessage, in, body); 157 158 WSIFMessage outMessage = operation.createInputMessage(); 159 WSIFMessage faultMessage = operation.createInputMessage(); 160 boolean answer = operation.executeRequestResponseOperation(inMessage, outMessage, faultMessage); 161 if (answer) { 162 marshaler.toNMS(exchange, out, operationInfo, outMessage); 163 } 164 else { 165 Fault fault = exchange.createFault(); 166 marshaler.toNMS(exchange, fault, operationInfo, outMessage); 167 exchange.setFault(fault); 168 } 169 return true; 170 } 171 catch (WSIFException e) { 172 throw new MessagingException(e); 173 } 174 } 175 176 } 177 | Popular Tags |