KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > rpc > RPCMessageClient


1 /*
2  * MessageQueueClient: The message queue client library
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * RPCMessageClient.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.rpc;
24
25 // java imports
26
import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 /**
32  * This object is responsible for establishing a connection to the Message
33  * Service and for setting the appropriate RPC proxy and handler so that
34  * messages can be constructed appropriatly.
35  *
36  * @author Brett Chaldecott
37  */

38 public class RPCMessageClient {
39     
40     /**
41      * Creates a new instance of RPCMessageClient
42      */

43     private RPCMessageClient() {
44     }
45     
46     
47     /**
48      * This method is called to setup a async interface so that messages can
49      * be sent properly.
50      *
51      * @return The reference that was created.
52      * @param from The address all the requests will come from.
53      * @param targetInterface The target interface.
54      * @param asyncInterface The asynchronis interface.
55      * @param targetURL The target url for the message.
56      * @exception RPCMessageClientException
57      */

58     public static Object JavaDoc create(String JavaDoc from, Class JavaDoc targetInterface,
59             Class JavaDoc asyncInterface, String JavaDoc targetURL) throws
60             RPCMessageClientException {
61         verify(targetInterface,asyncInterface);
62         RPCMessageHandler handler = new RPCMessageHandler(from,targetInterface,
63                 targetURL,true);
64         return Proxy.newProxyInstance(
65                 asyncInterface.getClassLoader(),
66                 new Class JavaDoc[] {asyncInterface},handler);
67     }
68     
69     
70     /**
71      * This method is called to setup a async interface so that messages can
72      * be sent properly.
73      *
74      * @return The reference that was created.
75      * @param from The address all the requests will come from.
76      * @param targetInterface The target interface.
77      * @param asyncInterface The asynchronis interface.
78      * @param services The list of services for this rpc message
79      * @param broadcast If true this message will be sent to all daemons suppling
80      * the given service.
81      * @exception RPCMessageClientException
82      */

83     public static Object JavaDoc create(String JavaDoc from, Class JavaDoc targetInterface,
84             Class JavaDoc asyncInterface, List JavaDoc services, boolean broadcast) throws
85             RPCMessageClientException {
86         verify(targetInterface,asyncInterface);
87         RPCMessageHandler handler = new RPCMessageHandler(from, targetInterface,
88                 services, broadcast,true);
89         return Proxy.newProxyInstance(
90                 asyncInterface.getClassLoader(),
91                 new Class JavaDoc[] {asyncInterface},handler);
92     }
93     
94     
95     /**
96      * This method is called to setup a async interface so that messages can
97      * be sent properly. The message will only go one way no result will be
98      * received.
99      *
100      * @return The reference that was created.
101      * @param from The address all the requests will come from.
102      * @param targetInterface The target interface.
103      * @param asyncInterface The asynchronis interface.
104      * @param targetURL The target url for the message.
105      * @exception RPCMessageClientException
106      */

107     public static Object JavaDoc createOneWay(String JavaDoc from, Class JavaDoc targetInterface,
108             Class JavaDoc asyncInterface, String JavaDoc targetURL) throws
109             RPCMessageClientException {
110         verify(targetInterface,asyncInterface);
111         RPCMessageHandler handler = new RPCMessageHandler(from,targetInterface,
112                 targetURL,false);
113         return Proxy.newProxyInstance(
114                 asyncInterface.getClassLoader(),
115                 new Class JavaDoc[] {asyncInterface},handler);
116     }
117     
118     
119     /**
120      * This method is called to setup a async interface so that messages can
121      * be sent properly. The message will only go one way no result will be
122      * received.
123      *
124      * @return The reference that was created.
125      * @param from The address all the requests will come from.
126      * @param targetInterface The target interface.
127      * @param asyncInterface The asynchronis interface.
128      * @param services The list of services for this rpc message
129      * @param broadcast If true this message will be sent to all daemons suppling
130      * the given service.
131      * @exception RPCMessageClientException
132      */

133     public static Object JavaDoc createOneWay(String JavaDoc from, Class JavaDoc targetInterface,
134             Class JavaDoc asyncInterface, List JavaDoc services, boolean broadcast) throws
135             RPCMessageClientException {
136         verify(targetInterface,asyncInterface);
137         RPCMessageHandler handler = new RPCMessageHandler(from, targetInterface,
138                 services, broadcast,false);
139         return Proxy.newProxyInstance(
140                 asyncInterface.getClassLoader(),
141                 new Class JavaDoc[] {asyncInterface},handler);
142     }
143     
144     
145     /**
146      * This method is called to setup a async interface so that messages can
147      * be sent properly.
148      *
149      * @return The reference that was created.
150      * @param from The address all the requests will come from.
151      * @param targetInterface The target interface.
152      * @param asyncInterface The asynchronis interface.
153      * @param targetURL The target url for the message.
154      * @param correlationId The correlation id for this message.
155      * @exception RPCMessageClientException
156      */

157     public static Object JavaDoc create(String JavaDoc from, Class JavaDoc targetInterface,
158             Class JavaDoc asyncInterface, String JavaDoc targetURL, String JavaDoc correlationId) throws
159             RPCMessageClientException {
160         verify(targetInterface,asyncInterface);
161         RPCMessageHandler handler = new RPCMessageHandler(from, targetInterface,
162                 targetURL, correlationId);
163         return Proxy.newProxyInstance(
164                 asyncInterface.getClassLoader(),
165                 new Class JavaDoc[] {asyncInterface},handler);
166     }
167     
168     
169     /**
170      * This method is called to setup a async interface so that messages can
171      * be sent properly.
172      *
173      * @return The reference that was created.
174      * @param from The address all the requests will come from.
175      * @param targetInterface The target interface.
176      * @param asyncInterface The asynchronis interface.
177      * @param services The list of services for this rpc message
178      * @param broadcast If true this message will be sent to all daemons suppling
179      * the given service.
180      * @param correlationId The correlation id for this message.
181      * @exception RPCMessageClientException
182      */

183     public static Object JavaDoc create(String JavaDoc from, Class JavaDoc targetInterface,
184             Class JavaDoc asyncInterface, List JavaDoc services, boolean broadcast,
185             String JavaDoc correlationId) throws RPCMessageClientException {
186         verify(targetInterface,asyncInterface);
187         RPCMessageHandler handler = new RPCMessageHandler(from, targetInterface,
188                 services, broadcast, correlationId);
189         return Proxy.newProxyInstance(
190                 asyncInterface.getClassLoader(),
191                 new Class JavaDoc[] {asyncInterface},handler);
192     }
193     
194     
195     /**
196      * This method is called to setup a async interface so that messages can
197      * be sent properly.
198      *
199      * @return The reference that was created.
200      * @param jndiURL The url of the message producer.
201      * @param context The context of the message service.
202      * @param from The address all the requests will come from.
203      * @param targetInterface The target interface.
204      * @param asyncInterface The asynchronis interface.
205      * @param targetURL The target url for the message.
206      * @exception RPCMessageClientException
207      */

208     public static Object JavaDoc create(InitialContext JavaDoc context, String JavaDoc jndiURL,
209             String JavaDoc from, Class JavaDoc targetInterface, Class JavaDoc asyncInterface,
210             String JavaDoc targetURL) throws RPCMessageClientException {
211         verify(targetInterface,asyncInterface);
212         RPCMessageHandler handler = new RPCMessageHandler(context, jndiURL,
213             from, targetInterface, targetURL);
214         return Proxy.newProxyInstance(
215                 asyncInterface.getClassLoader(),
216                 new Class JavaDoc[] {asyncInterface},handler);
217     }
218     
219     
220     /**
221      * This method is called to setup a async interface so that messages can
222      * be sent properly.
223      *
224      * @return The reference that was created.
225      * @param context The context of the message service.
226      * @param jndiURL The url of the message producer.
227      * @param from The address all the requests will come from.
228      * @param targetInterface The target interface.
229      * @param asyncInterface The asynchronis interface.
230      * @param services The list of services for this rpc message
231      * @param broadcast If true this message will be sent to all daemons suppling
232      * the given service.
233      * @exception RPCMessageClientException
234      */

235     public static Object JavaDoc create(InitialContext JavaDoc context, String JavaDoc jndiURL,
236             String JavaDoc from, Class JavaDoc targetInterface, Class JavaDoc asyncInterface, List JavaDoc services,
237             boolean broadcast) throws RPCMessageClientException {
238         verify(targetInterface,asyncInterface);
239         RPCMessageHandler handler = new RPCMessageHandler(context, jndiURL,
240             from, targetInterface, services, broadcast);
241         return Proxy.newProxyInstance(
242                 asyncInterface.getClassLoader(),
243                 new Class JavaDoc[] {asyncInterface},handler);
244     }
245     
246     
247     /**
248      * This method is responsible for verifying the target interface and async
249      * interface in sync.
250      *
251      * @param targetInterface The interface the call are made onto.
252      * @param asyncInterface The async interface the calls are made with.
253      * @exception RPCMessageClientException
254      */

255     private static void verify(Class JavaDoc targetInterface, Class JavaDoc asyncInterface)
256     throws RPCMessageClientException {
257         Method JavaDoc[] methods = asyncInterface.getMethods();
258         for (int index = 0; index < methods.length; index++) {
259             Method JavaDoc method = methods[index];
260             try {
261                 targetInterface.getMethod(method.getName(),
262                         method.getParameterTypes());
263             } catch (Exception JavaDoc ex) {
264                 throw new RPCMessageClientException(
265                         "Could not validate the methods : " + ex.getMessage(),
266                         ex);
267             }
268             if (method.getReturnType() != java.lang.String JavaDoc.class) {
269                 throw new RPCMessageClientException(
270                         "The return type of the async interface must be a " +
271                         "String. As it will contain the new Message Id");
272             }
273         }
274     }
275 }
276
Popular Tags