1 17 package org.apache.servicemix.sca; 18 19 import java.io.StringWriter ; 20 import java.lang.reflect.InvocationHandler ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.UndeclaredThrowableException ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 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 52 public class ScaEndpoint extends Endpoint implements ExchangeProcessor { 53 54 protected ServiceEndpoint activated; 55 56 protected EntryPoint entryPoint; 57 58 protected Map <Class , Method > 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 { 73 logger = this.serviceUnit.getComponent().getLogger(); 74 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext(); 75 activated = ctx.activateEndpoint(service, endpoint); 76 channel = ctx.getDeliveryChannel(); 77 ConfiguredReference referenceValue = entryPoint.getConfiguredReference(); 79 ConfiguredService targetServiceEndpoint = referenceValue.getTargetConfiguredServices().get(0); 80 Class serviceInterface = targetServiceEndpoint.getService().getServiceContract().getInterface(); 82 List <Class > classes = new ArrayList <Class >(); 83 methodMap = new HashMap <Class , Method >(); 84 for (Method mth : serviceInterface.getMethods()) { 85 Class [] params = mth.getParameterTypes(); 86 if (params.length != 1) { 87 throw new IllegalStateException ("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 [0])); 94 } 95 96 public void deactivate() throws Exception { 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 { 108 if (exchange.getStatus() == ExchangeStatus.DONE) { 109 return; 110 } else if (exchange.getStatus() == ExchangeStatus.ERROR) { 111 return; 112 } 113 Object input = jaxbContext.createUnmarshaller().unmarshal(exchange.getMessage("in").getContent()); 114 Method method = methodMap.get(input.getClass()); 115 if (method == null) { 116 throw new IllegalStateException ("Could not determine invoked web method"); 117 } 118 boolean oneWay = method.getReturnType() == null; 119 Object output; 120 try { 121 EntryPointContext entryPointContext = (EntryPointContext) ((ScaServiceUnit) serviceUnit) 122 .getTuscanyRuntime().getModuleContext().getContext(entryPoint.getName()); 123 InvocationHandler handler = (InvocationHandler ) entryPointContext.getImplementationInstance(); 124 output = handler.invoke(null, method, new Object [] { input }); 125 } catch (UndeclaredThrowableException e) { 126 throw e; 127 } catch (RuntimeException e) { 128 throw e; 129 } catch (Error e) { 130 throw e; 131 } catch (Exception e) { 132 throw e; 133 } catch (Throwable e) { 134 throw new RuntimeException (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 writer = new StringWriter (); 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 { 150 } 151 152 public void stop() throws Exception { 153 } 154 155 } 156 | Popular Tags |