KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > component > WSNEndpoint


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.wsn.component;
18
19 import java.io.StringWriter JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.jbi.component.ComponentContext;
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.ExchangeStatus;
29 import javax.jbi.messaging.Fault;
30 import javax.jbi.messaging.InOnly;
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.jws.Oneway;
36 import javax.jws.WebMethod;
37 import javax.jws.WebService;
38 import javax.xml.bind.JAXBContext;
39 import javax.xml.bind.JAXBException;
40 import javax.xml.bind.annotation.XmlRootElement;
41 import javax.xml.namespace.QName JavaDoc;
42 import javax.xml.ws.WebFault;
43
44 import org.apache.servicemix.common.Endpoint;
45 import org.apache.servicemix.common.ExchangeProcessor;
46 import org.apache.servicemix.jbi.jaxp.StringSource;
47 import org.oasis_open.docs.wsrf.bf_2.BaseFaultType;
48
49 public class WSNEndpoint extends Endpoint implements ExchangeProcessor {
50
51     protected ServiceEndpoint activated;
52     protected String JavaDoc address;
53     protected Object JavaDoc pojo;
54     protected DeliveryChannel channel;
55     protected JAXBContext jaxbContext;
56     protected Class JavaDoc endpointInterface;
57     
58     public WSNEndpoint(String JavaDoc address, Object JavaDoc pojo) {
59         this.address = address;
60         this.pojo = pojo;
61         String JavaDoc[] parts = split(address);
62         service = new QName JavaDoc(parts[0], parts[1]);
63         endpoint = parts[2];
64     }
65
66     @Override JavaDoc
67     public Role getRole() {
68         return Role.PROVIDER;
69     }
70
71     @Override JavaDoc
72     public void activate() throws Exception JavaDoc {
73         logger = this.serviceUnit.getComponent().getLogger();
74         WebService ws = getWebServiceAnnotation(pojo.getClass());
75         if (ws == null) {
76             throw new IllegalStateException JavaDoc("Unable to find WebService annotation");
77         }
78         endpointInterface = Class.forName(ws.endpointInterface());
79         jaxbContext = createJAXBContext(endpointInterface);
80         ws = getWebServiceAnnotation(endpointInterface);
81         if (ws != null) {
82             interfaceName = new QName JavaDoc(ws.targetNamespace(), ws.name());
83         }
84         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
85         activated = ctx.activateEndpoint(service, endpoint);
86         channel = ctx.getDeliveryChannel();
87     }
88     
89     public static JAXBContext createJAXBContext(Class JavaDoc interfaceClass) throws JAXBException {
90         List JavaDoc<Class JavaDoc> classes = new ArrayList JavaDoc<Class JavaDoc>();
91         classes.add(JbiFault.class);
92         for (Method JavaDoc mth : interfaceClass.getMethods()) {
93             WebMethod wm = (WebMethod) mth.getAnnotation(WebMethod.class);
94             if (wm != null) {
95                 classes.add(mth.getReturnType());
96                 classes.addAll(Arrays.asList(mth.getParameterTypes()));
97             }
98         }
99         return JAXBContext.newInstance(classes.toArray(new Class JavaDoc[0]));
100     }
101
102     @Override JavaDoc
103     public void deactivate() throws Exception JavaDoc {
104         ServiceEndpoint ep = activated;
105         activated = null;
106         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
107         ctx.deactivateEndpoint(ep);
108     }
109
110     @Override JavaDoc
111     public ExchangeProcessor getProcessor() {
112         return this;
113     }
114
115     @SuppressWarnings JavaDoc("unchecked")
116     public void process(MessageExchange exchange) throws Exception JavaDoc {
117         if (exchange.getStatus() == ExchangeStatus.DONE) {
118             return;
119         } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
120             return;
121         }
122         Object JavaDoc input = jaxbContext.createUnmarshaller().unmarshal(exchange.getMessage("in").getContent());
123         Method JavaDoc webMethod = null;
124         for (Method JavaDoc mth : endpointInterface.getMethods()) {
125             Class JavaDoc[] params = mth.getParameterTypes();
126             if (params.length == 1 && params[0].isAssignableFrom(input.getClass())) {
127                 webMethod = mth;
128                 break;
129             }
130         }
131         if (webMethod == null) {
132             throw new IllegalStateException JavaDoc("Could not determine invoked web method");
133         }
134         boolean oneWay = webMethod.getAnnotation(Oneway.class) != null;
135         Object JavaDoc output;
136         try {
137             output = webMethod.invoke(pojo, new Object JavaDoc[] { input });
138         } catch (InvocationTargetException JavaDoc e) {
139             if (e.getCause() instanceof Exception JavaDoc) {
140                 WebFault fa = (WebFault) e.getCause().getClass().getAnnotation(WebFault.class);
141                 if (exchange instanceof InOnly == false && fa != null) {
142                     BaseFaultType info = (BaseFaultType) e.getCause().getClass().getMethod("getFaultInfo").invoke(e.getCause());
143                     Fault fault = exchange.createFault();
144                     exchange.setFault(fault);
145                     exchange.setError((Exception JavaDoc) e.getCause());
146                     StringWriter JavaDoc writer = new StringWriter JavaDoc();
147                     jaxbContext.createMarshaller().marshal(new JbiFault(info), writer);
148                     fault.setContent(new StringSource(writer.toString()));
149                     channel.send(exchange);
150                     return;
151                 } else {
152                     throw (Exception JavaDoc) e.getCause();
153                 }
154             } else if (e.getCause() instanceof Error JavaDoc) {
155                 throw (Error JavaDoc) e.getCause();
156             } else {
157                 throw new RuntimeException JavaDoc(e.getCause());
158             }
159         }
160         if (oneWay) {
161             exchange.setStatus(ExchangeStatus.DONE);
162             channel.send(exchange);
163         } else {
164             NormalizedMessage msg = exchange.createMessage();
165             exchange.setMessage(msg, "out");
166             StringWriter JavaDoc writer = new StringWriter JavaDoc();
167             jaxbContext.createMarshaller().marshal(output, writer);
168             msg.setContent(new StringSource(writer.toString()));
169             channel.send(exchange);
170         }
171     }
172     
173     @XmlRootElement(name = "Fault")
174     public static class JbiFault {
175         private BaseFaultType info;
176         public JbiFault() {
177         }
178         public JbiFault(BaseFaultType info) {
179             this.info = info;
180         }
181         public BaseFaultType getInfo() {
182             return info;
183         }
184         public void setInfo(BaseFaultType info) {
185             this.info = info;
186         }
187     }
188     
189     protected Method JavaDoc getWebServiceMethod(QName JavaDoc interfaceName, QName JavaDoc operation) throws Exception JavaDoc {
190         WebService ws = getWebServiceAnnotation(pojo.getClass());
191         if (ws == null) {
192             throw new IllegalStateException JavaDoc("Unable to find WebService annotation");
193         }
194         Class JavaDoc itf = Class.forName(ws.endpointInterface());
195         for (Method JavaDoc mth : itf.getMethods()) {
196             WebMethod wm = (WebMethod) mth.getAnnotation(WebMethod.class);
197             if (wm != null) {
198                 
199             }
200         }
201         return null;
202     }
203
204     @SuppressWarnings JavaDoc("unchecked")
205     protected WebService getWebServiceAnnotation(Class JavaDoc clazz) {
206         for (Class JavaDoc cl = clazz; cl != null; cl = cl.getSuperclass()) {
207             WebService ws = (WebService) cl.getAnnotation(WebService.class);
208             if (ws != null) {
209                 return ws;
210             }
211         }
212         return null;
213     }
214
215     public void start() throws Exception JavaDoc {
216         // Nothing to do
217
}
218
219     public void stop() throws Exception JavaDoc {
220         // Nothing to do
221
}
222
223     protected String JavaDoc[] split(String JavaDoc uri) {
224         char sep;
225         if (uri.indexOf('/') > 0) {
226             sep = '/';
227         } else {
228             sep = ':';
229         }
230         int idx1 = uri.lastIndexOf(sep);
231         int idx2 = uri.lastIndexOf(sep, idx1 - 1);
232         String JavaDoc epName = uri.substring(idx1 + 1);
233         String JavaDoc svcName = uri.substring(idx2 + 1, idx1);
234         String JavaDoc nsUri = uri.substring(0, idx2);
235         return new String JavaDoc[] { nsUri, svcName, epName };
236     }
237 }
238
Popular Tags