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