KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: Xid.java,v 1.4 2005/04/14 03:58:19 laran Exp $
2

3 package org.jgroups.blocks;
4
5
6
7 import org.jgroups.Address;
8
9 import java.io.Externalizable JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInput JavaDoc;
12 import java.io.ObjectOutput JavaDoc;
13
14
15
16
17 /**
18  * Distributed transaction ID modeled after the JTA spec. This is used to
19  * identify entities of work, with which resources are associated. A transaction
20  * will thus trigger the creation of an associated entry on each receiver, which
21  * keeps track of resources acquired and their locks, operations to be committed
22  * in case {@link ReplicationReceiver#commit(Xid)}is called<br/>
23  * A transaction is identified by its creator and a transaction ID. The creator
24  * is currently a <a HREF="http://www.jgroups.com">JGroups</a> address,
25  * consisting of the IP address and port of the member.
26  * <br/><em>Note that this class might be replaced in the future with the real
27  * JTA counterpart.</em>
28  * @
29  * @author <a HREF="mailto:belaban@yahoo.com">Bela Ban</a>.
30  * @version $Revision: 1.4 $
31  *
32  * <p><b>Revisions:</b>
33  *
34  * <p>Dec 28 2002 Bela Ban: first implementation
35  */

36 public class Xid implements Externalizable JavaDoc {
37     protected Address creator=null;
38     protected long id=0;
39     protected int mode=DIRTY_READS;
40     protected static transient long next_id=0;
41     public static final String JavaDoc XID="xid";
42
43     /**
44      * Writes are serialized, but reads can be dirty, e.g. a data might have
45      * been changed while we read it. This is fast because we don't need to
46      * acquire locks for reads.
47      */

48     public static final int DIRTY_READS = 1;
49     
50     /**
51      * Reads are dirty until another transaction actually commits; at that
52      * points the modified data will be reflected here.
53      */

54     public static final int READ_COMMITTED = 2;
55     
56     /**
57      * Each read causes the data read to be copied to the private workspace, so
58      * subsequent reads always read the private data.
59      */

60     public static final int REPEATABLE_READ = 3;
61     
62     /**
63      * Reads and writes require locks. This is very costly, and is not
64      * recommended (and currently not implemented either :-)).
65      */

66     public static final int SERIALIZABLE = 4;
67
68
69     public Xid() {
70         ; // used by externalization
71
}
72
73     private Xid(Address creator, long id) {
74         this.creator=creator; this.id=id;
75     }
76
77     private Xid(Address creator, long id, int mode) {
78         this.creator=creator; this.id=id; this.mode=mode;
79     }
80
81     public Address getCreator() {return creator;}
82     public long getId() {return id;}
83     public long getMode() {return mode;}
84
85
86     public static Xid create(Address creator) throws Exception JavaDoc {
87         if(creator == null)
88             throw new Exception JavaDoc("Xid.create(): creator == null");
89         synchronized(Xid.class) {
90             return new Xid(creator, ++next_id);
91         }
92     }
93
94     public static Xid create(Address creator, int mode) throws Exception JavaDoc {
95         if(creator == null)
96             throw new Exception JavaDoc("Xid.create(): creator == null");
97         synchronized(Xid.class) {
98             return new Xid(creator, ++next_id, mode);
99         }
100     }
101
102     public static String JavaDoc modeToString(int m) {
103         switch(m) {
104         case DIRTY_READS: return "DIRTY_READS";
105         case READ_COMMITTED: return "READ_COMMITTED";
106         case REPEATABLE_READ: return "REPEATABLE_READ";
107         case SERIALIZABLE: return "SERIALIZABLE";
108         default: return "<unknown>";
109         }
110     }
111
112     public boolean equals(Object JavaDoc other) {
113         return compareTo(other) == 0;
114     }
115
116     public int hashCode() {
117         return creator.hashCode() + (int)id;
118     }
119
120     public int compareTo(Object JavaDoc o) {
121         Xid other;
122         int comp;
123         if(o == null || !(o instanceof Xid))
124             throw new ClassCastException JavaDoc("Xid.compareTo(): comparison between different classes");
125         other=(Xid)o;
126         comp=creator.compareTo(other.getCreator());
127         if(comp != 0) return comp;
128         if(id < other.getId()) return -1;
129         if(id > other.getId()) return 1;
130         return 0;
131     }
132
133     public String JavaDoc toString() {
134         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
135         sb.append('<').append(creator).append(">:").append(id);
136         return sb.toString();
137     }
138
139
140     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
141         out.writeObject(creator);
142         out.writeLong(id);
143         out.writeInt(mode);
144     }
145
146     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
147         creator=(Address)in.readObject();
148         id=in.readLong();
149         mode=in.readInt();
150     }
151
152
153
154 }
155
Popular Tags