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.HashUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    
022    import java.io.Serializable;
023    
024    import java.net.InetAddress;
025    import java.net.InetSocketAddress;
026    
027    import java.util.Objects;
028    
029    /**
030     * @author Tina Tian
031     */
032    @ProviderType
033    public class ClusterNode implements Serializable {
034    
035            public ClusterNode(String clusterNodeId, InetAddress bindInetAddress) {
036                    if (clusterNodeId == null) {
037                            throw new IllegalArgumentException("Cluster node ID is null");
038                    }
039    
040                    if (bindInetAddress == null) {
041                            throw new IllegalArgumentException("Bind inet address is null");
042                    }
043    
044                    _clusterNodeId = clusterNodeId;
045                    _bindInetAddress = bindInetAddress;
046            }
047    
048            @Override
049            public boolean equals(Object obj) {
050                    if (this == obj) {
051                            return true;
052                    }
053    
054                    if (!(obj instanceof ClusterNode)) {
055                            return false;
056                    }
057    
058                    ClusterNode clusterNode = (ClusterNode)obj;
059    
060                    if (Objects.equals(_clusterNodeId, clusterNode._clusterNodeId) &&
061                            Objects.equals(_bindInetAddress, clusterNode._bindInetAddress) &&
062                            Objects.equals(
063                                    _portalInetSocketAddress,
064                                    clusterNode._portalInetSocketAddress) &&
065                            Objects.equals(_portalProtocol, clusterNode._portalProtocol)) {
066    
067                            return true;
068                    }
069    
070                    return false;
071            }
072    
073            public InetAddress getBindInetAddress() {
074                    return _bindInetAddress;
075            }
076    
077            public String getClusterNodeId() {
078                    return _clusterNodeId;
079            }
080    
081            public InetAddress getPortalInetAddress() {
082                    if (_portalInetSocketAddress == null) {
083                            return null;
084                    }
085    
086                    return _portalInetSocketAddress.getAddress();
087            }
088    
089            public InetSocketAddress getPortalInetSocketAddress() {
090                    return _portalInetSocketAddress;
091            }
092    
093            public int getPortalPort() {
094                    if (_portalInetSocketAddress == null) {
095                            return -1;
096                    }
097    
098                    return _portalInetSocketAddress.getPort();
099            }
100    
101            public String getPortalProtocol() {
102                    return _portalProtocol;
103            }
104    
105            @Override
106            public int hashCode() {
107                    int hash = HashUtil.hash(0, _clusterNodeId);
108    
109                    hash = HashUtil.hash(hash, _bindInetAddress);
110                    hash = HashUtil.hash(hash, _portalInetSocketAddress);
111                    hash = HashUtil.hash(hash, _portalProtocol);
112    
113                    return hash;
114            }
115    
116            public void setPortalInetSocketAddress(
117                    InetSocketAddress portalInetSocketAddress) {
118    
119                    _portalInetSocketAddress = portalInetSocketAddress;
120            }
121    
122            public void setPortalProtocol(String portalProtocol) {
123                    _portalProtocol = portalProtocol;
124            }
125    
126            @Override
127            public String toString() {
128                    StringBundler sb = new StringBundler(9);
129    
130                    sb.append("{bindInetAddress=");
131                    sb.append(_bindInetAddress);
132                    sb.append(", clusterNodeId=");
133                    sb.append(_clusterNodeId);
134                    sb.append(", portalInetSocketAddress=");
135                    sb.append(_portalInetSocketAddress);
136                    sb.append(", portalProtocol=");
137                    sb.append(_portalProtocol);
138                    sb.append("}");
139    
140                    return sb.toString();
141            }
142    
143            private final InetAddress _bindInetAddress;
144            private final String _clusterNodeId;
145            private InetSocketAddress _portalInetSocketAddress;
146            private String _portalProtocol;
147    
148    }