001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.framework.interfaces;
023:
024: import java.net.InetAddress;
025: import java.io.Serializable;
026:
027: import org.jgroups.stack.IpAddress;
028:
029: /**
030: * Replacement for a JG IpAddress that doesn't base its representation
031: * on the JG address but on the computed node name added to the IPAddress instead.
032: * This is to avoid any problem in the cluster as some nodes may interpret a node name
033: * differently (IP resolution, name case, FQDN or host name, etc.)
034: *
035: * @see org.jboss.ha.framework.server.ClusterPartition
036: *
037: * @author <a href="mailto:sacha.labourey@jboss.org">Sacha Labourey</a>.
038: * @version $Revision: 57188 $
039: *
040: * <p><b>Revisions:</b>
041: *
042: * <p><b>August 17 2003 Sacha Labourey:</b>
043: * <ul>
044: * <li> First implementation </li>
045: * </ul>
046: */
047:
048: public class ClusterNode implements Comparable, Cloneable, Serializable {
049:
050: // Constants -----------------------------------------------------
051:
052: // Attributes ----------------------------------------------------
053:
054: protected String id = null;
055: protected String jgId = null;
056: protected IpAddress originalJGAddress = null;
057:
058: // Static --------------------------------------------------------
059:
060: // Constructors --------------------------------------------------
061:
062: public ClusterNode() {
063: }
064:
065: public ClusterNode(IpAddress jgAddress) {
066: if (jgAddress.getAdditionalData() == null) {
067: this .id = jgAddress.getIpAddress().getHostAddress() + ":"
068: + jgAddress.getPort();
069: } else {
070: this .id = new String(jgAddress.getAdditionalData());
071: }
072:
073: this .originalJGAddress = jgAddress;
074: StringBuffer sb = new StringBuffer();
075: java.net.InetAddress jgIPAddr = jgAddress.getIpAddress();
076: if (jgIPAddr == null)
077: sb.append("<null>");
078: else {
079: if (jgIPAddr.isMulticastAddress())
080: sb.append(jgIPAddr.getHostAddress());
081: else
082: sb.append(getShortName(jgIPAddr.getHostName()));
083: }
084: sb.append(":" + jgAddress.getPort());
085: this .jgId = sb.toString();
086: }
087:
088: // Public --------------------------------------------------------
089:
090: public String getName() {
091: return this .id;
092: }
093:
094: public String getJGName() {
095: return this .jgId;
096: }
097:
098: public IpAddress getOriginalJGAddress() {
099: return this .originalJGAddress;
100: }
101:
102: public InetAddress getIpAddress() {
103: return this .originalJGAddress.getIpAddress();
104: }
105:
106: public int getPort() {
107: return this .originalJGAddress.getPort();
108: }
109:
110: // Comparable implementation ----------------------------------------------
111:
112: // Comparable implementation ----------------------------------------------
113:
114: public int compareTo(Object o) {
115: if ((o == null) || !(o instanceof ClusterNode))
116: throw new ClassCastException(
117: "ClusterNode.compareTo(): comparison between different classes");
118:
119: ClusterNode other = (ClusterNode) o;
120:
121: return this .id.compareTo(other.id);
122: }
123:
124: // java.lang.Object overrides ---------------------------------------------------
125:
126: public boolean equals(Object obj) {
127: if (obj == null || !(obj instanceof ClusterNode))
128: return false;
129:
130: ClusterNode other = (ClusterNode) obj;
131: return this .id.equals(other.id);
132: }
133:
134: public int hashCode() {
135: return id.hashCode();
136: }
137:
138: public String toString() {
139: return this .getName();
140: }
141:
142: // Package protected ---------------------------------------------
143:
144: // Protected -----------------------------------------------------
145:
146: protected String getShortName(String hostname) {
147: int index = hostname.indexOf('.');
148:
149: if (hostname == null)
150: return "";
151: if (index > 0 && !Character.isDigit(hostname.charAt(0)))
152: return hostname.substring(0, index);
153: else
154: return hostname;
155: }
156:
157: // Private -------------------------------------------------------
158:
159: // Inner classes -------------------------------------------------
160:
161: }
|