KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > GlobalTransaction


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache;
8
9
10 import org.jgroups.Address;
11
12 import java.io.Externalizable JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInput JavaDoc;
15 import java.io.ObjectOutput JavaDoc;
16
17
18 /**
19  * Uniquely identifies a transaction that spans all nodes in a cluster. This is used when
20  * replicating all modifications in a transaction; the PREPARE and COMMIT (or ROLLBACK)
21  * messages have to have a unique identifier to associate the changes with<br>
22  *
23  * @author <a HREF="mailto:bela@jboss.org">Bela Ban</a> Apr 12, 2003
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  * @version $Revision: 1.18 $
26  */

27 public class GlobalTransaction implements Externalizable JavaDoc
28 {
29
30    private static final long serialVersionUID = 8011434781266976149L;
31
32    private static long sid = 0;
33
34    private Address addr = null;
35    private long id = -1;
36    private transient boolean remote = false;
37
38    // cache the hashcode
39
private transient int hash_code = -1; // in the worst case, hashCode() returns 0, then increases, so we're safe here
40

41    /**
42     * empty ctor used by externalization
43     */

44    public GlobalTransaction()
45    {
46    }
47
48    private GlobalTransaction(Address addr)
49    {
50       this.addr = addr;
51       id = newId();
52    }
53
54    private static synchronized long newId()
55    {
56       return ++sid;
57    }
58
59    public static GlobalTransaction create(Address addr)
60    {
61       return new GlobalTransaction(addr);
62    }
63
64    public Object JavaDoc getAddress()
65    {
66       return addr;
67    }
68
69    public void setAddress(Address address)
70    {
71       addr = address;
72    }
73
74    public long getId()
75    {
76       return id;
77    }
78
79    public int hashCode()
80    {
81       if (hash_code == -1)
82       {
83          hash_code = (addr != null ? addr.hashCode() : 0) + (int) id;
84       }
85       return hash_code;
86    }
87
88    public boolean equals(Object JavaDoc other)
89    {
90       if (this == other)
91          return true;
92       if (!(other instanceof GlobalTransaction))
93          return false;
94
95       GlobalTransaction otherGtx = (GlobalTransaction) other;
96
97       return ((addr == null && otherGtx.addr == null) || (addr != null && otherGtx.addr != null && addr.compareTo(otherGtx.addr) == 0)) &&
98               id == otherGtx.id;
99    }
100
101    public String JavaDoc toString()
102    {
103       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104       sb.append("GlobalTransaction:<").append(addr).append(">:").append(id);
105       return sb.toString();
106    }
107
108    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
109    {
110       out.writeObject(addr);
111       out.writeLong(id);
112       // out.writeInt(hash_code);
113
}
114
115    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
116    {
117       addr = (Address) in.readObject();
118       id = in.readLong();
119       hash_code = -1;
120    }
121
122    /**
123     * @return Returns the remote.
124     */

125    public boolean isRemote()
126    {
127       return remote;
128    }
129
130    /**
131     * @param remote The remote to set.
132     */

133    public void setRemote(boolean remote)
134    {
135       this.remote = remote;
136    }
137
138
139    public void setId(long id)
140    {
141       this.id = id;
142    }
143 }
144
Popular Tags