KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > JbiChannel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jsr181.xfire;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import javax.jbi.messaging.DeliveryChannel;
25 import javax.jbi.messaging.ExchangeStatus;
26 import javax.jbi.messaging.InOut;
27 import javax.jbi.messaging.MessageExchangeFactory;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.jbi.servicedesc.ServiceEndpoint;
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.stream.XMLOutputFactory;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36
37 import org.apache.servicemix.jbi.jaxp.StAXSourceTransformer;
38 import org.codehaus.xfire.MessageContext;
39 import org.codehaus.xfire.XFireException;
40 import org.codehaus.xfire.exchange.InMessage;
41 import org.codehaus.xfire.exchange.MessageSerializer;
42 import org.codehaus.xfire.exchange.OutMessage;
43 import org.codehaus.xfire.fault.XFireFault;
44 import org.codehaus.xfire.soap.AbstractSoapBinding;
45 import org.codehaus.xfire.transport.AbstractChannel;
46 import org.codehaus.xfire.transport.Channel;
47
48 /**
49  * Jbi channel, only support local invocations.
50  */

51 public class JbiChannel extends AbstractChannel {
52
53     public static final String JavaDoc JBI_INTERFACE_NAME = "jbi.interface";
54     public static final String JavaDoc JBI_SERVICE_NAME = "jbi.service";
55     public static final String JavaDoc JBI_ENDPOINT = "jbi.endpoint";
56     
57     private StAXSourceTransformer sourceTransformer;
58     private XMLOutputFactory outputFactory;
59     
60     public JbiChannel(String JavaDoc uri, JbiTransport transport) {
61         setTransport(transport);
62         setUri(uri);
63         this.sourceTransformer = new StAXSourceTransformer();
64         this.outputFactory = XMLOutputFactory.newInstance();
65     }
66
67     public void open() throws Exception JavaDoc {
68     }
69
70     public void send(MessageContext context, OutMessage message) throws XFireException {
71         if (Channel.BACKCHANNEL_URI.equals(message.getUri())) {
72             final OutputStream JavaDoc out = (OutputStream JavaDoc) context.getProperty(Channel.BACKCHANNEL_URI);
73             if (out != null) {
74                 try {
75                     final XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out, message.getEncoding());
76                     message.getSerializer().writeMessage(message, writer, context);
77                     writer.close();
78                 } catch (XMLStreamException e) {
79                     throw new XFireException("Error closing output stream", e);
80                 }
81                 return;
82             }
83         } else {
84             try {
85                 DeliveryChannel channel = ((JbiTransport) getTransport()).getContext().getDeliveryChannel();
86                 MessageExchangeFactory factory = channel.createExchangeFactory();
87                 if (context.getExchange().hasOutMessage()) {
88                     InOut me = factory.createInOutExchange();
89                     me.setInterfaceName((QName JavaDoc) context.getService().getProperty(JBI_INTERFACE_NAME));
90                     me.setService((QName JavaDoc) context.getService().getProperty(JBI_SERVICE_NAME));
91                     me.setEndpoint((ServiceEndpoint) context.getService().getProperty(JBI_ENDPOINT));
92                     NormalizedMessage msg = me.createMessage();
93                     me.setInMessage(msg);
94                     msg.setContent(getContent(context, message));
95                     if (!channel.sendSync(me)) {
96                         throw new XFireException("Unable to send jbi exchange: sendSync returned false");
97                     }
98                     if (me.getStatus() == ExchangeStatus.ERROR) {
99                         me.setStatus(ExchangeStatus.DONE);
100                         channel.send(me);
101                         if (me.getError() != null) {
102                             throw new XFireFault(me.getError(), XFireFault.RECEIVER);
103                         } else if (me.getFault() != null){
104                             // TODO: retrieve fault
105
throw new XFireFault("Fault received", XFireFault.RECEIVER);
106                         } else {
107                             throw new XFireFault("Unkown Error", XFireFault.RECEIVER);
108                         }
109                     }
110                     Source JavaDoc outSrc = me.getOutMessage().getContent();
111                     me.setStatus(ExchangeStatus.DONE);
112                     channel.send(me);
113
114                     InMessage inMessage = new InMessage(sourceTransformer.toXMLStreamReader(outSrc), getUri());
115                     getEndpoint().onReceive(context, inMessage);
116                 } else {
117                     // TODO
118
}
119                 
120                 
121             } catch (Exception JavaDoc e) {
122                 throw new XFireException("Error sending jbi exchange", e);
123             }
124         }
125     }
126
127     protected Source JavaDoc getContent(MessageContext context, OutMessage message) throws XMLStreamException, IOException JavaDoc, XFireException {
128         ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
129         XMLStreamWriter writer = outputFactory.createXMLStreamWriter(outStream, message.getEncoding());
130         MessageSerializer serializer = context.getOutMessage().getSerializer();
131         if (serializer == null)
132         {
133             AbstractSoapBinding binding = (AbstractSoapBinding) context.getBinding();
134             if (binding == null)
135             {
136                 throw new XFireException("Couldn't find the binding!");
137             }
138             serializer = AbstractSoapBinding.getSerializer(binding.getStyle(), binding.getUse());
139         }
140         serializer.writeMessage(message, writer, context);
141         writer.close();
142         outStream.close();
143         StreamSource JavaDoc src = new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc(outStream.toByteArray()));
144         return src;
145     }
146     
147 }
148
Popular Tags