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 com.liferay.portal.kernel.security.SecureRandomUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    import java.io.Serializable;
021    
022    import java.util.Collections;
023    import java.util.HashSet;
024    import java.util.Set;
025    import java.util.UUID;
026    
027    /**
028     * @author Tina Tian
029     */
030    public class ClusterRequest implements Serializable {
031    
032            public static ClusterRequest createMulticastRequest(Serializable payload) {
033                    return createMulticastRequest(payload, false);
034            }
035    
036            public static ClusterRequest createMulticastRequest(
037                    Serializable payload, boolean skipLocal) {
038    
039                    return new ClusterRequest(payload, skipLocal, true);
040            }
041    
042            public static ClusterRequest createUnicastRequest(
043                    Serializable payload, String... targetClusterNodeIds) {
044    
045                    if ((targetClusterNodeIds == null) ||
046                            (targetClusterNodeIds.length == 0)) {
047    
048                            throw new NullPointerException("Target cluster node IDs is null");
049                    }
050    
051                    return new ClusterRequest(payload, false, false, targetClusterNodeIds);
052            }
053    
054            public Serializable getPayload() {
055                    return _payload;
056            }
057    
058            public Set<String> getTargetClusterNodeIds() {
059                    return Collections.unmodifiableSet(_targetClusterNodeIds);
060            }
061    
062            public String getUuid() {
063                    return _uuid;
064            }
065    
066            public boolean isFireAndForget() {
067                    return _fireAndForget;
068            }
069    
070            public boolean isMulticast() {
071                    return _multicast;
072            }
073    
074            public boolean isSkipLocal() {
075                    return _skipLocal;
076            }
077    
078            public void setFireAndForget(boolean fireAndForget) {
079                    _fireAndForget = fireAndForget;
080            }
081    
082            @Override
083            public String toString() {
084                    StringBundler sb = new StringBundler(11);
085    
086                    sb.append("{multicast=");
087                    sb.append(_multicast);
088                    sb.append(", payload=");
089                    sb.append(_payload);
090                    sb.append(", skipLocal=");
091                    sb.append(_skipLocal);
092    
093                    if (!_multicast) {
094                            sb.append(", _targetClusterNodeIds=");
095                            sb.append(_targetClusterNodeIds);
096                    }
097    
098                    sb.append(", uuid=");
099                    sb.append(_uuid);
100                    sb.append("}");
101    
102                    return sb.toString();
103            }
104    
105            private ClusterRequest(
106                    Serializable payload, boolean skipLocal, boolean multicast,
107                    String... targetClusterNodeIds) {
108    
109                    if (payload == null) {
110                            throw new NullPointerException("Payload is null");
111                    }
112    
113                    _payload = payload;
114                    _skipLocal = skipLocal;
115                    _multicast = multicast;
116    
117                    _uuid = _generateUUID();
118    
119                    for (String targetClusterNodeId : targetClusterNodeIds) {
120                            _targetClusterNodeIds.add(targetClusterNodeId);
121                    }
122            }
123    
124            private String _generateUUID() {
125                    UUID uuid = new UUID(
126                            SecureRandomUtil.nextLong(), SecureRandomUtil.nextLong());
127    
128                    return uuid.toString();
129            }
130    
131            private boolean _fireAndForget;
132            private final boolean _multicast;
133            private final Serializable _payload;
134            private final boolean _skipLocal;
135            private final Set<String> _targetClusterNodeIds = new HashSet<>();
136            private final String _uuid;
137    
138    }