1 17 package org.apache.servicemix.components.reflection; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Proxy ; 22 23 import javax.jbi.JBIException; 24 import javax.jbi.messaging.ExchangeStatus; 25 import javax.jbi.messaging.InOut; 26 import javax.jbi.messaging.NormalizedMessage; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.servicemix.components.util.ComponentSupport; 31 import org.apache.servicemix.jbi.RuntimeJBIException; 32 33 39 public class ProxyInOutBinding extends ComponentSupport implements InvocationHandler { 40 41 private static final Log log = LogFactory.getLog(ProxyInOutBinding.class); 42 43 private ClassLoader cl; 44 private final Class [] interfaces; 45 46 public ProxyInOutBinding(Object target) { 47 this( target.getClass().getClassLoader(), target.getClass().getInterfaces()); 48 } 49 50 public ProxyInOutBinding(ClassLoader cl, Class [] interfaces) { 51 this.cl = cl; 52 this.interfaces = interfaces; 53 } 54 55 public Object createProxy() { 56 return Proxy.newProxyInstance(cl, interfaces, this); 57 } 58 59 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 60 if (log.isTraceEnabled()) { 61 log.trace("Invoked: " + proxy); 62 } 63 try { 64 InOut messageExchange = getDeliveryChannel().createExchangeFactory().createInOutExchange(); 65 NormalizedMessage inMessage = messageExchange.createMessage(); 66 inMessage.setProperty("proxy", proxy); 67 inMessage.setProperty("method", method); 68 inMessage.setProperty("args", args); 69 70 messageExchange.setInMessage(inMessage); 71 if (getDeliveryChannel().sendSync(messageExchange)) { 72 NormalizedMessage outMessage = messageExchange.getOutMessage(); 73 return getBody(outMessage); 74 } else if ( messageExchange.getStatus() == ExchangeStatus.ERROR ) { 75 throw messageExchange.getError(); 76 } 77 return null; 78 } 79 catch (JBIException e) { 80 throw new RuntimeJBIException(e); 81 } 82 } 83 } 84 | Popular Tags |