001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }