|
26 | 26 | import ome.formats.OMEROMetadataStoreClient; |
27 | 27 | import omero.api.IQueryPrx; |
28 | 28 | import omero.gateway.Gateway; |
29 | | -import omero.gateway.JoinSessionCredentials; |
30 | 29 | import omero.gateway.LoginCredentials; |
31 | 30 | import omero.gateway.SecurityContext; |
32 | 31 | import omero.gateway.exception.DSOutOfServiceException; |
|
43 | 42 |
|
44 | 43 | import java.util.List; |
45 | 44 | import java.util.concurrent.ExecutionException; |
| 45 | +import java.util.concurrent.atomic.AtomicInteger; |
| 46 | +import java.util.concurrent.locks.Lock; |
| 47 | +import java.util.concurrent.locks.ReentrantLock; |
46 | 48 |
|
47 | 49 |
|
48 | 50 | /** |
|
52 | 54 | */ |
53 | 55 | public abstract class GatewayWrapper { |
54 | 56 |
|
| 57 | + /** Number of requested import stores */ |
| 58 | + private final AtomicInteger storeUses = new AtomicInteger(0); |
| 59 | + |
| 60 | + /** Import store lock */ |
| 61 | + private final Lock storeLock = new ReentrantLock(true); |
| 62 | + |
55 | 63 | /** Gateway linking the code to OMERO, only linked to one group. */ |
56 | 64 | private Gateway gateway; |
57 | 65 |
|
@@ -87,6 +95,35 @@ public Gateway getGateway() { |
87 | 95 | } |
88 | 96 |
|
89 | 97 |
|
| 98 | + /** |
| 99 | + * Retrieves the shared import store in a thread-safe way. |
| 100 | + * |
| 101 | + * @throws DSOutOfServiceException If the connection is broken, or not logged in. |
| 102 | + */ |
| 103 | + private OMEROMetadataStoreClient getImportStoreLocked() throws DSOutOfServiceException { |
| 104 | + storeLock.lock(); |
| 105 | + try { |
| 106 | + return gateway.getImportStore(ctx); |
| 107 | + } finally { |
| 108 | + storeLock.unlock(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Closes the import store in a thread-safe manner. |
| 115 | + */ |
| 116 | + private void closeImportStoreLocked() { |
| 117 | + if (storeLock.tryLock()) { |
| 118 | + try { |
| 119 | + gateway.closeImport(ctx, null); |
| 120 | + } finally { |
| 121 | + storeLock.unlock(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
90 | 127 | /** |
91 | 128 | * Returns the current user. |
92 | 129 | * |
@@ -163,7 +200,7 @@ public boolean isConnected() { |
163 | 200 | */ |
164 | 201 | public void connect(String hostname, int port, String sessionId) |
165 | 202 | throws ServiceException { |
166 | | - connect(new JoinSessionCredentials(sessionId, hostname, port)); |
| 203 | + connect(new LoginCredentials(sessionId, sessionId, hostname, port)); |
167 | 204 | } |
168 | 205 |
|
169 | 206 |
|
@@ -233,6 +270,8 @@ public void connect(LoginCredentials cred) throws ServiceException { |
233 | 270 | public void disconnect() { |
234 | 271 | if (isConnected()) { |
235 | 272 | boolean sudo = ctx.isSudo(); |
| 273 | + storeUses.set(0); |
| 274 | + closeImport(); |
236 | 275 | user = new ExperimenterWrapper(new ExperimenterData()); |
237 | 276 | ctx = new SecurityContext(-1); |
238 | 277 | ctx.setExperimenter(user.asDataObject()); |
@@ -348,18 +387,30 @@ public AdminFacility getAdminFacility() throws ExecutionException { |
348 | 387 | /** |
349 | 388 | * Creates or recycles the import store. |
350 | 389 | * |
351 | | - * @return config. |
| 390 | + * @return See above. |
352 | 391 | * |
353 | 392 | * @throws ServiceException Cannot connect to OMERO. |
354 | 393 | */ |
355 | 394 | public OMEROMetadataStoreClient getImportStore() throws ServiceException { |
356 | | - return ExceptionHandler.of(gateway, g -> g.getImportStore(ctx)) |
| 395 | + storeUses.incrementAndGet(); |
| 396 | + return ExceptionHandler.of(this, GatewayWrapper::getImportStoreLocked) |
357 | 397 | .rethrow(DSOutOfServiceException.class, ServiceException::new, |
358 | 398 | "Could not retrieve import store") |
359 | 399 | .get(); |
360 | 400 | } |
361 | 401 |
|
362 | 402 |
|
| 403 | + /** |
| 404 | + * Closes the import store. |
| 405 | + */ |
| 406 | + public void closeImport() { |
| 407 | + int remainingStores = storeUses.decrementAndGet(); |
| 408 | + if (remainingStores <= 0) { |
| 409 | + closeImportStoreLocked(); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + |
363 | 414 | /** |
364 | 415 | * Finds objects on OMERO through a database query. |
365 | 416 | * |
|
0 commit comments