4949
5050public class NextcloudConnector implements AutoCloseable {
5151
52+ /**
53+ * Number of open connectors sharing the static HTTP client, so it is only
54+ * shut down once the last connector is closed (see issue #87).
55+ */
56+ private static final java .util .concurrent .atomic .AtomicInteger OPEN_INSTANCES =
57+ new java .util .concurrent .atomic .AtomicInteger (0 );
58+
59+ private volatile boolean closed = false ;
60+
5261 private final ServerConfig serverConfig ;
5362 private final ProvisionConnector pc ;
5463 private final FilesharingConnector fc ;
@@ -121,6 +130,7 @@ public NextcloudConnector(String originalServiceUrl, AuthenticationConfig authen
121130 fl = new Files (this .serverConfig );
122131 gf = new GroupFolders (this .serverConfig );
123132 st = new SystemTags (this .serverConfig );
133+ OPEN_INSTANCES .incrementAndGet ();
124134
125135 } catch (MalformedURLException e ) {
126136 throw new IllegalArgumentException (e );
@@ -144,6 +154,7 @@ public NextcloudConnector(String serverName, boolean useHTTPS, int port,
144154 fl = new Files (this .serverConfig );
145155 gf = new GroupFolders (this .serverConfig );
146156 st = new SystemTags (this .serverConfig );
157+ OPEN_INSTANCES .incrementAndGet ();
147158 }
148159
149160 /**
@@ -193,8 +204,11 @@ public void setWebDavPathResolverAsType(final WebDavPathResolverBuilder.TYPE typ
193204 }
194205
195206 /**
196- * Close the HTTP client. Perform this to cleanly shut down this
197- * application.
207+ * Immediately shuts down the shared HTTP client, regardless of how many
208+ * other {@link NextcloudConnector} instances are still open. Prefer
209+ * {@link #close()} (e.g. via try-with-resources), which only shuts the
210+ * shared client down once the last connector is closed. Use this only when
211+ * you explicitly want to tear everything down at once.
198212 *
199213 * @throws IOException In case of IO errors
200214 */
@@ -401,13 +415,22 @@ public void setGroupFolderQuota(int groupFolderId, long quota) {
401415 }
402416
403417 /**
404- * Close the HTTP client. Perform this to cleanly shut down this
405- * application.
418+ * Closes this connector. The shared HTTP client is only shut down once the
419+ * last open {@link NextcloudConnector} has been closed, so closing one
420+ * connector no longer breaks others that are still in use (see issue #87).
421+ * Idempotent: closing an already-closed connector does nothing.
406422 *
407423 * @throws Exception In case of errors
408424 */
425+ @ Override
409426 public void close () throws Exception {
410- shutdown ();
427+ if (closed ) {
428+ return ;
429+ }
430+ closed = true ;
431+ if (OPEN_INSTANCES .decrementAndGet () <= 0 ) {
432+ shutdown ();
433+ }
411434 }
412435
413436 /**
0 commit comments