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 public AbstractProxyConnection(final Connection proxiedConnection) {
54 myProxiedConnection = proxiedConnection;
55 }
56
57
58
59
60
61
62
63
64 @Override
65 public void addPropertyChangeListener(final PropertyChangeListener listener) {
66 try {
67 myProxiedConnection
68 .addPropertyChangeListener(new ProxiedChangeListener(this,
69 listener));
70 }
71 catch (final MongoDbException error) {
72 onExceptin(error);
73 listener.propertyChange(new PropertyChangeEvent(this,
74 OPEN_PROP_NAME, Boolean.TRUE, Boolean.FALSE));
75 }
76 }
77
78
79
80
81
82
83 @Override
84 public void close() throws IOException {
85 myProxiedConnection.close();
86 }
87
88
89
90
91
92
93
94
95
96 @Override
97 public void flush() throws IOException {
98 try {
99 myProxiedConnection.flush();
100 }
101 catch (final MongoDbException error) {
102 onExceptin(error);
103 throw error;
104 }
105 }
106
107
108
109
110
111
112
113 @Override
114 public int getPendingCount() {
115 try {
116 return myProxiedConnection.getPendingCount();
117 }
118 catch (final MongoDbException error) {
119 onExceptin(error);
120 throw error;
121 }
122 }
123
124
125
126
127
128
129
130 @Override
131 public String getServerName() {
132 return getProxiedConnection().getServerName();
133 }
134
135
136
137
138
139
140
141 @Override
142 public boolean isAvailable() {
143 try {
144 return myProxiedConnection.isAvailable();
145 }
146 catch (final MongoDbException error) {
147 onExceptin(error);
148 throw error;
149 }
150 }
151
152
153
154
155
156
157
158 @Override
159 public boolean isIdle() {
160 try {
161 return myProxiedConnection.isIdle();
162 }
163 catch (final MongoDbException error) {
164 onExceptin(error);
165 throw error;
166 }
167 }
168
169
170
171
172
173
174
175 @Override
176 public boolean isOpen() {
177 try {
178 return myProxiedConnection.isOpen();
179 }
180 catch (final MongoDbException error) {
181 onExceptin(error);
182 throw error;
183 }
184 }
185
186
187
188
189
190
191
192 @Override
193 public boolean isShuttingDown() {
194 return myProxiedConnection.isShuttingDown();
195 }
196
197
198
199
200
201
202
203 @Override
204 public void raiseErrors(final MongoDbException exception) {
205 myProxiedConnection.raiseErrors(exception);
206 }
207
208
209
210
211
212
213
214
215 @Override
216 public void removePropertyChangeListener(
217 final PropertyChangeListener listener) {
218 myProxiedConnection
219 .removePropertyChangeListener(new ProxiedChangeListener(this,
220 listener));
221 }
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 myProxiedConnection.send(message1, message2, replyCallback);
234 }
235 catch (final MongoDbException error) {
236 onExceptin(error);
237 throw error;
238 }
239 }
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 myProxiedConnection.send(message, replyCallback);
252 }
253 catch (final MongoDbException error) {
254 onExceptin(error);
255 throw error;
256 }
257 }
258
259
260
261
262
263
264
265 @Override
266 public void shutdown(final boolean force) {
267 myProxiedConnection.shutdown(force);
268 }
269
270
271
272
273
274
275
276 @Override
277 public void waitForClosed(final int timeout, final TimeUnit timeoutUnits) {
278 try {
279 myProxiedConnection.waitForClosed(timeout, timeoutUnits);
280 }
281 catch (final MongoDbException error) {
282 onExceptin(error);
283 throw error;
284 }
285 }
286
287
288
289
290
291
292 protected Connection getProxiedConnection() {
293 return myProxiedConnection;
294 }
295
296
297
298
299
300
301
302
303
304
305
306 protected void onExceptin(final MongoDbException exception) {
307
308 IOUtils.close(this);
309 }
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 final PropertyChangeListener delegate) {
336 myProxiedConn = proxiedConn;
337 myDelegate = delegate;
338 }
339
340
341
342
343
344
345
346 @Override
347 public boolean equals(final Object object) {
348 boolean result = false;
349 if (this == object) {
350 result = true;
351 }
352 else if ((object != null) && (getClass() == object.getClass())) {
353 final ProxiedChangeListener other = (ProxiedChangeListener) object;
354
355 result = myDelegate.equals(other.myDelegate);
356 }
357 return result;
358 }
359
360
361
362
363
364
365
366 @Override
367 public int hashCode() {
368 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 final PropertyChangeEvent newEvent = new PropertyChangeEvent(
382 myProxiedConn, event.getPropertyName(),
383 event.getOldValue(), event.getNewValue());
384 newEvent.setPropagationId(event.getPropagationId());
385 myDelegate.propertyChange(newEvent);
386 }
387 }
388 }