001    /**
002     * Copyright (c) 2000-2010 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 java.util.HashSet;
018    import java.util.List;
019    import java.util.Set;
020    import java.util.concurrent.CancellationException;
021    import java.util.concurrent.CountDownLatch;
022    import java.util.concurrent.Future;
023    import java.util.concurrent.TimeUnit;
024    import java.util.concurrent.TimeoutException;
025    
026    /**
027     * @author Tina Tian
028     */
029    public class FutureClusterResponses implements Future<ClusterNodeResponses> {
030    
031            public FutureClusterResponses(List<Address> addresses) {
032                    _clusterNodeResponses = new ClusterNodeResponses();
033                    _countDownLatch = new CountDownLatch(addresses.size());
034                    _expectedReplyAddress = new HashSet<Address>(addresses);
035            }
036    
037            public void addClusterNodeResponse(
038                    ClusterNodeResponse clusterNodeResponse) {
039    
040                    _clusterNodeResponses.addClusterResponse(clusterNodeResponse);
041    
042                    _countDownLatch.countDown();
043            }
044    
045            public void addExpectedReplyAddress(Address address) {
046                    _expectedReplyAddress.add(address);
047            }
048    
049            public boolean cancel(boolean mayInterruptIfRunning) {
050                    if (_cancelled || isDone()) {
051                            return false;
052                    }
053    
054                    _cancelled = true;
055    
056                    return true;
057            }
058    
059            public boolean expectsReply(Address address) {
060                    return _expectedReplyAddress.contains(address);
061            }
062    
063            public ClusterNodeResponses get() throws InterruptedException {
064                    if (_cancelled) {
065                            throw new CancellationException();
066                    }
067    
068                    _countDownLatch.await();
069    
070                    return _clusterNodeResponses;
071            }
072    
073            public ClusterNodeResponses get(long timeout, TimeUnit timeUnit)
074                    throws InterruptedException, TimeoutException {
075    
076                    if (_cancelled) {
077                            throw new CancellationException();
078                    }
079    
080                    if (_countDownLatch.await(timeout, timeUnit)) {
081                            return _clusterNodeResponses;
082                    }
083                    else {
084                            throw new TimeoutException();
085                    }
086            }
087    
088            public ClusterNodeResponses getPartialResults() {
089                    return _clusterNodeResponses;
090            }
091    
092            public boolean isCancelled() {
093                    return _cancelled;
094            }
095    
096            public boolean isDone() {
097                    if ((_countDownLatch.getCount() == 0) || _cancelled) {
098                            return true;
099                    }
100                    else {
101                            return false;
102                    }
103            }
104    
105            private boolean _cancelled;
106            private ClusterNodeResponses _clusterNodeResponses;
107            private CountDownLatch _countDownLatch;
108            private Set<Address> _expectedReplyAddress;
109    
110    }