001: // $Id: ReplicationData.java,v 1.3 2004/07/05 05:41:45 belaban Exp $
002:
003: package org.jgroups.blocks;
004:
005: import java.io.Externalizable;
006: import java.io.IOException;
007: import java.io.ObjectInput;
008: import java.io.ObjectOutput;
009:
010: /**
011: * Class used for data exchange by ReplicationManager and ReplicationReceiver.
012: * @author Bela Ban
013: */
014: public class ReplicationData implements Externalizable {
015: public static final int SEND = 1;
016: public static final int COMMIT = 2;
017: public static final int ROLLBACK = 3;
018:
019: int type = 0;
020: byte[] data = null;
021: Xid transaction = null;
022: byte[] lock_info = null;
023: long lock_acquisition_timeout = 0;
024: long lock_lease_timeout = 0;
025: boolean use_locks = false;
026:
027: public ReplicationData() {
028: ; // used by externalization
029: }
030:
031: public ReplicationData(int type, byte[] data, Xid transaction,
032: byte[] lock_info, long lock_acquisition_timeout,
033: long lock_lease_timeout, boolean use_locks) {
034: this .type = type;
035: this .data = data;
036: this .transaction = transaction;
037: this .lock_info = lock_info;
038: this .lock_acquisition_timeout = lock_acquisition_timeout;
039: this .lock_lease_timeout = lock_lease_timeout;
040: this .use_locks = use_locks;
041: }
042:
043: public int getType() {
044: return type;
045: }
046:
047: public byte[] getData() {
048: return data;
049: }
050:
051: public Xid getTransaction() {
052: return transaction;
053: }
054:
055: public byte[] getLockInfo() {
056: return lock_info;
057: }
058:
059: public long getLockAcquisitionTimeout() {
060: return lock_acquisition_timeout;
061: }
062:
063: public long getLockLeaseTimeout() {
064: return lock_lease_timeout;
065: }
066:
067: public boolean useLocks() {
068: return use_locks;
069: }
070:
071: public String toString() {
072: StringBuffer sb = new StringBuffer();
073: sb.append(typeToString(type)).append(" [").append(
074: ", transaction=").append(transaction);
075: switch (type) {
076: case SEND:
077: if (data != null)
078: sb.append(", data=").append(data.length).append(
079: " bytes");
080: sb.append(", lock_acquisition_timeout=").append(
081: lock_acquisition_timeout);
082: sb.append(", lock_lease_timeout=").append(
083: lock_lease_timeout);
084: sb.append(", use_locks=").append(use_locks);
085: break;
086: case COMMIT:
087: case ROLLBACK:
088: break;
089: }
090: sb.append(']');
091: return sb.toString();
092: }
093:
094: public static String typeToString(int t) {
095: switch (t) {
096: case SEND:
097: return "SEND";
098: case COMMIT:
099: return "COMMIT";
100: case ROLLBACK:
101: return "ROLLBACK";
102: default:
103: return "<unknown>";
104: }
105: }
106:
107: public void writeExternal(ObjectOutput out) throws IOException {
108: out.writeInt(type);
109: if (data != null) {
110: out.writeInt(data.length);
111: out.write(data, 0, data.length);
112: } else
113: out.writeInt(0);
114: if (transaction != null) {
115: out.writeBoolean(true);
116: transaction.writeExternal(out);
117: } else
118: out.writeBoolean(false);
119: if (use_locks) {
120: out.writeBoolean(true);
121: if (lock_info != null) {
122: out.writeInt(lock_info.length);
123: out.write(lock_info, 0, lock_info.length);
124: } else
125: out.writeInt(0);
126: out.writeLong(lock_acquisition_timeout);
127: out.writeLong(lock_lease_timeout);
128: } else
129: out.writeBoolean(false);
130: }
131:
132: public void readExternal(ObjectInput in) throws IOException,
133: ClassNotFoundException {
134: int num;
135: type = in.readInt();
136: if ((num = in.readInt()) > 0) {
137: data = new byte[num];
138: in.readFully(data, 0, num);
139: }
140: if (in.readBoolean()) {
141: transaction = new Xid();
142: transaction.readExternal(in);
143: }
144: use_locks = in.readBoolean();
145: if (use_locks) {
146: if ((num = in.readInt()) > 0) {
147: lock_info = new byte[num];
148: in.readFully(lock_info, 0, num);
149: }
150: lock_acquisition_timeout = in.readLong();
151: lock_lease_timeout = in.readLong();
152: }
153: }
154: }
|