1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.cluster;
16  
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Set;
20  import java.util.concurrent.CancellationException;
21  import java.util.concurrent.CountDownLatch;
22  import java.util.concurrent.Future;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.TimeoutException;
25  
26  /**
27   * <a href="FutureClusterResponses.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Tina Tian
30   */
31  public class FutureClusterResponses implements Future<ClusterNodeResponses> {
32  
33      public FutureClusterResponses(List<Address> addresses) {
34          _clusterNodeResponses = new ClusterNodeResponses();
35          _countDownLatch = new CountDownLatch(addresses.size());
36          _expectedReplyAddress = new HashSet<Address>(addresses);
37      }
38  
39      public void addClusterNodeResponse(
40          ClusterNodeResponse clusterNodeResponse) {
41  
42          _clusterNodeResponses.addClusterResponse(clusterNodeResponse);
43  
44          _countDownLatch.countDown();
45      }
46  
47      public void addExpectedReplyAddress(Address address) {
48          _expectedReplyAddress.add(address);
49      }
50  
51      public boolean cancel(boolean mayInterruptIfRunning) {
52          if (_cancelled || isDone()) {
53              return false;
54          }
55  
56          _cancelled = true;
57  
58          return true;
59      }
60  
61      public boolean expectsReply(Address address) {
62          return _expectedReplyAddress.contains(address);
63      }
64  
65      public ClusterNodeResponses get() throws InterruptedException {
66          if (_cancelled) {
67              throw new CancellationException();
68          }
69  
70          _countDownLatch.await();
71  
72          return _clusterNodeResponses;
73      }
74  
75      public ClusterNodeResponses get(long timeout, TimeUnit timeUnit)
76          throws InterruptedException, TimeoutException {
77  
78          if (_cancelled) {
79              throw new CancellationException();
80          }
81  
82          if (_countDownLatch.await(timeout, timeUnit)) {
83              return _clusterNodeResponses;
84          }
85          else {
86              throw new TimeoutException();
87          }
88      }
89  
90      public ClusterNodeResponses getPartialResults() {
91          return _clusterNodeResponses;
92      }
93  
94      public boolean isCancelled() {
95          return _cancelled;
96      }
97  
98      public boolean isDone() {
99          if ((_countDownLatch.getCount() == 0) || _cancelled) {
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106 
107     private boolean _cancelled;
108     private ClusterNodeResponses _clusterNodeResponses;
109     private CountDownLatch _countDownLatch;
110     private Set<Address> _expectedReplyAddress;
111 
112 }