001    /**
002     * Copyright (c) 2000-2013 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            @Override
043            public int compareTo(ClusterNode clusterNode) {
044                    InetAddress inetAddress = clusterNode._inetAddress;
045    
046                    String hostAddress = _inetAddress.getHostAddress();
047    
048                    int value = hostAddress.compareTo(inetAddress.getHostAddress());
049    
050                    if (value != 0) {
051                            return value;
052                    }
053    
054                    if ((_portalProtocol == null) ||
055                            (clusterNode._portalProtocol == null)) {
056    
057                            if (_portalProtocol != null) {
058                                    return 1;
059                            }
060    
061                            if (clusterNode._portalProtocol != null) {
062                                    return -1;
063                            }
064    
065                            return 0;
066                    }
067    
068                    value = _portalProtocol.compareTo(clusterNode._portalProtocol);
069    
070                    if (value != 0) {
071                            return value;
072                    }
073    
074                    if (_port > clusterNode._port) {
075                            value = 1;
076                    }
077                    else if (_port < clusterNode._port) {
078                            value = -1;
079                    }
080    
081                    return value;
082            }
083    
084            @Override
085            public boolean equals(Object obj) {
086                    if (this == obj) {
087                            return true;
088                    }
089    
090                    if (!(obj instanceof ClusterNode)) {
091                            return false;
092                    }
093    
094                    ClusterNode clusterNode = (ClusterNode)obj;
095    
096                    if (Validator.equals(_clusterNodeId, clusterNode._clusterNodeId)) {
097                            return true;
098                    }
099    
100                    return false;
101            }
102    
103            public String getClusterNodeId() {
104                    return _clusterNodeId;
105            }
106    
107            public InetAddress getInetAddress() {
108                    return _inetAddress;
109            }
110    
111            public int getPort() {
112                    return _port;
113            }
114    
115            public String getPortalProtocol() {
116                    return _portalProtocol;
117            }
118    
119            @Override
120            public int hashCode() {
121                    return _clusterNodeId.hashCode();
122            }
123    
124            public void setPort(int port) {
125                    _port = port;
126            }
127    
128            public void setPortalProtocol(String portalProtocol) {
129                    _portalProtocol = portalProtocol;
130            }
131    
132            @Override
133            public String toString() {
134                    StringBundler sb = new StringBundler(9);
135    
136                    sb.append("{clusterNodeId=");
137                    sb.append(_clusterNodeId);
138                    sb.append(", portalProtocol=");
139                    sb.append(_portalProtocol);
140                    sb.append(", inetAddress=");
141                    sb.append(_inetAddress);
142                    sb.append(", port=");
143                    sb.append(_port);
144                    sb.append("}");
145    
146                    return sb.toString();
147            }
148    
149            private String _clusterNodeId;
150            private InetAddress _inetAddress;
151            private int _port;
152            private String _portalProtocol;
153    
154    }