001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cluster;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.net.InetAddress;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class ClusterNode implements Comparable<ClusterNode>, Serializable {
028    
029            public ClusterNode(String clusterNodeId) {
030                    _clusterNodeId = clusterNodeId;
031            }
032    
033            public int compareTo(ClusterNode clusterNode) {
034                    InetAddress inetAddress = clusterNode._inetAddress;
035    
036                    String ipAddress = inetAddress.getHostAddress();
037    
038                    String curIpAddress = _inetAddress.getHostAddress();
039    
040                    int value = curIpAddress.compareTo(ipAddress);
041    
042                    if (value == 0) {
043                            if (_port > clusterNode._port) {
044                                    value = 1;
045                            }
046                            else if (_port < clusterNode._port) {
047                                    value = -1;
048                            }
049                    }
050    
051                    return value;
052            }
053    
054            @Override
055            public boolean equals(Object obj) {
056                    if (this == obj) {
057                            return true;
058                    }
059    
060                    if (!(obj instanceof ClusterNode)) {
061                            return false;
062                    }
063    
064                    ClusterNode clusterNode = (ClusterNode)obj;
065    
066                    if (Validator.equals(_clusterNodeId, clusterNode._clusterNodeId)) {
067                            return true;
068                    }
069    
070                    return false;
071            }
072    
073            public String getClusterNodeId() {
074                    return _clusterNodeId;
075            }
076    
077            public String getHostName() {
078                    return _hostName;
079            }
080    
081            public InetAddress getInetAddress() {
082                    return _inetAddress;
083            }
084    
085            public int getPort() {
086                    return _port;
087            }
088    
089            @Override
090            public int hashCode() {
091                    return _clusterNodeId.hashCode();
092            }
093    
094            public void setHostName(String hostName) {
095                    _hostName = hostName;
096            }
097    
098            public void setInetAddress(InetAddress inetAddress) {
099                    _inetAddress = inetAddress;
100            }
101    
102            public void setPort(int port) {
103                    _port = port;
104            }
105    
106            @Override
107            public String toString() {
108                    StringBundler sb = new StringBundler(9);
109    
110                    sb.append("{clusterNodeId=");
111                    sb.append(_clusterNodeId);
112                    sb.append(", hostName=");
113                    sb.append(_hostName);
114                    sb.append(", inetAddress=");
115                    sb.append(_inetAddress);
116                    sb.append(", port=");
117                    sb.append(_port);
118                    sb.append("}");
119    
120                    return sb.toString();
121            }
122    
123            private String _clusterNodeId;
124            private String _hostName;
125            private InetAddress _inetAddress;
126            private int _port;
127    
128    }