KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > xfire > XFireBinding


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.components.xfire;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20
21 import javax.jbi.JBIException;
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.stream.XMLStreamReader;
25
26 import org.apache.servicemix.components.util.OutBinding;
27 import org.codehaus.xfire.MessageContext;
28 import org.codehaus.xfire.XFire;
29 import org.codehaus.xfire.exchange.InMessage;
30 import org.codehaus.xfire.transport.Channel;
31 import org.codehaus.xfire.transport.Transport;
32 import org.codehaus.xfire.transport.local.LocalTransport;
33
34 public class XFireBinding extends OutBinding {
35     private XMarshaler marshaler;
36
37     private XFire xfire;
38
39     public XFireBinding()
40     {
41         super();
42
43         this.marshaler = new XMarshaler();
44     }
45     
46     protected void process(MessageExchange messageExchange, NormalizedMessage nm) throws Exception JavaDoc {
47         
48         XMLStreamReader reader = marshaler.createStreamReader(nm);
49         if (reader == null) {
50             throw new JBIException("Could not get source as XMLStreamReader.");
51         }
52         
53         InMessage in = new InMessage(reader, "");
54         MessageContext context = new MessageContext();
55         context.setXFire(xfire);
56         context.setService(xfire.getServiceRegistry().getService(getService().getLocalPart()));
57
58         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
59         context.setProperty(Channel.BACKCHANNEL_URI, buffer);
60         
61         Transport transport = xfire.getTransportManager().getTransport(LocalTransport.BINDING_ID);
62         Channel channel = transport.createChannel();
63         channel.receive(context, in);
64
65         NormalizedMessage outMessage = messageExchange.createMessage();
66
67         marshaler.setContent(outMessage, buffer.toString());
68         marshaler.toNMS(outMessage, context.getOutMessage());
69
70         answer(messageExchange, outMessage);
71     }
72
73     public XFire getXfire() {
74         return xfire;
75     }
76
77     public void setXfire(XFire xfire) {
78         this.xfire = xfire;
79     }
80 }
81
Popular Tags