001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.tx;
006:
007: import com.tc.io.serializer.TCObjectInputStream;
008: import com.tc.io.serializer.TCObjectOutputStream;
009: import com.tc.net.groups.ClientID;
010: import com.tc.net.groups.NodeID;
011: import com.tc.net.groups.NodeIDImpl;
012: import com.tc.net.groups.NodeIDSerializer;
013:
014: import java.io.ByteArrayInputStream;
015: import java.io.ByteArrayOutputStream;
016: import java.io.IOException;
017:
018: /**
019: * A class that represents a particular client transaction from the server's perspective (ie. the combination of
020: * NodeID and a client TransactionID)
021: */
022: public class ServerTransactionID {
023: public static final ServerTransactionID NULL_ID = new ServerTransactionID(
024: ClientID.NULL_ID, TransactionID.NULL_ID);
025:
026: private final TransactionID txnID;
027: private final NodeID sourceID;
028: private final int hashCode;
029:
030: public ServerTransactionID(NodeID source, TransactionID txnID) {
031: this .sourceID = source;
032: this .txnID = txnID;
033:
034: int hash = 29;
035: hash = (37 * hash) + source.hashCode();
036: hash = (37 * hash) + txnID.hashCode();
037: this .hashCode = hash;
038: }
039:
040: public NodeID getSourceID() {
041: return sourceID;
042: }
043:
044: public TransactionID getClientTransactionID() {
045: return txnID;
046: }
047:
048: public boolean isServerGeneratedTransaction() {
049: //TODO::Move this logic away from here
050: return (sourceID instanceof NodeIDImpl);
051: }
052:
053: public boolean isNull() {
054: return sourceID.isNull() && txnID.isNull();
055: }
056:
057: public String toString() {
058: return new StringBuffer().append("ServerTransactionID{")
059: .append(sourceID).append(',').append(txnID).append('}')
060: .toString();
061: }
062:
063: public int hashCode() {
064: return this .hashCode;
065: }
066:
067: public boolean equals(Object obj) {
068: if (obj instanceof ServerTransactionID) {
069: ServerTransactionID other = (ServerTransactionID) obj;
070: return this .sourceID.equals(other.sourceID)
071: && this .txnID.equals(other.txnID);
072: }
073: return false;
074: }
075:
076: /**
077: * Utility method for serialization.
078: */
079: public byte[] getBytes() {
080: try {
081: ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
082: TCObjectOutputStream tco = new TCObjectOutputStream(baos);
083: NodeIDSerializer.writeNodeID(sourceID, tco);
084: tco.writeLong(txnID.toLong());
085: tco.close();
086: return baos.toByteArray();
087: } catch (IOException e) {
088: throw new AssertionError(e);
089: }
090: }
091:
092: /**
093: * Utility method for deserialization.
094: */
095: public static ServerTransactionID createFrom(byte[] data) {
096: try {
097: ByteArrayInputStream bais = new ByteArrayInputStream(data);
098: TCObjectInputStream tci = new TCObjectInputStream(bais);
099: return new ServerTransactionID(NodeIDSerializer
100: .readNodeID(tci), new TransactionID(tci.readLong()));
101: } catch (Exception e) {
102: throw new AssertionError(e);
103: }
104: }
105: }
|