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.util.StringBundler;
018    
019    import java.io.Serializable;
020    
021    /**
022     * @author Tina Tian
023     */
024    public class ClusterNodeResponse implements Serializable {
025    
026            public static ClusterNodeResponse createExceptionClusterNodeResponse(
027                    ClusterNode clusterNode, String uuid, Exception exception) {
028    
029                    return new ClusterNodeResponse(clusterNode, uuid, null, exception);
030            }
031    
032            public static ClusterNodeResponse createResultClusterNodeResponse(
033                    ClusterNode clusterNode, String uuid, Object result) {
034    
035                    if ((result != null) && !(result instanceof Serializable)) {
036                            return new ClusterNodeResponse(
037                                    clusterNode, uuid, null,
038                                    new ClusterException("Return value is not serializable"));
039                    }
040    
041                    return new ClusterNodeResponse(clusterNode, uuid, result, null);
042            }
043    
044            public ClusterNode getClusterNode() {
045                    return _clusterNode;
046            }
047    
048            public Exception getException() {
049                    return _exception;
050            }
051    
052            public Object getResult() throws Exception {
053                    if (_exception != null) {
054                            throw _exception;
055                    }
056    
057                    return _result;
058            }
059    
060            public String getUuid() {
061                    return _uuid;
062            }
063    
064            public boolean hasException() {
065                    if (_exception != null) {
066                            return true;
067                    }
068                    else {
069                            return false;
070                    }
071            }
072    
073            @Override
074            public String toString() {
075                    StringBundler sb = new StringBundler(9);
076    
077                    sb.append("{clusterNode=");
078                    sb.append(_clusterNode);
079    
080                    if (hasException()) {
081                            sb.append(", exception=");
082                            sb.append(_exception);
083                    }
084                    else {
085                            sb.append(", result=");
086                            sb.append(_result);
087                    }
088    
089                    sb.append(", uuid=");
090                    sb.append(_uuid);
091                    sb.append("}");
092    
093                    return sb.toString();
094            }
095    
096            private ClusterNodeResponse(
097                    ClusterNode clusterNode, String uuid, Object result,
098                    Exception exception) {
099    
100                    if (clusterNode == null) {
101                            throw new NullPointerException("ClusterNode is null");
102                    }
103    
104                    _clusterNode = clusterNode;
105                    _uuid = uuid;
106                    _result = result;
107                    _exception = exception;
108            }
109    
110            private final ClusterNode _clusterNode;
111            private final Exception _exception;
112            private final Object _result;
113            private final String _uuid;
114    
115    }