1 10 11 package org.mule.extras.client; 12 13 import org.mule.MuleException; 14 import org.mule.config.i18n.Message; 15 import org.mule.umo.UMOException; 16 import org.mule.umo.UMOMessage; 17 import org.mule.umo.transformer.UMOTransformer; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Proxy ; 22 23 30 31 public class MuleProxyListener implements InvocationHandler 32 { 33 private Class listenerClass; 34 private AbstractEventTransformer eventTransformer; 35 private String componentName; 36 private Object proxy; 37 private MuleClient client; 38 39 public MuleProxyListener(Class listenerClass, String componentName) throws UMOException 40 { 41 setListenerClass(listenerClass); 42 setEventTransformer(new EventObjectTransformer()); 43 setComponentName(componentName); 44 setClient(new MuleClient()); 45 createProxy(); 46 } 47 48 public MuleProxyListener(Class listenerClass, 49 AbstractEventTransformer eventTransformer, 50 String componentName) throws UMOException 51 { 52 setListenerClass(listenerClass); 53 setEventTransformer(eventTransformer); 54 setComponentName(componentName); 55 setClient(new MuleClient()); 56 createProxy(); 57 } 58 59 public MuleProxyListener(Class listenerClass, 60 AbstractEventTransformer eventTransformer, 61 String componentName, 62 MuleClient client) 63 { 64 setListenerClass(listenerClass); 65 setEventTransformer(eventTransformer); 66 setComponentName(componentName); 67 setClient(client); 68 createProxy(); 69 } 70 71 protected void createProxy() 72 { 73 proxy = Proxy.newProxyInstance(listenerClass.getClassLoader(), new Class []{listenerClass}, this); 74 } 75 76 public Class getListenerClass() 77 { 78 return listenerClass; 79 } 80 81 public void setListenerClass(Class listenerClass) 82 { 83 this.listenerClass = listenerClass; 84 } 85 86 public UMOTransformer getEventTransformer() 87 { 88 return eventTransformer; 89 } 90 91 public void setEventTransformer(AbstractEventTransformer eventTransformer) 92 { 93 this.eventTransformer = eventTransformer; 94 } 95 96 public String getComponentName() 97 { 98 return componentName; 99 } 100 101 public void setComponentName(String componentName) 102 { 103 this.componentName = componentName; 104 } 105 106 public MuleClient getClient() 107 { 108 return client; 109 } 110 111 public void setClient(MuleClient client) 112 { 113 this.client = client; 114 } 115 116 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 117 { 118 if (args.length == 0) 119 { 120 throw new MuleException(new Message("client", 2)); 121 } 122 UMOMessage message = eventTransformer.transform(args[0], method); 123 if (!"void".equals(method.getReturnType().getName())) 124 { 125 UMOMessage result = client.sendDirect(componentName, null, message); 126 if (UMOMessage.class.equals(method.getReturnType())) 127 { 128 return result; 129 } 130 else 131 { 132 return (result == null ? null : result.getPayload()); 133 } 134 } 135 else 136 { 137 client.dispatchDirect(componentName, message); 138 return null; 139 } 140 } 141 142 public Object getProxy() 143 { 144 return proxy; 145 } 146 } 147 | Popular Tags |