1 21 22 package com.rift.coad.daemon.messageservice.message; 24 25 import java.util.Date ; 27 import java.util.List ; 28 29 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 40 public class RPCMessageImpl extends MessageImpl implements RPCMessage { 41 42 private String xmlBody = null; 44 private byte[] result = null; 45 private byte[] exception = null; 46 private RPCXMLParser parser = null; 47 48 57 public RPCMessageImpl(String messageId, String user, String sessionId, 58 List principals, int status) { 59 super(messageId,user,sessionId,principals,status); 60 } 61 62 63 77 public RPCMessageImpl(String messageId, Date created, int retries, 78 Date processedDate,String user, String sessionId, List principals, 79 String from, int messageType, int status) { 80 super(messageId,created,retries,processedDate,user,sessionId,principals, 81 from,messageType, status); 82 } 83 84 85 90 public void clearBody() throws MessageServiceException { 91 xmlBody = null; 92 } 93 94 95 101 public void setMethodBodyXML(String xml) throws MessageServiceException { 102 this.xmlBody = xml; 103 } 104 105 106 112 public String getMethodBodyXML() throws MessageServiceException { 113 return xmlBody; 114 } 115 116 117 125 public void defineMethod(Class returnType, String name, Class [] types) 126 throws MessageServiceException { 127 String returnTypeName = "void"; 128 if (returnType != null) { 129 returnTypeName = returnType.getName(); 130 } 131 132 String 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 151 public Object getReturnType() throws MessageServiceException { 152 parseXML(); 153 return parser.getReturnType(); 154 } 155 156 157 163 public String getMethodName() throws MessageServiceException { 164 parseXML(); 165 return parser.getMethodName(); 166 } 167 168 169 175 public Class [] getArgumentTypes() throws MessageServiceException { 176 parseXML(); 177 return parser.getArgumentTypes(); 178 } 179 180 181 187 public void setArguments(Object [] args) throws MessageServiceException { 188 for (int index = 0; index < args.length; index++) { 189 String name = "p" + (index + 1); 190 this.setObjectProperty(name,args[index]); 191 } 192 } 193 194 195 201 public Object [] getArguments() throws MessageServiceException { 202 parseXML(); 203 Object [] argumentTypes = parser.getArgumentTypes(); 204 Object [] args = new Object [argumentTypes.length]; 205 for (int index = 0; index < args.length; index++) { 206 String name = "p" + (index + 1); 207 args[index] = this.getObjectProperty(name); 208 } 209 return args; 210 } 211 212 213 219 public boolean generatedException() throws MessageServiceException { 220 return (exception != null); 221 } 222 223 224 230 public Object getResult() throws MessageServiceException { 231 if (result == null) { 232 return null; 233 } 234 try { 235 return ObjectSerializer.deserialize(result); 236 } catch (Exception ex) { 237 throw new MessageServiceException( 238 "Failed to deserialize the result [" + ex.getMessage() + 239 "]"); 240 } 241 } 242 243 244 250 public byte[] getResultBytes() throws MessageServiceException { 251 return result; 252 } 253 254 255 261 public void setResult(Object 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 ex) { 269 throw new MessageServiceException("Failed to set the result [" + 270 ex.getMessage() + "]"); 271 } 272 } 273 274 275 281 public void setResultBytes(byte[] result) throws MessageServiceException { 282 this.result = result; 283 } 284 285 286 293 public Throwable getThrowable() throws MessageServiceException { 294 if (exception == null) { 295 return null; 296 } 297 try { 298 return (Throwable )ObjectSerializer.deserialize(exception); 299 } catch (Exception ex) { 300 throw new MessageServiceException( 301 "Failed to deserialize the exception [" + ex.getMessage() + 302 "]",ex); 303 } 304 } 305 306 313 public byte[] getThrowableBytes() throws MessageServiceException { 314 return exception; 315 } 316 317 318 325 public void setThrowable(Throwable throwable) throws 326 MessageServiceException { 327 try { 328 this.exception = ObjectSerializer.serialize(throwable); 329 } catch (Exception ex) { 330 throw new MessageServiceException("Failed to set the exception [" + 331 ex.getMessage() + "]"); 332 } 333 } 334 335 336 342 public void setThrowableBytes(byte[] exception) throws 343 MessageServiceException { 344 this.exception = exception; 345 } 346 347 348 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 ex) { 364 throw new MessageServiceException("Failed parse the xml : " + 365 ex.getMessage(),ex); 366 } 367 } 368 } 369 | Popular Tags |