KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > client > MuleProxyListener


1 /*
2  * $Id: MuleProxyListener.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22
23 /**
24  * <code>MuleProxyListener</code> is a generic listent proxy that can be used to
25  * foward calls as Mule events from any Observer/Observerable implementation.
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30
31 public class MuleProxyListener implements InvocationHandler JavaDoc
32 {
33     private Class JavaDoc listenerClass;
34     private AbstractEventTransformer eventTransformer;
35     private String JavaDoc componentName;
36     private Object JavaDoc proxy;
37     private MuleClient client;
38
39     public MuleProxyListener(Class JavaDoc listenerClass, String JavaDoc 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 JavaDoc listenerClass,
49                              AbstractEventTransformer eventTransformer,
50                              String JavaDoc 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 JavaDoc listenerClass,
60                              AbstractEventTransformer eventTransformer,
61                              String JavaDoc 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 JavaDoc[]{listenerClass}, this);
74     }
75
76     public Class JavaDoc getListenerClass()
77     {
78         return listenerClass;
79     }
80
81     public void setListenerClass(Class JavaDoc 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 JavaDoc getComponentName()
97     {
98         return componentName;
99     }
100
101     public void setComponentName(String JavaDoc 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 JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
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 JavaDoc getProxy()
143     {
144         return proxy;
145     }
146 }
147
Popular Tags