01: /**
02: * $RCSfile: $
03: * $Revision: $
04: * $Date: $
05: *
06: * Copyright (C) 2007 Jive Software. All rights reserved.
07: *
08: * This software is published under the terms of the GNU Public License (GPL),
09: * a copy of which is included in this distribution.
10: */package org.jivesoftware.openfire.cluster;
11:
12: import org.jivesoftware.util.cache.ExternalizableUtil;
13:
14: import java.io.Externalizable;
15: import java.io.IOException;
16: import java.io.ObjectInput;
17: import java.io.ObjectOutput;
18: import java.util.ArrayList;
19: import java.util.Arrays;
20: import java.util.List;
21:
22: /**
23: * Class which wraps the byte[] we use to identify cluster members. The main reason
24: * for this class is that byte[] cannot be directly compared so having a collection
25: * of byte[] is not possible since you cannot remove to equivalent byte[] from the
26: * collection.<p>
27: *
28: * @author Pete Matern
29: * @author Gaston Dombiak
30: */
31: public class NodeID implements Externalizable {
32: private static List<NodeID> instances = new ArrayList<NodeID>();
33:
34: private byte[] nodeID;
35:
36: public static synchronized NodeID getInstance(byte[] nodeIdBytes) {
37: for (NodeID nodeID : instances) {
38: if (nodeID.equals(nodeIdBytes)) {
39: return nodeID;
40: }
41: }
42: NodeID answer = new NodeID(nodeIdBytes);
43: instances.add(answer);
44: return answer;
45: }
46:
47: public static synchronized void deleteInstance(byte[] nodeIdBytes) {
48: NodeID toDelete = null;
49: for (NodeID nodeID : instances) {
50: if (nodeID.equals(nodeIdBytes)) {
51: toDelete = nodeID;
52: break;
53: }
54: }
55: if (toDelete != null) {
56: instances.remove(toDelete);
57: }
58: }
59:
60: public NodeID() {
61: }
62:
63: private NodeID(byte[] nodeIdBytes) {
64: this .nodeID = nodeIdBytes;
65: }
66:
67: public boolean equals(byte[] anotherID) {
68: return Arrays.equals(nodeID, anotherID);
69: }
70:
71: public boolean equals(Object o) {
72: if (this == o)
73: return true;
74: if (o == null || getClass() != o.getClass())
75: return false;
76:
77: NodeID that = (NodeID) o;
78:
79: return Arrays.equals(nodeID, that.nodeID);
80: }
81:
82: public int hashCode() {
83: return Arrays.hashCode(nodeID);
84: }
85:
86: public byte[] toByteArray() {
87: return nodeID;
88: }
89:
90: public void writeExternal(ObjectOutput out) throws IOException {
91: ExternalizableUtil.getInstance().writeByteArray(out, nodeID);
92: }
93:
94: public void readExternal(ObjectInput in) throws IOException,
95: ClassNotFoundException {
96: nodeID = ExternalizableUtil.getInstance().readByteArray(in);
97: }
98: }
|