1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.ldap.client.api.future;
22
23
24 import java.util.concurrent.BlockingQueue;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30
31 import org.apache.directory.api.ldap.model.message.Response;
32 import org.apache.directory.ldap.client.api.LdapConnection;
33
34
35
36
37
38
39
40
41 public class ResponseFuture<R extends Response> implements Future<Response>
42 {
43
44 protected BlockingQueue<R> queue;
45
46
47 protected boolean cancelled = false;
48
49
50 protected Throwable cause;
51
52
53 protected int messageId;
54
55
56 protected LdapConnection connection;
57
58
59
60
61
62
63
64
65 public ResponseFuture( LdapConnection connection, int messageId )
66 {
67 queue = new LinkedBlockingQueue<R>();
68 this.messageId = messageId;
69 this.connection = connection;
70 }
71
72
73
74
75
76 public boolean cancel( boolean mayInterruptIfRunning )
77 {
78 if ( cancelled )
79 {
80 return cancelled;
81 }
82
83
84 cancelled = true;
85
86
87 if ( connection.doesFutureExistFor( messageId ) )
88 {
89 connection.abandon( messageId );
90 }
91
92
93
94 queue.clear();
95
96 return cancelled;
97 }
98
99
100
101
102
103
104 public R get() throws InterruptedException, ExecutionException
105 {
106 R response = null;
107
108 response = queue.take();
109
110 return response;
111 }
112
113
114
115
116
117
118 public void set( R response ) throws InterruptedException, ExecutionException
119 {
120 queue.add( response );
121 }
122
123
124
125
126
127
128 public R get( long timeout, TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException
129 {
130 R response = queue.poll( timeout, unit );
131
132 return response;
133 }
134
135
136
137
138
139 public boolean isCancelled()
140 {
141 return cancelled;
142 }
143
144
145
146
147
148
149
150 public boolean isDone()
151 {
152 throw new UnsupportedOperationException( "Operation not supported" );
153 }
154
155
156
157
158
159 public Throwable getCause()
160 {
161 return cause;
162 }
163
164
165
166
167
168
169 public void setCause( Throwable cause )
170 {
171 this.cause = cause;
172 }
173
174
175
176
177
178
179 public void cancel()
180 {
181
182 cancelled = true;
183 }
184
185
186
187
188
189 public String toString()
190 {
191 StringBuilder sb = new StringBuilder();
192
193 sb.append( "[msgId : " ).append( messageId ).append( ", " );
194 sb.append( "size : " ).append( queue.size() ).append( ", " );
195 sb.append( "Canceled :" ).append( cancelled ).append( "]" );
196
197 return sb.toString();
198 }
199 }