1 17 package org.apache.servicemix.wsn.component; 18 19 import java.io.StringWriter ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 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 ; 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 address; 53 protected Object pojo; 54 protected DeliveryChannel channel; 55 protected JAXBContext jaxbContext; 56 protected Class endpointInterface; 57 58 public WSNEndpoint(String address, Object pojo) { 59 this.address = address; 60 this.pojo = pojo; 61 String [] parts = split(address); 62 service = new QName (parts[0], parts[1]); 63 endpoint = parts[2]; 64 } 65 66 @Override  67 public Role getRole() { 68 return Role.PROVIDER; 69 } 70 71 @Override  72 public void activate() throws Exception { 73 logger = this.serviceUnit.getComponent().getLogger(); 74 WebService ws = getWebServiceAnnotation(pojo.getClass()); 75 if (ws == null) { 76 throw new IllegalStateException ("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 (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 interfaceClass) throws JAXBException { 90 List <Class > classes = new ArrayList <Class >(); 91 classes.add(JbiFault.class); 92 for (Method 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 [0])); 100 } 101 102 @Override  103 public void deactivate() throws Exception { 104 ServiceEndpoint ep = activated; 105 activated = null; 106 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext(); 107 ctx.deactivateEndpoint(ep); 108 } 109 110 @Override  111 public ExchangeProcessor getProcessor() { 112 return this; 113 } 114 115 @SuppressWarnings ("unchecked") 116 public void process(MessageExchange exchange) throws Exception { 117 if (exchange.getStatus() == ExchangeStatus.DONE) { 118 return; 119 } else if (exchange.getStatus() == ExchangeStatus.ERROR) { 120 return; 121 } 122 Object input = jaxbContext.createUnmarshaller().unmarshal(exchange.getMessage("in").getContent()); 123 Method webMethod = null; 124 for (Method mth : endpointInterface.getMethods()) { 125 Class [] 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 ("Could not determine invoked web method"); 133 } 134 boolean oneWay = webMethod.getAnnotation(Oneway.class) != null; 135 Object output; 136 try { 137 output = webMethod.invoke(pojo, new Object [] { input }); 138 } catch (InvocationTargetException e) { 139 if (e.getCause() instanceof Exception ) { 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 ) e.getCause()); 146 StringWriter writer = new StringWriter (); 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 ) e.getCause(); 153 } 154 } else if (e.getCause() instanceof Error ) { 155 throw (Error ) e.getCause(); 156 } else { 157 throw new RuntimeException (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 writer = new StringWriter (); 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 getWebServiceMethod(QName interfaceName, QName operation) throws Exception { 190 WebService ws = getWebServiceAnnotation(pojo.getClass()); 191 if (ws == null) { 192 throw new IllegalStateException ("Unable to find WebService annotation"); 193 } 194 Class itf = Class.forName(ws.endpointInterface()); 195 for (Method 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 ("unchecked") 205 protected WebService getWebServiceAnnotation(Class clazz) { 206 for (Class 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 { 216 } 218 219 public void stop() throws Exception { 220 } 222 223 protected String [] split(String 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 epName = uri.substring(idx1 + 1); 233 String svcName = uri.substring(idx2 + 1, idx1); 234 String nsUri = uri.substring(0, idx2); 235 return new String [] { nsUri, svcName, epName }; 236 } 237 } 238 | Popular Tags |