|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ApplicationMessage.java | 50% | 60,9% | 71,4% | 63,4% |
|
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 | 3 |
public ApplicationMessage() {
|
43 | 3 |
super();
|
44 | 3 |
headers.put(TIMESTAMP_CREATED, new Date( ) );
|
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
*
|
|
49 |
*/
|
|
50 | 0 |
public ApplicationMessage(String sessionId, Serializable body) {
|
51 | 0 |
super();
|
52 | 0 |
headers.put(TIMESTAMP_CREATED, new Date( ) );
|
53 | 0 |
setSessionID(sessionId); |
54 | 0 |
setBody(body); |
55 |
} |
|
56 |
|
|
57 |
/**
|
|
58 |
* This Message body.
|
|
59 |
*
|
|
60 |
* @return
|
|
61 |
*/
|
|
62 | 2 |
public Object getBody() {
|
63 | 2 |
return body;
|
64 |
} |
|
65 |
|
|
66 |
/**
|
|
67 |
* The user sessionID.
|
|
68 |
*
|
|
69 |
* @return
|
|
70 |
*/
|
|
71 | 0 |
public String getSessionID() {
|
72 | 0 |
return (String)headers.get(SESSION_ID);
|
73 |
} |
|
74 |
|
|
75 | 3 |
public void putHeader(Serializable key, Serializable value){ |
76 | 3 |
headers.put(key, value); |
77 |
} |
|
78 |
|
|
79 | 4 |
public Object getHeader( Object key ){
|
80 | 4 |
return headers.get( key);
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* When was this message created.
|
|
85 |
*
|
|
86 |
* @return
|
|
87 |
*/
|
|
88 | 0 |
public Date getTimestampCreated() {
|
89 | 0 |
return (Date)headers.get(TIMESTAMP_CREATED);
|
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* @param object the message body.
|
|
94 |
*/
|
|
95 | 1 |
public void setBody(Serializable object) { |
96 | 1 |
body = object; |
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* @param string the user session id.
|
|
101 |
*/
|
|
102 | 1 |
public void setSessionID(String string) { |
103 | 1 |
headers.put( SESSION_ID, string); |
104 |
} |
|
105 |
|
|
106 | 3 |
public void setOriginator(int value){ |
107 |
|
|
108 | 3 |
if( value != SERVER_MODULE && value != DATABASE_MODULE )
|
109 | 0 |
throw new IllegalArgumentException("Invalid originator value : [" + value + "]."); |
110 |
|
|
111 | 3 |
putHeader( ORIGINATOR, new Integer(value) );
|
112 |
} |
|
113 |
|
|
114 | 4 |
public int getOriginator(){ |
115 | 4 |
Object value = getHeader(ORIGINATOR); |
116 |
|
|
117 | 4 |
if( value != null ) |
118 | 4 |
return ((Integer)value).intValue();
|
119 |
|
|
120 | 0 |
throw new IllegalArgumentException("No originator set."); |
121 |
} |
|
122 |
|
|
123 | 4 |
public boolean isFromServer(){ |
124 | 4 |
return getOriginator() == SERVER_MODULE;
|
125 |
} |
|
126 |
|
|
127 | 0 |
public boolean isFromDatabase(){ |
128 | 0 |
return getOriginator() == DATABASE_MODULE;
|
129 |
} |
|
130 |
|
|
131 | 2 |
public String toString(){
|
132 | 2 |
return "From "+(isFromServer()?"server":"database") |
133 |
+". Size:"
|
|
134 |
+ (getBody()==null?0:getBody().toString().length())
|
|
135 |
+ ".";
|
|
136 |
} |
|
137 |
} |
|
138 |
|
|