KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > handler > ExternalJbiServiceClient


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.sca.handler;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.jbi.messaging.DeliveryChannel;
23 import javax.jbi.messaging.ExchangeStatus;
24 import javax.jbi.messaging.InOut;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.xml.bind.JAXBContext;
27
28 import org.apache.servicemix.jbi.jaxp.StringSource;
29 import org.apache.servicemix.sca.ScaServiceUnit;
30 import org.apache.servicemix.sca.assembly.JbiBinding;
31 import org.apache.tuscany.model.assembly.ExternalService;
32
33 public class ExternalJbiServiceClient {
34
35     private ExternalService externalService;
36     
37     private JbiBinding jbiBinding;
38
39     private ScaServiceUnit serviceUnit;
40
41     /**
42      * Constructs a new ExternalWebServiceClient.
43      *
44      * @param externalService
45      * @param wsBinding
46      */

47     public ExternalJbiServiceClient(ExternalService externalService) {
48         this.serviceUnit = ScaServiceUnit.getCurrentScaServiceUnit();
49         this.externalService = externalService;
50         this.jbiBinding = (JbiBinding) this.externalService.getBindings().get(0);
51     }
52
53     /**
54      * Invoke an operation on the external Web service.
55      *
56      * @param method
57      * @param args
58      * @return
59      */

60     public Object JavaDoc invoke(Method JavaDoc method, Object JavaDoc[] args) {
61         if (args == null || args.length != 1) {
62             throw new IllegalStateException JavaDoc("args should have exactly one object");
63         }
64         try {
65             Object JavaDoc payload = args[0];
66             Class JavaDoc inputClass = method.getParameterTypes()[0];
67             Class JavaDoc outputClass = method.getReturnType();
68             JAXBContext context = JAXBContext.newInstance(inputClass, outputClass);
69             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
70             context.createMarshaller().marshal(payload, baos);
71             
72             DeliveryChannel channel = serviceUnit.getComponent().getComponentContext().getDeliveryChannel();
73             // TODO: in-only case ?
74
// TODO: interface based routing ?
75
// TODO: explicit endpoint selection ?
76
InOut inout = channel.createExchangeFactory().createInOutExchange();
77             inout.setService(jbiBinding.getServiceName());
78             NormalizedMessage in = inout.createMessage();
79             inout.setInMessage(in);
80             in.setContent(new StringSource(baos.toString()));
81             boolean sent = channel.sendSync(inout);
82             // TODO: check for error ?
83
NormalizedMessage out = inout.getOutMessage();
84             Object JavaDoc response = context.createUnmarshaller().unmarshal(out.getContent());
85             inout.setStatus(ExchangeStatus.DONE);
86             channel.send(inout);
87             return response;
88         } catch (Exception JavaDoc e) {
89             throw new RuntimeException JavaDoc(e);
90         }
91     }
92 }
93
Popular Tags