001: /*
002: * MessageQueueClient: The message queue client library
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * RPCMessageImpl.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice.message;
024:
025: // java imports
026: import java.util.Date;
027: import java.util.List;
028:
029: // coadunation imports
030: import com.rift.coad.lib.common.ObjectSerializer;
031: import com.rift.coad.daemon.messageservice.MessageServiceException;
032: import com.rift.coad.daemon.messageservice.RPCMessage;
033: import com.rift.coad.daemon.messageservice.message.rpc.RPCXMLParser;
034:
035: /**
036: * The implementation of the RPC message object.
037: *
038: * @author Brett Chaldecott
039: */
040: public class RPCMessageImpl extends MessageImpl implements RPCMessage {
041:
042: // the classes private member variables
043: private String xmlBody = null;
044: private byte[] result = null;
045: private byte[] exception = null;
046: private RPCXMLParser parser = null;
047:
048: /**
049: * Creates a new instance of RPCMessageImpl
050: *
051: * @param messageId The unique identifier for this message.
052: * @param user The user of this message.
053: * @param sessionId The id of this user session.
054: * @param principals The list of principals assigned to this message.
055: * @param status The status of this message.
056: */
057: public RPCMessageImpl(String messageId, String user,
058: String sessionId, List principals, int status) {
059: super (messageId, user, sessionId, principals, status);
060: }
061:
062: /**
063: * Creates a new instance of RPCMessageImpl
064: *
065: * @param messageId The id of the message that was created.
066: * @param create The created time stamp.
067: * @param retries The number of retries of this message.
068: * @param processedDate The last time this message was processed.
069: * @param user The name of the user.
070: * @param sessionId The id of this user session.
071: * @param principals The list of principals.
072: * @param from The from address of the message.
073: * @param messageType The type of message being used.
074: * @param status The status of this message.
075: */
076: public RPCMessageImpl(String messageId, Date created, int retries,
077: Date processedDate, String user, String sessionId,
078: List principals, String from, int messageType, int status) {
079: super (messageId, created, retries, processedDate, user,
080: sessionId, principals, from, messageType, status);
081: }
082:
083: /**
084: * This method clears the body of the rpc message.
085: *
086: * @exception MessageServiceException
087: */
088: public void clearBody() throws MessageServiceException {
089: xmlBody = null;
090: }
091:
092: /**
093: * This method sets the XML body for the message.
094: *
095: * @param xml The string containing the formatted xml for the request.
096: * @exception MessageServiceException
097: */
098: public void setMethodBodyXML(String xml)
099: throws MessageServiceException {
100: this .xmlBody = xml;
101: }
102:
103: /**
104: * This method returns the XML body of the message.
105: *
106: * @return The string containing the formatted xml for the request.
107: * @exception MessageServiceException
108: */
109: public String getMethodBodyXML() throws MessageServiceException {
110: return xmlBody;
111: }
112:
113: /**
114: * This method sets the method information for
115: *
116: * @param returnType The return type for this message.
117: * @param name The name of this method.
118: * @param types The types that are arguments to this method.
119: * @exception MessageServiceException
120: */
121: public void defineMethod(Class returnType, String name,
122: Class[] types) throws MessageServiceException {
123: String returnTypeName = "void";
124: if (returnType != null) {
125: returnTypeName = returnType.getName();
126: }
127:
128: String typesString = "";
129: for (int index = 0; index < types.length; index++) {
130: typesString += String.format(
131: " <argument name=\"p%d\" type=\"%s\"/>\n",
132: index + 1, types[index].getName());
133: }
134: xmlBody = String
135: .format("<method name=\"%s\" type=\"%s\">\n" + "%s"
136: + "</method>", name, returnTypeName,
137: typesString);
138: }
139:
140: /**
141: * This method retrieves the return type of a method.
142: *
143: * @return The object containing the return type information.
144: * @exception MessageServiceException
145: */
146: public Object getReturnType() throws MessageServiceException {
147: parseXML();
148: return parser.getReturnType();
149: }
150:
151: /**
152: * This method returns the name of the method being wrapped.
153: *
154: * @return The string containing the method name.
155: * @exception MessageServiceException
156: */
157: public String getMethodName() throws MessageServiceException {
158: parseXML();
159: return parser.getMethodName();
160: }
161:
162: /**
163: * This method returns the argument types for this method.
164: *
165: * @return The list of arguments.
166: * @exception MessageServiceException
167: */
168: public Class[] getArgumentTypes() throws MessageServiceException {
169: parseXML();
170: return parser.getArgumentTypes();
171: }
172:
173: /**
174: * This method sets the arguments for this message.
175: *
176: * @param args The arguments to set.
177: * @exception MessageServiceException
178: */
179: public void setArguments(Object[] args)
180: throws MessageServiceException {
181: for (int index = 0; index < args.length; index++) {
182: String name = "p" + (index + 1);
183: this .setObjectProperty(name, args[index]);
184: }
185: }
186:
187: /**
188: * This method returns the arguments for a method.
189: *
190: * @return The list of arguments.
191: * @exception MessageServiceException.
192: */
193: public Object[] getArguments() throws MessageServiceException {
194: parseXML();
195: Object[] argumentTypes = parser.getArgumentTypes();
196: Object[] args = new Object[argumentTypes.length];
197: for (int index = 0; index < args.length; index++) {
198: String name = "p" + (index + 1);
199: args[index] = this .getObjectProperty(name);
200: }
201: return args;
202: }
203:
204: /**
205: * This method returns true if an exception was thrown.
206: *
207: * @return TRUE if and exception was generated, FALSE if not.
208: * @exception MessageServiceException
209: */
210: public boolean generatedException() throws MessageServiceException {
211: return (exception != null);
212: }
213:
214: /**
215: * This method returns the result of the RPC call.
216: *
217: * @return The object returns as a result of the asynchronis call.
218: * @exception MessageServiceException
219: */
220: public Object getResult() throws MessageServiceException {
221: if (result == null) {
222: return null;
223: }
224: try {
225: return ObjectSerializer.deserialize(result);
226: } catch (Exception ex) {
227: throw new MessageServiceException(
228: "Failed to deserialize the result ["
229: + ex.getMessage() + "]");
230: }
231: }
232:
233: /**
234: * This method returns the result of the RPC call.
235: *
236: * @return The object returns as a result of the asynchronis call.
237: * @exception MessageServiceException
238: */
239: public byte[] getResultBytes() throws MessageServiceException {
240: return result;
241: }
242:
243: /**
244: * This method is responsible for setting the result of the return.
245: *
246: * @param result The result of the message.
247: * @exception MessageServiceException
248: */
249: public void setResult(Object result) throws MessageServiceException {
250: try {
251: if (result != null) {
252: this .result = ObjectSerializer.serialize(result);
253: } else {
254: this .result = null;
255: }
256: } catch (Exception ex) {
257: throw new MessageServiceException(
258: "Failed to set the result [" + ex.getMessage()
259: + "]");
260: }
261: }
262:
263: /**
264: * This method is responsible for setting the result of the return.
265: *
266: * @param result The bytes containing the result
267: * @exception MessageServiceException
268: */
269: public void setResultBytes(byte[] result)
270: throws MessageServiceException {
271: this .result = result;
272: }
273:
274: /**
275: * This method returns the exception that got thrown while processing this
276: * RPC message.
277: *
278: * @return The exception that got thrown while processing this message.
279: * @exception MessageServiceException
280: */
281: public Throwable getThrowable() throws MessageServiceException {
282: if (exception == null) {
283: return null;
284: }
285: try {
286: return (Throwable) ObjectSerializer.deserialize(exception);
287: } catch (Exception ex) {
288: throw new MessageServiceException(
289: "Failed to deserialize the exception ["
290: + ex.getMessage() + "]", ex);
291: }
292: }
293:
294: /**
295: * This method returns the exception that got thrown while processing this
296: * RPC message.
297: *
298: * @return The bytes representing the serialized exception
299: * @exception MessageServiceException
300: */
301: public byte[] getThrowableBytes() throws MessageServiceException {
302: return exception;
303: }
304:
305: /**
306: * This method returns the exception that got thrown while processing this
307: * RPC message.
308: *
309: * @param throwable The throwable exception to set.
310: * @exception MessageServiceException
311: */
312: public void setThrowable(Throwable throwable)
313: throws MessageServiceException {
314: try {
315: this .exception = ObjectSerializer.serialize(throwable);
316: } catch (Exception ex) {
317: throw new MessageServiceException(
318: "Failed to set the exception [" + ex.getMessage()
319: + "]");
320: }
321: }
322:
323: /**
324: * This method sets the throwable bytes of the exception.
325: *
326: * @param exception The exception bytes.
327: * @exception MessageServiceException
328: */
329: public void setThrowableBytes(byte[] exception)
330: throws MessageServiceException {
331: this .exception = exception;
332: }
333:
334: /**
335: * This method is responsible for parsing XML.
336: *
337: * @exception MessageServiceException
338: */
339: private void parseXML() throws MessageServiceException {
340: if (parser != null) {
341: return;
342: }
343: if (xmlBody == null) {
344: throw new MessageServiceException(
345: "The xml body has not been setup.");
346: }
347: try {
348: parser = new RPCXMLParser(xmlBody);
349: } catch (Exception ex) {
350: throw new MessageServiceException("Failed parse the xml : "
351: + ex.getMessage(), ex);
352: }
353: }
354: }
|