001
014
015 package com.liferay.portal.kernel.cluster;
016
017 import com.liferay.portal.kernel.util.MethodHandler;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
020
021 import java.io.Serializable;
022
023 import java.util.Collection;
024 import java.util.HashSet;
025 import java.util.Set;
026
027
030 public class ClusterRequest implements Serializable {
031
032 public static ClusterRequest createClusterRequest(
033 ClusterMessageType clusterMessageType,
034 ClusterNode originatingClusterNode) {
035
036 ClusterRequest clusterRequest = new ClusterRequest();
037
038 clusterRequest.setClusterMessageType(clusterMessageType);
039 clusterRequest.setMulticast(true);
040 clusterRequest.setOriginatingClusterNode(originatingClusterNode);
041 clusterRequest.setSkipLocal(true);
042 clusterRequest.setUuid(PortalUUIDUtil.generate());
043
044 return clusterRequest;
045 }
046
047 public static ClusterRequest createMulticastRequest(
048 MethodHandler methodHandler) {
049
050 return createMulticastRequest(methodHandler, false);
051 }
052
053 public static ClusterRequest createMulticastRequest(
054 MethodHandler methodHandler, boolean skipLocal) {
055
056 ClusterRequest clusterRequest = new ClusterRequest();
057
058 clusterRequest.setClusterMessageType(ClusterMessageType.EXECUTE);
059 clusterRequest.setMethodHandler(methodHandler);
060 clusterRequest.setMulticast(true);
061 clusterRequest.setSkipLocal(skipLocal);
062 clusterRequest.setUuid(PortalUUIDUtil.generate());
063
064 return clusterRequest;
065 }
066
067 public static ClusterRequest createUnicastRequest(
068 MethodHandler methodHandler, Address... targetClusterNodeAddresses) {
069
070 ClusterRequest clusterRequest = new ClusterRequest();
071
072 clusterRequest.addTargetClusterNodeAddresses(
073 targetClusterNodeAddresses);
074 clusterRequest.setClusterMessageType(ClusterMessageType.EXECUTE);
075 clusterRequest.setMethodHandler(methodHandler);
076 clusterRequest.setMulticast(false);
077 clusterRequest.setSkipLocal(false);
078 clusterRequest.setUuid(PortalUUIDUtil.generate());
079
080 return clusterRequest;
081 }
082
083 public static ClusterRequest createUnicastRequest(
084 MethodHandler methodHandler, String... targetClusterNodeIds) {
085
086 ClusterRequest clusterRequest = new ClusterRequest();
087
088 clusterRequest.addTargetClusterNodeIds(targetClusterNodeIds);
089 clusterRequest.setClusterMessageType(ClusterMessageType.EXECUTE);
090 clusterRequest.setMethodHandler(methodHandler);
091 clusterRequest.setMulticast(false);
092 clusterRequest.setSkipLocal(false);
093 clusterRequest.setUuid(PortalUUIDUtil.generate());
094
095 return clusterRequest;
096 }
097
098 public void addTargetClusterNodeAddresses(
099 Address... targetClusterNodeAddresses) {
100
101 if (_targetClusterNodeAddresses == null) {
102 _targetClusterNodeAddresses = new HashSet<Address>(
103 targetClusterNodeAddresses.length);
104 }
105
106 for (Address targetClusterNodeAddress : targetClusterNodeAddresses) {
107 _targetClusterNodeAddresses.add(targetClusterNodeAddress);
108 }
109 }
110
111 public void addTargetClusterNodeIds(String... targetClusterNodeIds) {
112 if (_targetClusterNodeIds == null) {
113 _targetClusterNodeIds = new HashSet<String>(
114 targetClusterNodeIds.length);
115 }
116
117 for (String targetClusterNodeId : targetClusterNodeIds) {
118 _targetClusterNodeIds.add(targetClusterNodeId);
119 }
120 }
121
122 public ClusterMessageType getClusterMessageType() {
123 return _clusterMessageType;
124 }
125
126 public MethodHandler getMethodHandler() {
127 return _methodHandler;
128 }
129
130 public ClusterNode getOriginatingClusterNode() {
131 return _originatingClusterNode;
132 }
133
134 public Collection<Address> getTargetClusterNodeAddresses() {
135 return _targetClusterNodeAddresses;
136 }
137
138 public Collection<String> getTargetClusterNodeIds() {
139 return _targetClusterNodeIds;
140 }
141
142 public String getUuid() {
143 return _uuid;
144 }
145
146 public boolean isFireAndForget() {
147 return _fireAndForget;
148 }
149
150 public boolean isMulticast() {
151 return _multicast;
152 }
153
154 public boolean isSkipLocal() {
155 return _skipLocal;
156 }
157
158 public void setClusterMessageType(ClusterMessageType clusterMessageType) {
159 _clusterMessageType = clusterMessageType;
160 }
161
162 public void setFireAndForget(boolean fireAndForget) {
163 _fireAndForget = fireAndForget;
164 }
165
166 public void setMethodHandler(MethodHandler methodHandler) {
167 _methodHandler = methodHandler;
168 }
169
170 public void setMulticast(boolean multicast) {
171 _multicast = multicast;
172 }
173
174 public void setOriginatingClusterNode(ClusterNode originatingClusterNode) {
175 _originatingClusterNode = originatingClusterNode;
176 }
177
178 public void setSkipLocal(boolean skipLocal) {
179 _skipLocal = skipLocal;
180 }
181
182 public void setUuid(String uuid) {
183 _uuid = uuid;
184 }
185
186 @Override
187 public String toString() {
188 StringBundler sb = new StringBundler(11);
189
190 sb.append("{clusterMessageType=");
191 sb.append(_clusterMessageType);
192
193 boolean clusterMessageTypeNotifyOrUpdate = false;
194
195 if (_clusterMessageType.equals(ClusterMessageType.NOTIFY) ||
196 _clusterMessageType.equals(ClusterMessageType.UPDATE)) {
197
198 clusterMessageTypeNotifyOrUpdate = true;
199 }
200
201 if (!clusterMessageTypeNotifyOrUpdate) {
202 sb.append(", methodHandler=");
203 sb.append(_methodHandler);
204 }
205
206 sb.append(", multicast=");
207 sb.append(_multicast);
208
209 if (clusterMessageTypeNotifyOrUpdate) {
210 sb.append(", originatingClusterNode=");
211 sb.append(_originatingClusterNode);
212 }
213
214 sb.append(", skipLocal=");
215 sb.append(_skipLocal);
216 sb.append(", uuid=");
217 sb.append(_uuid);
218 sb.append("}");
219
220 return sb.toString();
221 }
222
223 private ClusterRequest() {
224 }
225
226 private ClusterMessageType _clusterMessageType;
227 private boolean _fireAndForget;
228 private MethodHandler _methodHandler;
229 private boolean _multicast;
230 private ClusterNode _originatingClusterNode;
231 private boolean _skipLocal;
232 private Set<Address> _targetClusterNodeAddresses;
233 private Set<String> _targetClusterNodeIds;
234 private String _uuid;
235
236 }