KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > ReplicationData


1 // $Id: ReplicationData.java,v 1.3 2004/07/05 05:41:45 belaban Exp $
2

3 package org.jgroups.blocks;
4
5 import java.io.Externalizable JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.ObjectInput JavaDoc;
8 import java.io.ObjectOutput JavaDoc;
9
10
11
12 /**
13  * Class used for data exchange by ReplicationManager and ReplicationReceiver.
14  * @author Bela Ban
15  */

16 public class ReplicationData implements Externalizable JavaDoc {
17     public static final int SEND = 1;
18     public static final int COMMIT = 2;
19     public static final int ROLLBACK = 3;
20
21     
22     int type=0;
23     byte[] data=null;
24     Xid transaction=null;
25     byte[] lock_info=null;
26     long lock_acquisition_timeout=0;
27     long lock_lease_timeout=0;
28     boolean use_locks=false;
29         
30
31     public ReplicationData() {
32         ; // used by externalization
33
}
34
35     
36     public ReplicationData(int type,
37                            byte[] data,
38                            Xid transaction,
39                            byte[] lock_info,
40                            long lock_acquisition_timeout,
41                            long lock_lease_timeout,
42                            boolean use_locks) {
43         this.type=type;
44         this.data=data;
45         this.transaction=transaction;
46         this.lock_info=lock_info;
47         this.lock_acquisition_timeout=lock_acquisition_timeout;
48         this.lock_lease_timeout=lock_lease_timeout;
49         this.use_locks=use_locks;
50     }
51
52     
53     public int getType() {return type;}
54     public byte[] getData() {return data;}
55     public Xid getTransaction() {return transaction;}
56     public byte[] getLockInfo() {return lock_info;}
57     public long getLockAcquisitionTimeout() {return lock_acquisition_timeout;}
58     public long getLockLeaseTimeout() {return lock_lease_timeout;}
59     public boolean useLocks() {return use_locks;}
60
61
62     public String JavaDoc toString() {
63         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
64         sb.append(typeToString(type)).append(" [").append(", transaction=").append(transaction);
65         switch(type) {
66         case SEND:
67             if(data != null)
68                 sb.append(", data=").append(data.length).append(" bytes");
69             sb.append(", lock_acquisition_timeout=").append(lock_acquisition_timeout);
70             sb.append(", lock_lease_timeout=").append(lock_lease_timeout);
71             sb.append(", use_locks=").append(use_locks);
72             break;
73         case COMMIT:
74         case ROLLBACK:
75             break;
76         }
77         sb.append(']');
78         return sb.toString();
79     }
80
81
82     public static String JavaDoc typeToString(int t) {
83         switch(t) {
84         case SEND: return "SEND";
85         case COMMIT: return "COMMIT";
86         case ROLLBACK: return "ROLLBACK";
87         default: return "<unknown>";
88         }
89     }
90
91
92     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
93         out.writeInt(type);
94         if(data != null) {
95             out.writeInt(data.length);
96             out.write(data, 0, data.length);
97         }
98         else
99             out.writeInt(0);
100         if(transaction != null) {
101             out.writeBoolean(true);
102             transaction.writeExternal(out);
103         }
104         else
105             out.writeBoolean(false);
106         if(use_locks) {
107             out.writeBoolean(true);
108             if(lock_info != null) {
109                 out.writeInt(lock_info.length);
110                 out.write(lock_info, 0, lock_info.length);
111             }
112             else
113                 out.writeInt(0);
114             out.writeLong(lock_acquisition_timeout);
115             out.writeLong(lock_lease_timeout);
116         }
117         else
118             out.writeBoolean(false);
119     }
120     
121     
122     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
123         int num;
124         type=in.readInt();
125         if((num=in.readInt()) > 0) {
126             data=new byte[num];
127             in.readFully(data, 0, num);
128         }
129         if(in.readBoolean()) {
130             transaction=new Xid();
131             transaction.readExternal(in);
132         }
133         use_locks=in.readBoolean();
134         if(use_locks) {
135             if((num=in.readInt()) > 0) {
136                 lock_info=new byte[num];
137                 in.readFully(lock_info, 0, num);
138             }
139             lock_acquisition_timeout=in.readLong();
140             lock_lease_timeout=in.readLong();
141         }
142     }
143 }
144
Popular Tags