KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RPCMessageHandler.java
20  */

21
22 // the package path
23
package com.rift.coad.daemon.messageservice.rpc;
24
25 // java imports
26
import java.util.List JavaDoc;
27 import java.lang.reflect.InvocationHandler JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35 // coadunation imports
36
import com.rift.coad.daemon.messageservice.Producer;
37 import com.rift.coad.daemon.messageservice.MessageProducer;
38 import com.rift.coad.daemon.messageservice.RPCMessage;
39 import com.rift.coad.daemon.messageservice.Message;
40 import com.rift.coad.util.connection.ConnectionManager;
41
42 /**
43  * This object handles the creation of a RPC message.
44  *
45  * @author Brett Chaldecott
46  */

47 public class RPCMessageHandler implements InvocationHandler JavaDoc {
48     
49     // the logger reference
50
protected Logger log =
51             Logger.getLogger(RPCMessageHandler.class.getName());
52     
53     // private member variables
54
private Class JavaDoc targetInterface = null;
55     private Producer producer = null;
56     private String JavaDoc targetURL = null;
57     private String JavaDoc[] services = null;
58     private boolean reply = true;
59     private boolean broadcast = false;
60     private String JavaDoc correlationId = null;
61     private boolean usedCorrelationId = false;
62     
63     /**
64      * Creates a new instance of RPCMessageHandler
65      *
66      * @param from The address the message will come from.
67      * @param targetInterface The interface the message will wrap to.
68      * @param targetURL The target url for the request.
69      * @param reply TRUE if the message should reply, FALSE if it a oneway call.
70      * @exception RPCMessageClientException
71      */

72     public RPCMessageHandler(String JavaDoc from, Class JavaDoc targetInterface,
73             String JavaDoc targetURL, boolean reply) throws RPCMessageClientException {
74         try {
75             this.targetInterface = targetInterface;
76             MessageProducer messageProducer = (MessageProducer)ConnectionManager
77                     .getInstance().getConnection(MessageProducer.class,
78                     MessageProducer.JNDI_URL);
79             producer = messageProducer.createProducer(from);
80             this.targetURL = targetURL;
81             this.reply = reply;
82         } catch (Exception JavaDoc ex) {
83             throw new RPCMessageClientException(
84                     "Failed to init the message handler : " + ex.getMessage(),
85                     ex);
86         }
87     }
88     
89     
90     /**
91      * Creates a new instance of RPCMessageHandler
92      *
93      * @param from The address the message will come from.
94      * @param targetInterface The interface the message will wrap to.
95      * @param services The list of services.
96      * @param broadcast True if the messages must be sent all the systems that
97      * supply the listed services.
98      * @param reply TRUE if the message should reply, FALSE if it a oneway call.
99      * @exception RPCMessageClientException
100      */

101     public RPCMessageHandler(String JavaDoc from, Class JavaDoc targetInterface,
102             List JavaDoc services, boolean broadcast, boolean reply) throws
103             RPCMessageClientException {
104         try {
105             this.targetInterface = targetInterface;
106             MessageProducer messageProducer = (MessageProducer)ConnectionManager
107                     .getInstance().getConnection(MessageProducer.class,
108                     MessageProducer.JNDI_URL);
109             producer = messageProducer.createProducer(from);
110             copyServices(services);
111             this.broadcast = broadcast;
112             this.reply = reply;
113         } catch (Exception JavaDoc ex) {
114             throw new RPCMessageClientException(
115                     "Failed to init the message handler : " + ex.getMessage(),
116                     ex);
117         }
118     }
119     
120     
121     /**
122      * Creates a new instance of RPCMessageHandler
123      *
124      * @param from The address the message will come from.
125      * @param targetInterface The interface the message will wrap to.
126      * @param targetURL The target url for the request.
127      * @param correlationId The correlation id for the message.
128      * @exception RPCMessageClientException
129      */

130     public RPCMessageHandler(String JavaDoc from, Class JavaDoc targetInterface,
131             String JavaDoc targetURL, String JavaDoc correlationId) throws
132             RPCMessageClientException {
133         try {
134             this.targetInterface = targetInterface;
135             MessageProducer messageProducer = (MessageProducer)ConnectionManager
136                     .getInstance().getConnection(MessageProducer.class,
137                     MessageProducer.JNDI_URL);
138             producer = messageProducer.createProducer(from);
139             this.targetURL = targetURL;
140             this.correlationId = correlationId;
141         } catch (Exception JavaDoc ex) {
142             throw new RPCMessageClientException(
143                     "Failed to init the message handler : " + ex.getMessage(),
144                     ex);
145         }
146     }
147     
148     
149     /**
150      * Creates a new instance of RPCMessageHandler
151      *
152      * @param from The address the message will come from.
153      * @param targetInterface The interface the message will wrap to.
154      * @param services The list of services.
155      * @param broadcast True if the messages must be sent all the systems that
156      * supply the listed services.
157      * @param correlationId The correlation id for the message.
158      * @exception RPCMessageClientException
159      */

160     public RPCMessageHandler(String JavaDoc from, Class JavaDoc targetInterface,
161             List JavaDoc services, boolean broadcast, String JavaDoc correlationId) throws
162             RPCMessageClientException {
163         try {
164             this.targetInterface = targetInterface;
165             MessageProducer messageProducer = (MessageProducer)ConnectionManager
166                     .getInstance().getConnection(MessageProducer.class,
167                     MessageProducer.JNDI_URL);
168             producer = messageProducer.createProducer(from);
169             copyServices(services);
170             this.broadcast = broadcast;
171             this.correlationId = correlationId;
172         } catch (Exception JavaDoc ex) {
173             throw new RPCMessageClientException(
174                     "Failed to init the message handler : " + ex.getMessage(),
175                     ex);
176         }
177     }
178     
179     
180     /**
181      * Creates a new instance of RPCMessageHandler
182      *
183      * @param context The context that will be used to retrieve the service.
184      * @param jndiURL The url of the MessageProducer.
185      * @param from The address the message will come from.
186      * @param targetInterface The interface the message will wrap to.
187      * @param targetURL The target url for the request.
188      * @exception RPCMessageClientException
189      */

190     public RPCMessageHandler(InitialContext JavaDoc context, String JavaDoc jndiURL,
191             String JavaDoc from, Class JavaDoc targetInterface, String JavaDoc targetURL)
192             throws RPCMessageClientException {
193         try {
194             this.targetInterface = targetInterface;
195             MessageProducer messageProducer = (MessageProducer)ConnectionManager
196                     .getInstance(context).getConnection(MessageProducer.class,
197                     jndiURL);
198             producer = messageProducer.createProducer(from);
199             this.targetURL = targetURL;
200             this.reply = false;
201         } catch (Exception JavaDoc ex) {
202             throw new RPCMessageClientException(
203                     "Failed to init the message handler : " + ex.getMessage(),
204                     ex);
205         }
206     }
207     
208     
209     /**
210      * Creates a new instance of RPCMessageHandler
211      *
212      * @param context The context that will be used to retrieve the service.
213      * @param jndiURL The url of the MessageProducer.
214      * @param from The address the message will come from.
215      * @param targetInterface The interface the message will wrap to.
216      * @param services The list of services the message will be sent to.
217      * @param broadcast True if the messages must be sent all the systems that
218      * supply the listed services.
219      * @exception RPCMessageClientException
220      */

221     public RPCMessageHandler(InitialContext JavaDoc context, String JavaDoc jndiURL,
222             String JavaDoc from, Class JavaDoc targetInterface, List JavaDoc services, boolean broadcast)
223             throws RPCMessageClientException {
224         try {
225             this.targetInterface = targetInterface;
226             MessageProducer messageProducer = (MessageProducer)ConnectionManager
227                     .getInstance(context).getConnection(MessageProducer.class,
228                     jndiURL);
229             producer = messageProducer.createProducer(from);
230             copyServices(services);
231             this.reply = false;
232             this.broadcast = broadcast;
233         } catch (Exception JavaDoc ex) {
234             throw new RPCMessageClientException(
235                     "Failed to init the message handler : " + ex.getMessage(),
236                     ex);
237         }
238     }
239     
240     
241     /**
242      * This method is responsible for handling the invocation request.
243      *
244      * @return The results.
245      * @param proxy The proxy that the call is being made on.
246      * @param method The method that the call has been made on.
247      * @param args The arguments passed to that method.
248      * @exception Throwable
249      */

250     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws
251             Throwable JavaDoc {
252         if (correlationId != null && usedCorrelationId) {
253             throw new RPCMessageClientException(
254                     "The correlation Id has been used cannot re-use this " +
255                     "object.");
256         }
257         usedCorrelationId = true;
258         try {
259             RPCMessage message = null;
260             if (targetURL != null) {
261                 message = producer.createRPCMessage(Message.POINT_TO_POINT);
262                 message.setTarget(targetURL);
263             } else if (broadcast == false) {
264                 message = producer.createRPCMessage(Message.POINT_TO_SERVICE);
265                 message.setServices(this.services);
266             } else {
267                 message = producer.createRPCMessage(
268                         Message.POINT_TO_MULTI_SERVICE);
269                 message.setServices(this.services);
270             }
271             message.setReply(reply);
272             message.setCorrelationId(correlationId);
273             
274             Method JavaDoc targetMethod = this.targetInterface.getMethod(
275                     method.getName(),method.getParameterTypes());
276             message.defineMethod(targetMethod.getReturnType(),
277                     method.getName(),method.getParameterTypes());
278             message.setArguments(args);
279             
280             producer.submit(message);
281             return message.getMessageId();
282         } catch (Exception JavaDoc ex) {
283             log.error("Failed to setup the RPC message because : " +
284                     ex.getMessage(),ex);
285             throw new RPCMessageClientException(
286                     "Failed to setup the RPC message because : " +
287                     ex.getMessage(),ex);
288         }
289     }
290     
291     
292     /**
293      * This method copyies the services to a string array.
294      */

295     private void copyServices(List JavaDoc serviceList) {
296         services = new String JavaDoc[serviceList.size()];
297         for (int index = 0; index < serviceList.size(); index++) {
298             services[index] = (String JavaDoc)serviceList.get(index);
299         }
300     }
301     
302 }
303
Popular Tags