1 package org.objectweb.celtix.jbi.transport; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.lang.reflect.Method ; 8 import java.util.concurrent.Executor ; 9 import java.util.concurrent.Future ; 10 import java.util.logging.Logger ; 11 12 import javax.jbi.messaging.DeliveryChannel; 13 import javax.jbi.messaging.InOut; 14 import javax.jbi.messaging.MessageExchangeFactory; 15 import javax.jbi.messaging.NormalizedMessage; 16 import javax.jws.WebService; 17 import javax.wsdl.Port; 18 import javax.xml.namespace.QName ; 19 import javax.xml.transform.Source ; 20 import javax.xml.transform.stream.StreamSource ; 21 import javax.xml.ws.handler.MessageContext; 22 23 import org.objectweb.celtix.bindings.ClientBinding; 24 import org.objectweb.celtix.bindings.ResponseCallback; 25 import org.objectweb.celtix.context.InputStreamMessageContext; 26 import org.objectweb.celtix.context.ObjectMessageContext; 27 import org.objectweb.celtix.context.OutputStreamMessageContext; 28 import org.objectweb.celtix.transports.ClientTransport; 29 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 30 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 31 32 33 34 39 public class JBIClientTransport implements ClientTransport { 40 41 private static final Logger LOG = Logger.getLogger(JBIClientTransport.class.getName()); 42 private final DeliveryChannel channel; 43 private final EndpointReferenceType endpointRef; 44 private final QName serviceName; 45 private final ResponseCallback responseCallback; 46 47 public JBIClientTransport(DeliveryChannel dc, 48 EndpointReferenceType epr, 49 ClientBinding binding) { 50 channel = dc; 51 endpointRef = epr; 52 serviceName = EndpointReferenceUtils.getServiceName(endpointRef); 53 responseCallback = binding.createResponseCallback(); 54 } 55 56 public void invokeOneway(OutputStreamMessageContext context) throws IOException { 57 throw new RuntimeException ("not yet implemented"); 58 } 59 60 public InputStreamMessageContext invoke(OutputStreamMessageContext context) 61 throws IOException { 62 63 try { 64 Method targetMethod = (Method )context.get(ObjectMessageContext.METHOD_OBJ); 65 Class <?> clz = targetMethod.getDeclaringClass(); 66 67 LOG.fine("invoking service " + clz); 68 69 WebService ws = clz.getAnnotation(WebService.class); 70 assert ws != null; 71 QName interfaceName = new QName (ws.targetNamespace(), ws.name()); 72 73 MessageExchangeFactory factory = channel.createExchangeFactoryForService(serviceName); 74 LOG.fine("create message exchange svc: " + serviceName); 75 InOut xchng = factory.createInOutExchange(); 76 77 NormalizedMessage inMsg = xchng.createMessage(); 78 LOG.fine("exchange endpoint: " + xchng.getEndpoint()); 79 80 InputStream ins = null; 81 82 if (inMsg != null) { 83 LOG.fine("setup message contents on " + inMsg); 84 inMsg.setContent(getMessageContent(context)); 85 xchng.setService(serviceName); 86 LOG.fine("service for exchange " + serviceName); 87 88 xchng.setInterfaceName(interfaceName); 89 90 xchng.setOperation(new QName (targetMethod.getName())); 91 xchng.setInMessage(inMsg); 92 LOG.fine("sending message"); 93 channel.sendSync(xchng); 94 95 NormalizedMessage outMsg = xchng.getOutMessage(); 96 ins = JBIMessageHelper.convertMessageToInputStream(outMsg.getContent()); 97 98 } else { 99 System.out.println("no message yet"); 100 } 101 102 if (ins == null) { 103 throw new IOException ("unable to retrieve message"); 104 } 105 return new JBIInputStreamMessageContext(context, ins); 106 107 } catch (Exception ex) { 108 ex.printStackTrace(); 109 throw new IOException (ex.toString()); 110 } 111 } 112 113 Source getMessageContent(OutputStreamMessageContext context) { 114 assert context instanceof JBIOutputStreamMessageContext 115 : "context must be of type JBIOutputStreamMessageContext"; 116 117 JBIOutputStreamMessageContext ctx = (JBIOutputStreamMessageContext)context; 118 ByteArrayOutputStream bos = (ByteArrayOutputStream )ctx.getOutputStream(); 119 return new StreamSource (new ByteArrayInputStream (bos.toByteArray())); 120 } 121 122 123 public Future <InputStreamMessageContext> invokeAsync(OutputStreamMessageContext context, 124 Executor executor) 125 throws IOException { 126 throw new RuntimeException ("not yet implemented"); 127 } 128 129 public void finalPrepareOutputStreamContext(OutputStreamMessageContext context) 130 throws IOException { 131 } 132 133 public ResponseCallback getResponseCallback() { 134 return responseCallback; 135 } 136 137 public void shutdown() { 138 } 139 140 141 public OutputStreamMessageContext createOutputStreamContext(MessageContext context) 142 throws IOException { 143 return new JBIOutputStreamMessageContext(context); 144 } 145 146 public EndpointReferenceType getTargetEndpoint() { 147 return null; 149 } 150 151 public EndpointReferenceType getDecoupledEndpoint() throws IOException { 152 return null; 154 } 155 156 public Port getPort() { 157 return null; 159 } 160 } 161 | Popular Tags |