1 23 package org.objectweb.joram.mom.messages; 24 25 import java.io.Serializable ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.IOException ; 29 import java.util.Vector ; 30 31 import org.objectweb.joram.shared.excepts.*; 32 33 import fr.dyade.aaa.util.Transaction; 34 import fr.dyade.aaa.agent.AgentServer; 35 36 import org.objectweb.joram.shared.JoramTracing; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 46 public final class Message implements Serializable { 47 48 transient public long order; 49 50 57 public transient int acksCounter; 58 65 public transient int durableAcksCounter; 66 67 public transient org.objectweb.joram.shared.messages.Message msg; 68 69 72 public Message(org.objectweb.joram.shared.messages.Message msg) { 73 this.msg = msg; 74 } 75 76 77 public void setIdentifier(String id) { 78 msg.id = id; 79 } 80 81 82 public void setPersistent(boolean persistent) { 83 msg.persistent = persistent; 84 } 85 86 91 public void setPriority(int priority) { 92 if (priority >= 0 && priority <= 9) 93 msg.priority = priority; 94 } 95 96 101 public void setExpiration(long expiration) { 102 if (expiration >= 0) 103 msg.expiration = expiration; 104 } 105 106 107 public void setTimestamp(long timestamp) { 108 msg.timestamp = timestamp; 109 } 110 111 121 122 public void setCorrelationId(String correlationId) { 123 msg.correlationId = correlationId; 124 } 125 126 136 137 public int getType() { 138 return msg.type; 139 } 140 141 142 public String getIdentifier() { 143 return msg.id; 144 } 145 146 147 public boolean getPersistent() { 148 return msg.persistent; 149 } 150 151 152 public int getPriority() { 153 return msg.priority; 154 } 155 156 157 public long getExpiration() { 158 return msg.expiration; 159 } 160 161 162 public long getTimestamp() { 163 return msg.timestamp; 164 } 165 166 171 176 177 public final String getCorrelationId() { 178 return msg.correlationId; 179 } 180 181 186 191 204 public void setObjectProperty(String name, Object value) throws MessageException { 205 if (name == null || name.equals("")) 206 throw new IllegalArgumentException ("Invalid property name: " + name); 207 208 if (value instanceof Boolean || 209 value instanceof Number || 210 value instanceof String ) { 211 msg.setProperty(name, value); 212 } else { 213 throw new MessageValueException("Can't set non primitive Java object" 214 + " as a property value."); 215 } 216 } 217 218 227 240 251 256 259 264 public boolean isValid(long currentTime) { 265 if (msg.expiration == 0) 266 return true; 267 268 return ((msg.expiration - currentTime) > 0); 269 } 270 271 281 282 transient String txname = null; 283 284 public void setTxName(String txname) { 285 this.txname = txname; 286 } 287 288 public String getTxName() { 289 return txname; 290 } 291 292 public static Message load(String txname) throws IOException , ClassNotFoundException { 293 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 294 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 295 "Message.load:" + txname); 296 297 Message msg = (Message) AgentServer.getTransaction().load(txname); 298 msg.txname = txname; 299 300 return msg; 301 } 302 303 public void save() { 304 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 305 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 306 "Message.save:" + txname); 307 308 if (! getPersistent()) return; 309 try { 310 AgentServer.getTransaction().save(this, txname); 311 } catch (IOException exc) { 312 JoramTracing.dbgMessage.log(BasicLevel.ERROR, 313 "Message named [" + txname + "] could not be saved", exc); 314 } 315 } 316 317 public void delete() { 318 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 319 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 320 "Message.delete:" + txname); 321 322 if (! getPersistent()) return; 323 AgentServer.getTransaction().delete(txname); 324 } 325 326 327 public static Vector loadAll(String msgTxname) { 328 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 329 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 330 "MessagePersistenceModule.loadAll() " + msgTxname); 331 332 Vector messages = new Vector (); 333 334 Transaction tx = AgentServer.getTransaction(); 336 String [] names = tx.getList(msgTxname); 337 338 for (int i = 0; i < names.length; i++) { 340 try { 341 Message msg = (Message) tx.load(names[i]); 342 msg.txname = names[i]; 343 344 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 345 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 346 "loadAll: names[" + i + "] = " + msg); 347 messages.add(msg); 348 } catch (Exception exc) { 349 JoramTracing.dbgMessage.log(BasicLevel.ERROR, 350 "Message named [" + names[i] + "] could not be loaded", exc); 351 } 352 } 353 return messages; 354 } 355 356 357 public static void deleteAll(String msgTxname) { 358 if (JoramTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 359 JoramTracing.dbgMessage.log(BasicLevel.DEBUG, 360 "MessagePersistenceModule.deleteAll() " + msgTxname); 361 362 Transaction tx = AgentServer.getTransaction(); 363 364 String [] names = tx.getList(msgTxname); 366 367 for (int i = 0; i < names.length; i++) { 369 tx.delete(names[i]); 370 } 371 } 372 373 377 388 399 402 403 private void writeObject(ObjectOutputStream out) throws IOException { 404 out.writeLong(order); 405 msg.writeTo(out); 409 } 410 411 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 412 order = in.readLong(); 413 acksCounter = 0; 416 durableAcksCounter = 0; 418 419 msg = new org.objectweb.joram.shared.messages.Message(); 420 msg.readFrom(in); 421 } 422 } 423 | Popular Tags |