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, InetAddress inetAddress) {
030                    if (clusterNodeId == null) {
031                            throw new IllegalArgumentException("Cluster node ID is null");
032                    }
033    
034                    if (inetAddress == null) {
035                            throw new IllegalArgumentException("Inet address is null");
036                    }
037    
038                    _clusterNodeId = clusterNodeId;
039                    _inetAddress = inetAddress;
040            }
041    
042            public int compareTo(ClusterNode clusterNode) {
043                    InetAddress inetAddress = clusterNode._inetAddress;
044    
045                    String ipAddress = inetAddress.getHostAddress();
046    
047                    String curIpAddress = _inetAddress.getHostAddress();
048    
049                    int value = curIpAddress.compareTo(ipAddress);
050    
051                    if (value == 0) {
052                            if (_port > clusterNode._port) {
053                                    value = 1;
054                            }
055                            else if (_port < clusterNode._port) {
056                                    value = -1;
057                            }
058                    }
059    
060                    return value;
061            }
062    
063            @Override
064            public boolean equals(Object obj) {
065                    if (this == obj) {
066                            return true;
067                    }
068    
069                    if (!(obj instanceof ClusterNode)) {
070                            return false;
071                    }
072    
073                    ClusterNode clusterNode = (ClusterNode)obj;
074    
075                    if (Validator.equals(_clusterNodeId, clusterNode._clusterNodeId)) {
076                            return true;
077                    }
078    
079                    return false;
080            }
081    
082            public String getClusterNodeId() {
083                    return _clusterNodeId;
084            }
085    
086            public InetAddress getInetAddress() {
087                    return _inetAddress;
088            }
089    
090            public int getPort() {
091                    return _port;
092            }
093    
094            @Override
095            public int hashCode() {
096                    return _clusterNodeId.hashCode();
097            }
098    
099            public void setPort(int port) {
100                    _port = port;
101            }
102    
103            @Override
104            public String toString() {
105                    StringBundler sb = new StringBundler(7);
106    
107                    sb.append("{clusterNodeId=");
108                    sb.append(_clusterNodeId);
109                    sb.append(", inetAddress=");
110                    sb.append(_inetAddress);
111                    sb.append(", port=");
112                    sb.append(_port);
113                    sb.append("}");
114    
115                    return sb.toString();
116            }
117    
118            private String _clusterNodeId;
119            private InetAddress _inetAddress;
120            private int _port;
121    
122    }