1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package com.allanbank.mongodb.client.connection.proxy; |
22 | |
|
23 | |
import java.beans.PropertyChangeEvent; |
24 | |
import java.beans.PropertyChangeListener; |
25 | |
import java.io.IOException; |
26 | |
import java.util.concurrent.TimeUnit; |
27 | |
|
28 | |
import com.allanbank.mongodb.MongoDbException; |
29 | |
import com.allanbank.mongodb.client.Message; |
30 | |
import com.allanbank.mongodb.client.callback.ReplyCallback; |
31 | |
import com.allanbank.mongodb.client.connection.Connection; |
32 | |
import com.allanbank.mongodb.util.IOUtils; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public abstract class AbstractProxyConnection implements Connection { |
43 | |
|
44 | |
|
45 | |
private final Connection myProxiedConnection; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 46 | public AbstractProxyConnection(final Connection proxiedConnection) { |
54 | 46 | myProxiedConnection = proxiedConnection; |
55 | 46 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Override |
65 | |
public void addPropertyChangeListener(final PropertyChangeListener listener) { |
66 | |
try { |
67 | 2 | myProxiedConnection |
68 | |
.addPropertyChangeListener(new ProxiedChangeListener(this, |
69 | |
listener)); |
70 | |
} |
71 | 1 | catch (final MongoDbException error) { |
72 | 1 | onExceptin(error); |
73 | 1 | listener.propertyChange(new PropertyChangeEvent(this, |
74 | |
OPEN_PROP_NAME, Boolean.TRUE, Boolean.FALSE)); |
75 | 1 | } |
76 | 2 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
@Override |
84 | |
public void close() throws IOException { |
85 | 50 | myProxiedConnection.close(); |
86 | 50 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
@Override |
97 | |
public void flush() throws IOException { |
98 | |
try { |
99 | 2 | myProxiedConnection.flush(); |
100 | |
} |
101 | 1 | catch (final MongoDbException error) { |
102 | 1 | onExceptin(error); |
103 | 1 | throw error; |
104 | 1 | } |
105 | 1 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
@Override |
114 | |
public int getPendingCount() { |
115 | |
try { |
116 | 2 | return myProxiedConnection.getPendingCount(); |
117 | |
} |
118 | 1 | catch (final MongoDbException error) { |
119 | 1 | onExceptin(error); |
120 | 1 | throw error; |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
@Override |
131 | |
public String getServerName() { |
132 | 1 | return getProxiedConnection().getServerName(); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
@Override |
142 | |
public boolean isAvailable() { |
143 | |
try { |
144 | 2 | return myProxiedConnection.isAvailable(); |
145 | |
} |
146 | 1 | catch (final MongoDbException error) { |
147 | 1 | onExceptin(error); |
148 | 1 | throw error; |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
@Override |
159 | |
public boolean isIdle() { |
160 | |
try { |
161 | 2 | return myProxiedConnection.isIdle(); |
162 | |
} |
163 | 1 | catch (final MongoDbException error) { |
164 | 1 | onExceptin(error); |
165 | 1 | throw error; |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
@Override |
176 | |
public boolean isOpen() { |
177 | |
try { |
178 | 2 | return myProxiedConnection.isOpen(); |
179 | |
} |
180 | 1 | catch (final MongoDbException error) { |
181 | 1 | onExceptin(error); |
182 | 1 | throw error; |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
@Override |
193 | |
public boolean isShuttingDown() { |
194 | 0 | return myProxiedConnection.isShuttingDown(); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
@Override |
204 | |
public void raiseErrors(final MongoDbException exception) { |
205 | 1 | myProxiedConnection.raiseErrors(exception); |
206 | 1 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Override |
216 | |
public void removePropertyChangeListener( |
217 | |
final PropertyChangeListener listener) { |
218 | 1 | myProxiedConnection |
219 | |
.removePropertyChangeListener(new ProxiedChangeListener(this, |
220 | |
listener)); |
221 | 1 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
@Override |
230 | |
public void send(final Message message1, final Message message2, |
231 | |
final ReplyCallback replyCallback) throws MongoDbException { |
232 | |
try { |
233 | 3 | myProxiedConnection.send(message1, message2, replyCallback); |
234 | |
} |
235 | 1 | catch (final MongoDbException error) { |
236 | 1 | onExceptin(error); |
237 | 1 | throw error; |
238 | 2 | } |
239 | 2 | } |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
@Override |
248 | |
public void send(final Message message, final ReplyCallback replyCallback) |
249 | |
throws MongoDbException { |
250 | |
try { |
251 | 10 | myProxiedConnection.send(message, replyCallback); |
252 | |
} |
253 | 1 | catch (final MongoDbException error) { |
254 | 1 | onExceptin(error); |
255 | 1 | throw error; |
256 | 9 | } |
257 | 9 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
@Override |
266 | |
public void shutdown(final boolean force) { |
267 | 1 | myProxiedConnection.shutdown(force); |
268 | 1 | } |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
@Override |
277 | |
public void waitForClosed(final int timeout, final TimeUnit timeoutUnits) { |
278 | |
try { |
279 | 2 | myProxiedConnection.waitForClosed(timeout, timeoutUnits); |
280 | |
} |
281 | 1 | catch (final MongoDbException error) { |
282 | 1 | onExceptin(error); |
283 | 1 | throw error; |
284 | 1 | } |
285 | 1 | } |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
protected Connection getProxiedConnection() { |
293 | 5 | return myProxiedConnection; |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
protected void onExceptin(final MongoDbException exception) { |
307 | |
|
308 | 9 | IOUtils.close(this); |
309 | 9 | } |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
protected static class ProxiedChangeListener implements |
318 | |
PropertyChangeListener { |
319 | |
|
320 | |
|
321 | |
private final PropertyChangeListener myDelegate; |
322 | |
|
323 | |
|
324 | |
private final AbstractProxyConnection myProxiedConn; |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public ProxiedChangeListener(final AbstractProxyConnection proxiedConn, |
335 | 10 | final PropertyChangeListener delegate) { |
336 | 10 | myProxiedConn = proxiedConn; |
337 | 10 | myDelegate = delegate; |
338 | 10 | } |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
@Override |
347 | |
public boolean equals(final Object object) { |
348 | 7 | boolean result = false; |
349 | 7 | if (this == object) { |
350 | 1 | result = true; |
351 | |
} |
352 | 6 | else if ((object != null) && (getClass() == object.getClass())) { |
353 | 4 | final ProxiedChangeListener other = (ProxiedChangeListener) object; |
354 | |
|
355 | 4 | result = myDelegate.equals(other.myDelegate); |
356 | |
} |
357 | 7 | return result; |
358 | |
} |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
@Override |
367 | |
public int hashCode() { |
368 | 3 | return ((myDelegate == null) ? 13 : myDelegate.hashCode()); |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
@Override |
379 | |
public void propertyChange(final PropertyChangeEvent event) { |
380 | |
|
381 | 1 | final PropertyChangeEvent newEvent = new PropertyChangeEvent( |
382 | |
myProxiedConn, event.getPropertyName(), |
383 | |
event.getOldValue(), event.getNewValue()); |
384 | 1 | newEvent.setPropagationId(event.getPropagationId()); |
385 | 1 | myDelegate.propertyChange(newEvent); |
386 | 1 | } |
387 | |
} |
388 | |
} |