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.fabric.netty.rpc;
016    
017    import com.liferay.portal.fabric.netty.handlers.NettyChannelAttributes;
018    import com.liferay.portal.kernel.concurrent.AsyncBroker;
019    import com.liferay.portal.kernel.concurrent.NoticeableFuture;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    
024    import io.netty.channel.Channel;
025    
026    import java.io.Serializable;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class RPCResponse<T extends Serializable> extends RPCSerializable {
032    
033            public RPCResponse(
034                    long id, boolean cancelled, T result, Throwable throwable) {
035    
036                    super(id);
037    
038                    _cancelled = cancelled;
039                    _result = result;
040                    _throwable = throwable;
041            }
042    
043            @Override
044            public void execute(Channel channel) {
045                    AsyncBroker<Long, Serializable> asyncBroker =
046                            NettyChannelAttributes.getAsyncBroker(channel);
047    
048                    if (_cancelled) {
049                            NoticeableFuture<?> noticeableFuture = asyncBroker.take(id);
050    
051                            if (noticeableFuture == null) {
052                                    _log.error(
053                                            "Unable to place cancellation because no future exists " +
054                                                    "with ID " + id);
055                            }
056                            else if (noticeableFuture.cancel(true)) {
057                                    if (_log.isDebugEnabled()) {
058                                            _log.debug("Cancelled future with ID " + id);
059                                    }
060                            }
061                            else if (_log.isDebugEnabled()) {
062                                    _log.debug(
063                                            "Unable to cancel future with ID " + id +
064                                                    " because it is already completed");
065                            }
066                    }
067                    else if (_throwable != null) {
068                            if (!asyncBroker.takeWithException(id, _throwable)) {
069                                    _log.error(
070                                            "Unable to place exception because no future exists with " +
071                                                    "ID " + id,
072                                            _throwable);
073                            }
074                    }
075                    else {
076                            if (!asyncBroker.takeWithResult(id, _result)) {
077                                    _log.error(
078                                            "Unable to place result " + _result +
079                                                    " because no future exists with ID " + id);
080                            }
081                    }
082            }
083    
084            @Override
085            public String toString() {
086                    StringBundler sb = new StringBundler(9);
087    
088                    sb.append("{cancelled=");
089                    sb.append(_cancelled);
090                    sb.append(", id=");
091                    sb.append(id);
092                    sb.append(", result=");
093                    sb.append(_result);
094                    sb.append(", throwable=");
095                    sb.append(_throwable);
096                    sb.append("}");
097    
098                    return sb.toString();
099            }
100    
101            private static final Log _log = LogFactoryUtil.getLog(RPCResponse.class);
102    
103            private static final long serialVersionUID = 1L;
104    
105            private final boolean _cancelled;
106            private final T _result;
107            private final Throwable _throwable;
108    
109    }