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.Future;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.concurrent.TimeUnit;
28
29 import org.apache.directory.api.ldap.model.message.Response;
30 import org.apache.directory.ldap.client.api.LdapConnection;
31
32
33
34
35
36
37
38
39 public class ResponseFuture<R extends Response> implements Future<Response>
40 {
41
42 protected BlockingQueue<R> queue;
43
44
45 protected boolean cancelled = false;
46
47
48 protected Throwable cause;
49
50
51 protected int messageId;
52
53
54 protected LdapConnection connection;
55
56
57
58
59
60
61
62
63 public ResponseFuture( LdapConnection connection, int messageId )
64 {
65 queue = new LinkedBlockingQueue<>();
66 this.messageId = messageId;
67 this.connection = connection;
68 }
69
70
71
72
73
74 @Override
75 public boolean cancel( boolean mayInterruptIfRunning )
76 {
77 if ( cancelled )
78 {
79 return cancelled;
80 }
81
82
83 cancelled = true;
84
85
86 if ( !connection.isRequestCompleted( messageId ) )
87 {
88 connection.abandon( messageId );
89 }
90
91
92
93 queue.clear();
94
95 return cancelled;
96 }
97
98
99
100
101
102
103 @Override
104 public R get() throws InterruptedException
105 {
106 return queue.take();
107 }
108
109
110
111
112
113
114
115
116 public void set( R response ) throws InterruptedException
117 {
118 queue.add( response );
119 }
120
121
122
123
124
125
126 @Override
127 public R get( long timeout, TimeUnit unit ) throws InterruptedException
128 {
129 return queue.poll( timeout, unit );
130 }
131
132
133
134
135
136 @Override
137 public boolean isCancelled()
138 {
139 return cancelled;
140 }
141
142
143
144
145
146
147
148 @Override
149 public boolean isDone()
150 {
151 throw new UnsupportedOperationException( "Operation not supported" );
152 }
153
154
155
156
157
158 public Throwable getCause()
159 {
160 return cause;
161 }
162
163
164
165
166
167
168 public void setCause( Throwable cause )
169 {
170 this.cause = cause;
171 }
172
173
174
175
176
177
178 public void cancel()
179 {
180
181 cancelled = true;
182 }
183
184
185
186
187
188 @Override
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 }