KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > interfaces > ClusterNode


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ha.framework.interfaces;
23
24 import java.net.InetAddress JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 import org.jgroups.stack.IpAddress;
28
29 /**
30  * Replacement for a JG IpAddress that doesn't base its representation
31  * on the JG address but on the computed node name added to the IPAddress instead.
32  * This is to avoid any problem in the cluster as some nodes may interpret a node name
33  * differently (IP resolution, name case, FQDN or host name, etc.)
34  *
35  * @see org.jboss.ha.framework.server.ClusterPartitionMBean
36  *
37  * @author <a HREF="mailto:sacha.labourey@jboss.org">Sacha Labourey</a>.
38  * @version $Revision: 58561 $
39  *
40  * <p><b>Revisions:</b>
41  *
42  * <p><b>August 17 2003 Sacha Labourey:</b>
43  * <ul>
44  * <li> First implementation </li>
45  * </ul>
46  */

47
48 public class ClusterNode
49    implements Comparable JavaDoc, Cloneable JavaDoc, Serializable JavaDoc
50 {
51
52    // Constants -----------------------------------------------------
53

54    // Attributes ----------------------------------------------------
55

56    protected String JavaDoc id = null;
57    protected String JavaDoc jgId = null;
58    protected IpAddress originalJGAddress = null;
59
60    // Static --------------------------------------------------------
61

62    // Constructors --------------------------------------------------
63

64    public ClusterNode()
65    {
66    }
67
68    public ClusterNode(IpAddress jgAddress)
69    {
70       if (jgAddress.getAdditionalData() == null)
71       {
72          this.id = jgAddress.getIpAddress().getHostAddress() + ":" + jgAddress.getPort();
73       }
74       else
75       {
76          this.id = new String JavaDoc(jgAddress.getAdditionalData());
77       }
78
79       this.originalJGAddress = jgAddress;
80       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
81       java.net.InetAddress JavaDoc jgIPAddr = jgAddress.getIpAddress();
82       if (jgIPAddr == null)
83          sb.append("<null>");
84       else
85       {
86          if (jgIPAddr.isMulticastAddress())
87             sb.append(jgIPAddr.getHostAddress());
88          else
89             sb.append(getShortName(jgIPAddr.getHostName()));
90       }
91       sb.append(":" + jgAddress.getPort());
92       this.jgId = sb.toString();
93    }
94
95    // Public --------------------------------------------------------
96

97    public String JavaDoc getName()
98    {
99       return this.id;
100    }
101
102    public String JavaDoc getJGName()
103    {
104       return this.jgId;
105    }
106
107    public IpAddress getOriginalJGAddress()
108    {
109       return this.originalJGAddress;
110    }
111    public InetAddress JavaDoc getIpAddress()
112    {
113       return this.originalJGAddress.getIpAddress();
114    }
115    public int getPort()
116    {
117       return this.originalJGAddress.getPort();
118    }
119
120    // Comparable implementation ----------------------------------------------
121

122    // Comparable implementation ----------------------------------------------
123

124    public int compareTo(Object JavaDoc o)
125    {
126       if ((o == null) || !(o instanceof ClusterNode))
127          throw new ClassCastException JavaDoc("ClusterNode.compareTo(): comparison between different classes");
128
129       ClusterNode other = (ClusterNode) o;
130
131       return this.id.compareTo(other.id);
132    }
133    // java.lang.Object overrides ---------------------------------------------------
134

135    public boolean equals(Object JavaDoc obj)
136    {
137       if (obj == null || !(obj instanceof ClusterNode)) return false;
138       
139       ClusterNode other = (ClusterNode) obj;
140       return this.id.equals(other.id);
141    }
142
143    public int hashCode()
144    {
145       return id.hashCode();
146    }
147
148    public String JavaDoc toString()
149    {
150       return this.getName();
151    }
152
153    // Package protected ---------------------------------------------
154

155    // Protected -----------------------------------------------------
156

157    protected String JavaDoc getShortName(String JavaDoc hostname)
158    {
159       int index = hostname.indexOf('.');
160
161       if (hostname == null) return "";
162       if (index > 0 && !Character.isDigit(hostname.charAt(0)))
163          return hostname.substring(0, index);
164       else
165          return hostname;
166    }
167
168    // Private -------------------------------------------------------
169

170    // Inner classes -------------------------------------------------
171

172 }
173
Popular Tags