KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > message > RPCMessageImpl


1 /*
2  * MessageQueueClient: The message queue client library
3  * Copyright (C) 2006 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  * RPCMessageImpl.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.message;
24
25 // java imports
26
import java.util.Date JavaDoc;
27 import java.util.List JavaDoc;
28
29 // coadunation imports
30
import com.rift.coad.lib.common.ObjectSerializer;
31 import com.rift.coad.daemon.messageservice.MessageServiceException;
32 import com.rift.coad.daemon.messageservice.RPCMessage;
33 import com.rift.coad.daemon.messageservice.message.rpc.RPCXMLParser;
34
35 /**
36  * The implementation of the RPC message object.
37  *
38  * @author Brett Chaldecott
39  */

40 public class RPCMessageImpl extends MessageImpl implements RPCMessage {
41     
42     // the classes private member variables
43
private String JavaDoc xmlBody = null;
44     private byte[] result = null;
45     private byte[] exception = null;
46     private RPCXMLParser parser = null;
47     
48     /**
49      * Creates a new instance of RPCMessageImpl
50      *
51      * @param messageId The unique identifier for this message.
52      * @param user The user of this message.
53      * @param sessionId The id of this user session.
54      * @param principals The list of principals assigned to this message.
55      * @param status The status of this message.
56      */

57     public RPCMessageImpl(String JavaDoc messageId, String JavaDoc user, String JavaDoc sessionId,
58             List JavaDoc principals, int status) {
59         super(messageId,user,sessionId,principals,status);
60     }
61     
62     
63     /**
64      * Creates a new instance of RPCMessageImpl
65      *
66      * @param messageId The id of the message that was created.
67      * @param create The created time stamp.
68      * @param retries The number of retries of this message.
69      * @param processedDate The last time this message was processed.
70      * @param user The name of the user.
71      * @param sessionId The id of this user session.
72      * @param principals The list of principals.
73      * @param from The from address of the message.
74      * @param messageType The type of message being used.
75      * @param status The status of this message.
76      */

77     public RPCMessageImpl(String JavaDoc messageId, Date JavaDoc created, int retries,
78             Date JavaDoc processedDate,String JavaDoc user, String JavaDoc sessionId, List JavaDoc principals,
79             String JavaDoc from, int messageType, int status) {
80         super(messageId,created,retries,processedDate,user,sessionId,principals,
81                 from,messageType, status);
82     }
83     
84     
85     /**
86      * This method clears the body of the rpc message.
87      *
88      * @exception MessageServiceException
89      */

90     public void clearBody() throws MessageServiceException {
91         xmlBody = null;
92     }
93     
94     
95     /**
96      * This method sets the XML body for the message.
97      *
98      * @param xml The string containing the formatted xml for the request.
99      * @exception MessageServiceException
100      */

101     public void setMethodBodyXML(String JavaDoc xml) throws MessageServiceException {
102         this.xmlBody = xml;
103     }
104     
105     
106     /**
107      * This method returns the XML body of the message.
108      *
109      * @return The string containing the formatted xml for the request.
110      * @exception MessageServiceException
111      */

112     public String JavaDoc getMethodBodyXML() throws MessageServiceException {
113         return xmlBody;
114     }
115     
116     
117     /**
118      * This method sets the method information for
119      *
120      * @param returnType The return type for this message.
121      * @param name The name of this method.
122      * @param types The types that are arguments to this method.
123      * @exception MessageServiceException
124      */

125     public void defineMethod(Class JavaDoc returnType, String JavaDoc name, Class JavaDoc[] types)
126     throws MessageServiceException {
127         String JavaDoc returnTypeName = "void";
128         if (returnType != null) {
129             returnTypeName = returnType.getName();
130         }
131         
132         String JavaDoc typesString = "";
133         for (int index = 0; index < types.length; index++) {
134             typesString += String.format(
135                     " <argument name=\"p%d\" type=\"%s\"/>\n",index + 1,
136                     types[index].getName());
137         }
138         xmlBody = String.format(
139                 "<method name=\"%s\" type=\"%s\">\n" +
140                 "%s" +
141                 "</method>",name,returnTypeName,typesString);
142     }
143     
144     
145     /**
146      * This method retrieves the return type of a method.
147      *
148      * @return The object containing the return type information.
149      * @exception MessageServiceException
150      */

151     public Object JavaDoc getReturnType() throws MessageServiceException {
152         parseXML();
153         return parser.getReturnType();
154     }
155     
156     
157     /**
158      * This method returns the name of the method being wrapped.
159      *
160      * @return The string containing the method name.
161      * @exception MessageServiceException
162      */

163     public String JavaDoc getMethodName() throws MessageServiceException {
164         parseXML();
165         return parser.getMethodName();
166     }
167     
168     
169     /**
170      * This method returns the argument types for this method.
171      *
172      * @return The list of arguments.
173      * @exception MessageServiceException
174      */

175     public Class JavaDoc[] getArgumentTypes() throws MessageServiceException {
176         parseXML();
177         return parser.getArgumentTypes();
178     }
179     
180     
181     /**
182      * This method sets the arguments for this message.
183      *
184      * @param args The arguments to set.
185      * @exception MessageServiceException
186      */

187     public void setArguments(Object JavaDoc[] args) throws MessageServiceException {
188         for (int index = 0; index < args.length; index++) {
189             String JavaDoc name = "p" + (index + 1);
190             this.setObjectProperty(name,args[index]);
191         }
192     }
193     
194     
195     /**
196      * This method returns the arguments for a method.
197      *
198      * @return The list of arguments.
199      * @exception MessageServiceException.
200      */

201     public Object JavaDoc[] getArguments() throws MessageServiceException {
202         parseXML();
203         Object JavaDoc[] argumentTypes = parser.getArgumentTypes();
204         Object JavaDoc[] args = new Object JavaDoc[argumentTypes.length];
205         for (int index = 0; index < args.length; index++) {
206             String JavaDoc name = "p" + (index + 1);
207             args[index] = this.getObjectProperty(name);
208         }
209         return args;
210     }
211     
212     
213     /**
214      * This method returns true if an exception was thrown.
215      *
216      * @return TRUE if and exception was generated, FALSE if not.
217      * @exception MessageServiceException
218      */

219     public boolean generatedException() throws MessageServiceException {
220         return (exception != null);
221     }
222     
223     
224     /**
225      * This method returns the result of the RPC call.
226      *
227      * @return The object returns as a result of the asynchronis call.
228      * @exception MessageServiceException
229      */

230     public Object JavaDoc getResult() throws MessageServiceException {
231         if (result == null) {
232             return null;
233         }
234         try {
235             return ObjectSerializer.deserialize(result);
236         } catch (Exception JavaDoc ex) {
237             throw new MessageServiceException(
238                     "Failed to deserialize the result [" + ex.getMessage() +
239                     "]");
240         }
241     }
242     
243     
244     /**
245      * This method returns the result of the RPC call.
246      *
247      * @return The object returns as a result of the asynchronis call.
248      * @exception MessageServiceException
249      */

250     public byte[] getResultBytes() throws MessageServiceException {
251         return result;
252     }
253     
254     
255     /**
256      * This method is responsible for setting the result of the return.
257      *
258      * @param result The result of the message.
259      * @exception MessageServiceException
260      */

261     public void setResult(Object JavaDoc result) throws MessageServiceException {
262         try {
263             if (result != null) {
264                 this.result = ObjectSerializer.serialize(result);
265             } else {
266                 this.result = null;
267             }
268         } catch (Exception JavaDoc ex) {
269             throw new MessageServiceException("Failed to set the result [" +
270                     ex.getMessage() + "]");
271         }
272     }
273     
274     
275     /**
276      * This method is responsible for setting the result of the return.
277      *
278      * @param result The bytes containing the result
279      * @exception MessageServiceException
280      */

281     public void setResultBytes(byte[] result) throws MessageServiceException {
282         this.result = result;
283     }
284     
285     
286     /**
287      * This method returns the exception that got thrown while processing this
288      * RPC message.
289      *
290      * @return The exception that got thrown while processing this message.
291      * @exception MessageServiceException
292      */

293     public Throwable JavaDoc getThrowable() throws MessageServiceException {
294         if (exception == null) {
295             return null;
296         }
297         try {
298             return (Throwable JavaDoc)ObjectSerializer.deserialize(exception);
299         } catch (Exception JavaDoc ex) {
300             throw new MessageServiceException(
301                     "Failed to deserialize the exception [" + ex.getMessage() +
302                     "]",ex);
303         }
304     }
305     
306     /**
307      * This method returns the exception that got thrown while processing this
308      * RPC message.
309      *
310      * @return The bytes representing the serialized exception
311      * @exception MessageServiceException
312      */

313     public byte[] getThrowableBytes() throws MessageServiceException {
314         return exception;
315     }
316     
317     
318     /**
319      * This method returns the exception that got thrown while processing this
320      * RPC message.
321      *
322      * @param throwable The throwable exception to set.
323      * @exception MessageServiceException
324      */

325     public void setThrowable(Throwable JavaDoc throwable) throws
326             MessageServiceException {
327         try {
328             this.exception = ObjectSerializer.serialize(throwable);
329         } catch (Exception JavaDoc ex) {
330             throw new MessageServiceException("Failed to set the exception [" +
331                     ex.getMessage() + "]");
332         }
333     }
334     
335     
336     /**
337      * This method sets the throwable bytes of the exception.
338      *
339      * @param exception The exception bytes.
340      * @exception MessageServiceException
341      */

342     public void setThrowableBytes(byte[] exception) throws
343             MessageServiceException {
344         this.exception = exception;
345     }
346     
347     
348     /**
349      * This method is responsible for parsing XML.
350      *
351      * @exception MessageServiceException
352      */

353     private void parseXML() throws MessageServiceException {
354         if (parser != null) {
355             return;
356         }
357         if (xmlBody == null) {
358             throw new MessageServiceException(
359                     "The xml body has not been setup.");
360         }
361         try {
362             parser = new RPCXMLParser(xmlBody);
363         } catch (Exception JavaDoc ex) {
364             throw new MessageServiceException("Failed parse the xml : " +
365                     ex.getMessage(),ex);
366         }
367     }
368 }
369
Popular Tags