001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.net.protocol.transport;
005:
006: import com.tc.net.protocol.tcm.ChannelID;
007:
008: public class ConnectionID {
009:
010: private final long channelID;
011: private final String serverID;
012:
013: public static final ConnectionID NULL_ID = new ConnectionID(
014: ChannelID.NULL_ID.toLong(),
015: "ffffffffffffffffffffffffffffffff");
016:
017: private static final String SEP = ".";
018:
019: public static ConnectionID parse(String compositeID)
020: throws InvalidConnectionIDException {
021: if (compositeID == null) {
022: throw new InvalidConnectionIDException(compositeID,
023: "null connectionID");
024: }
025:
026: String[] parts = compositeID.split("\\" + SEP);
027: if (parts.length != 2) {
028: // make formatter sane
029: throw new InvalidConnectionIDException(compositeID,
030: "wrong number of components: " + parts.length);
031: }
032:
033: String channelID = parts[0];
034: final long channel;
035: try {
036: channel = Long.parseLong(channelID);
037: } catch (Exception e) {
038: throw new InvalidConnectionIDException(compositeID,
039: "parse exception for channelID " + channelID, e);
040: }
041:
042: String server = parts[1];
043: if (server.length() != 32) {
044: throw new InvalidConnectionIDException(compositeID,
045: "invalid serverID length: " + server.length());
046: }
047:
048: if (!server.matches("[A-Fa-f0-9]+")) {
049: throw new InvalidConnectionIDException(compositeID,
050: "invalid chars in serverID: " + server);
051: }
052:
053: return new ConnectionID(channel, server);
054: }
055:
056: public ConnectionID(long channelID, String serverID) {
057: this .channelID = channelID;
058: this .serverID = serverID;
059: }
060:
061: public String toString() {
062: return "ConnectionID(" + getID() + ")";
063: }
064:
065: public boolean isNull() {
066: return NULL_ID.equals(this );
067: }
068:
069: public String getServerID() {
070: return this .serverID;
071: }
072:
073: public int hashCode() {
074: int hc = 17;
075: hc = 37 * hc + (int) (this .channelID ^ (this .channelID >>> 32));
076: if (this .serverID != null) {
077: hc = 37 * hc + serverID.hashCode();
078: }
079:
080: return hc;
081: }
082:
083: public boolean equals(Object obj) {
084: if (obj instanceof ConnectionID) {
085: ConnectionID other = (ConnectionID) obj;
086: return (this .channelID == other.channelID)
087: && (this .serverID.equals(other.serverID));
088: }
089: return false;
090: }
091:
092: public long getChannelID() {
093: return channelID;
094: }
095:
096: public String getID() {
097: return channelID + SEP + serverID;
098: }
099:
100: }
|