1 /* 2 * Replica is published under the terms 3 * of the Apache Software License. 4 */ 5 package replica.core; 6 7 import java.io.Serializable; 8 import java.util.Date; 9 import java.util.Hashtable; 10 11 /*** 12 * This class models a message sent trhouout the application. 13 * 14 * It adds some required header fields to the message body it self, like 15 * the user sessionID and the timestamp. 16 * 17 * This can be for instance, a database command send by the server 18 * to the database. 19 * 20 * It implements the Serializable interface so that it be sent to other 21 * group members. 22 * 23 * @author Pedro Costa 24 * @author Helder Silva 25 * @since 20/Jan/2004 26 */ 27 public class ApplicationMessage implements Serializable{ 28 29 public final static String SESSION_ID = "SESSION_ID"; 30 public final static String TIMESTAMP_CREATED = "TIMESTAMP_CREATED"; 31 public final static String ORIGINATOR = "ORIGINATOR"; 32 33 public final static int SERVER_MODULE = 0; 34 public final static int DATABASE_MODULE = 1; 35 36 private Hashtable headers = new Hashtable(); 37 private Object body; 38 39 /*** 40 * 41 */ 42 public ApplicationMessage() { 43 super(); 44 headers.put(TIMESTAMP_CREATED, new Date( ) ); 45 } 46 47 /*** 48 * 49 */ 50 public ApplicationMessage(String sessionId, Serializable body) { 51 super(); 52 headers.put(TIMESTAMP_CREATED, new Date( ) ); 53 setSessionID(sessionId); 54 setBody(body); 55 } 56 57 /*** 58 * This Message body. 59 * 60 * @return 61 */ 62 public Object getBody() { 63 return body; 64 } 65 66 /*** 67 * The user sessionID. 68 * 69 * @return 70 */ 71 public String getSessionID() { 72 return (String)headers.get(SESSION_ID); 73 } 74 75 public void putHeader(Serializable key, Serializable value){ 76 headers.put(key, value); 77 } 78 79 public Object getHeader( Object key ){ 80 return headers.get( key); 81 } 82 83 /*** 84 * When was this message created. 85 * 86 * @return 87 */ 88 public Date getTimestampCreated() { 89 return (Date)headers.get(TIMESTAMP_CREATED); 90 } 91 92 /*** 93 * @param object the message body. 94 */ 95 public void setBody(Serializable object) { 96 body = object; 97 } 98 99 /*** 100 * @param string the user session id. 101 */ 102 public void setSessionID(String string) { 103 headers.put( SESSION_ID, string); 104 } 105 106 public void setOriginator(int value){ 107 108 if( value != SERVER_MODULE && value != DATABASE_MODULE ) 109 throw new IllegalArgumentException("Invalid originator value : [" + value + "]."); 110 111 putHeader( ORIGINATOR, new Integer(value) ); 112 } 113 114 public int getOriginator(){ 115 Object value = getHeader(ORIGINATOR); 116 117 if( value != null ) 118 return ((Integer)value).intValue(); 119 120 throw new IllegalArgumentException("No originator set."); 121 } 122 123 public boolean isFromServer(){ 124 return getOriginator() == SERVER_MODULE; 125 } 126 127 public boolean isFromDatabase(){ 128 return getOriginator() == DATABASE_MODULE; 129 } 130 131 public String toString(){ 132 return "From "+(isFromServer()?"server":"database") 133 +". Size:" 134 + (getBody()==null?0:getBody().toString().length()) 135 + "."; 136 } 137 }

This page was automatically generated by Maven