001    /**
002     * Copyright (c) 2000-present 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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Serializable;
023    
024    import java.net.InetAddress;
025    import java.net.InetSocketAddress;
026    
027    /**
028     * @author Tina Tian
029     */
030    @ProviderType
031    public class ClusterNode implements Comparable<ClusterNode>, Serializable {
032    
033            public ClusterNode(String clusterNodeId, InetAddress bindInetAddress) {
034                    if (clusterNodeId == null) {
035                            throw new IllegalArgumentException("Cluster node ID is null");
036                    }
037    
038                    if (bindInetAddress == null) {
039                            throw new IllegalArgumentException("Bind inet address is null");
040                    }
041    
042                    _clusterNodeId = clusterNodeId;
043                    _bindInetAddress = bindInetAddress;
044            }
045    
046            @Override
047            public int compareTo(ClusterNode clusterNode) {
048                    InetAddress bindInetAddress = clusterNode._bindInetAddress;
049    
050                    String hostAddress = _bindInetAddress.getHostAddress();
051    
052                    int value = hostAddress.compareTo(bindInetAddress.getHostAddress());
053    
054                    if (value != 0) {
055                            return value;
056                    }
057    
058                    if ((_portalInetSocketAddress == null) ||
059                            (clusterNode._portalInetSocketAddress == null)) {
060    
061                            if (_portalInetSocketAddress != null) {
062                                    return 1;
063                            }
064    
065                            if (clusterNode._portalInetSocketAddress != null) {
066                                    return -1;
067                            }
068    
069                            return 0;
070                    }
071    
072                    InetAddress thisInetAddress = _portalInetSocketAddress.getAddress();
073    
074                    String thisHostAddress = thisInetAddress.getHostAddress();
075    
076                    InetSocketAddress otherPortalInetSocketAddress =
077                            clusterNode._portalInetSocketAddress;
078    
079                    InetAddress otherInetAddress =
080                            otherPortalInetSocketAddress.getAddress();
081    
082                    value = thisHostAddress.compareTo(otherInetAddress.getHostAddress());
083    
084                    if (value != 0) {
085                            return value;
086                    }
087    
088                    int otherPort = otherPortalInetSocketAddress.getPort();
089    
090                    int thisPort = _portalInetSocketAddress.getPort();
091    
092                    if (thisPort > otherPort) {
093                            value = 1;
094                    }
095                    else if (thisPort < otherPort) {
096                            value = -1;
097                    }
098    
099                    return value;
100            }
101    
102            @Override
103            public boolean equals(Object obj) {
104                    if (this == obj) {
105                            return true;
106                    }
107    
108                    if (!(obj instanceof ClusterNode)) {
109                            return false;
110                    }
111    
112                    ClusterNode clusterNode = (ClusterNode)obj;
113    
114                    if (Validator.equals(_clusterNodeId, clusterNode._clusterNodeId)) {
115                            return true;
116                    }
117    
118                    return false;
119            }
120    
121            public InetAddress getBindInetAddress() {
122                    return _bindInetAddress;
123            }
124    
125            public String getClusterNodeId() {
126                    return _clusterNodeId;
127            }
128    
129            public InetAddress getPortalInetAddress() {
130                    if (_portalInetSocketAddress == null) {
131                            return null;
132                    }
133    
134                    return _portalInetSocketAddress.getAddress();
135            }
136    
137            public InetSocketAddress getPortalInetSocketAddress() {
138                    return _portalInetSocketAddress;
139            }
140    
141            public int getPortalPort() {
142                    if (_portalInetSocketAddress == null) {
143                            return -1;
144                    }
145    
146                    return _portalInetSocketAddress.getPort();
147            }
148    
149            @Override
150            public int hashCode() {
151                    return _clusterNodeId.hashCode();
152            }
153    
154            public void setPortalInetSocketAddress(
155                    InetSocketAddress portalInetSocketAddress) {
156    
157                    _portalInetSocketAddress = portalInetSocketAddress;
158            }
159    
160            @Override
161            public String toString() {
162                    StringBundler sb = new StringBundler(7);
163    
164                    sb.append("{bindInetAddress=");
165                    sb.append(_bindInetAddress);
166                    sb.append(", clusterNodeId=");
167                    sb.append(_clusterNodeId);
168                    sb.append(", portalInetSocketAddress=");
169                    sb.append(_portalInetSocketAddress);
170                    sb.append("}");
171    
172                    return sb.toString();
173            }
174    
175            private final InetAddress _bindInetAddress;
176            private final String _clusterNodeId;
177            private InetSocketAddress _portalInetSocketAddress;
178    
179    }