KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > ScaEndpoint


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;
18
19 import java.io.StringWriter JavaDoc;
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.jbi.component.ComponentContext;
29 import javax.jbi.messaging.DeliveryChannel;
30 import javax.jbi.messaging.ExchangeStatus;
31 import javax.jbi.messaging.MessageExchange;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.jbi.messaging.MessageExchange.Role;
34 import javax.jbi.servicedesc.ServiceEndpoint;
35 import javax.xml.bind.JAXBContext;
36
37 import org.apache.servicemix.common.Endpoint;
38 import org.apache.servicemix.common.ExchangeProcessor;
39 import org.apache.servicemix.jbi.jaxp.StringSource;
40 import org.apache.tuscany.core.context.EntryPointContext;
41 import org.apache.tuscany.model.assembly.ConfiguredReference;
42 import org.apache.tuscany.model.assembly.ConfiguredService;
43 import org.apache.tuscany.model.assembly.EntryPoint;
44
45 /**
46  *
47  * @author gnodet
48  * @version $Revision: 366467 $
49  * @org.apache.xbean.XBean element="endpoint" description="A sca endpoint"
50  *
51  */

52 public class ScaEndpoint extends Endpoint implements ExchangeProcessor {
53
54     protected ServiceEndpoint activated;
55
56     protected EntryPoint entryPoint;
57
58     protected Map JavaDoc<Class JavaDoc, Method JavaDoc> methodMap;
59
60     protected JAXBContext jaxbContext;
61
62     protected DeliveryChannel channel;
63
64     public ScaEndpoint(EntryPoint entryPoint) {
65         this.entryPoint = entryPoint;
66     }
67
68     public Role getRole() {
69         return Role.PROVIDER;
70     }
71
72     public void activate() throws Exception JavaDoc {
73         logger = this.serviceUnit.getComponent().getLogger();
74         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
75         activated = ctx.activateEndpoint(service, endpoint);
76         channel = ctx.getDeliveryChannel();
77         // Get the target service
78
ConfiguredReference referenceValue = entryPoint.getConfiguredReference();
79         ConfiguredService targetServiceEndpoint = referenceValue.getTargetConfiguredServices().get(0);
80         // Get the business interface
81
Class JavaDoc serviceInterface = targetServiceEndpoint.getService().getServiceContract().getInterface();
82         List JavaDoc<Class JavaDoc> classes = new ArrayList JavaDoc<Class JavaDoc>();
83         methodMap = new HashMap JavaDoc<Class JavaDoc, Method JavaDoc>();
84         for (Method JavaDoc mth : serviceInterface.getMethods()) {
85             Class JavaDoc[] params = mth.getParameterTypes();
86             if (params.length != 1) {
87                 throw new IllegalStateException JavaDoc("Supports only methods with one parameter");
88             }
89             methodMap.put(params[0], mth);
90             classes.add(mth.getReturnType());
91             classes.add(params[0]);
92         }
93         jaxbContext = JAXBContext.newInstance(classes.toArray(new Class JavaDoc[0]));
94     }
95
96     public void deactivate() throws Exception JavaDoc {
97         ServiceEndpoint ep = activated;
98         activated = null;
99         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
100         ctx.deactivateEndpoint(ep);
101     }
102
103     public ExchangeProcessor getProcessor() {
104         return this;
105     }
106
107     public void process(MessageExchange exchange) throws Exception JavaDoc {
108         if (exchange.getStatus() == ExchangeStatus.DONE) {
109             return;
110         } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
111             return;
112         }
113         Object JavaDoc input = jaxbContext.createUnmarshaller().unmarshal(exchange.getMessage("in").getContent());
114         Method JavaDoc method = methodMap.get(input.getClass());
115         if (method == null) {
116             throw new IllegalStateException JavaDoc("Could not determine invoked web method");
117         }
118         boolean oneWay = method.getReturnType() == null;
119         Object JavaDoc output;
120         try {
121             EntryPointContext entryPointContext = (EntryPointContext) ((ScaServiceUnit) serviceUnit)
122                     .getTuscanyRuntime().getModuleContext().getContext(entryPoint.getName());
123             InvocationHandler JavaDoc handler = (InvocationHandler JavaDoc) entryPointContext.getImplementationInstance();
124             output = handler.invoke(null, method, new Object JavaDoc[] { input });
125         } catch (UndeclaredThrowableException JavaDoc e) {
126             throw e;
127         } catch (RuntimeException JavaDoc e) {
128             throw e;
129         } catch (Error JavaDoc e) {
130             throw e;
131         } catch (Exception JavaDoc e) {
132             throw e;
133         } catch (Throwable JavaDoc e) {
134             throw new RuntimeException JavaDoc(e);
135         }
136         if (oneWay) {
137             exchange.setStatus(ExchangeStatus.DONE);
138             channel.send(exchange);
139         } else {
140             NormalizedMessage msg = exchange.createMessage();
141             exchange.setMessage(msg, "out");
142             StringWriter JavaDoc writer = new StringWriter JavaDoc();
143             jaxbContext.createMarshaller().marshal(output, writer);
144             msg.setContent(new StringSource(writer.toString()));
145             channel.send(exchange);
146         }
147     }
148
149     public void start() throws Exception JavaDoc {
150     }
151
152     public void stop() throws Exception JavaDoc {
153     }
154
155 }
156
Popular Tags