Skip to content

Commit 50daafb

Browse files
committed
Allow contexts to be disposed more than once
Subsequent disposals are no-ops, rather than throwing an exception.
1 parent 545caf0 commit 50daafb

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

src/main/java/org/scijava/Context.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public class Context implements Disposable, AutoCloseable {
9797
*/
9898
private boolean strict;
9999

100+
/**
101+
* False if the context is currently active; true if the context
102+
* has already been disposed, or is in the process of being disposed.
103+
*/
104+
private boolean disposed;
105+
100106
/**
101107
* Creates a new SciJava application context with all available services.
102108
*
@@ -418,16 +424,8 @@ public boolean isInjectable(final Class<?> type) {
418424

419425
@Override
420426
public void dispose() {
421-
final EventService eventService = getService(EventService.class);
422-
if (eventService != null) eventService.publish(new ContextDisposingEvent());
423-
424-
// NB: Dispose services in reverse order.
425-
// This may or may not actually be necessary, but seems safer, since
426-
// dependent services will be disposed *before* their dependencies.
427-
final List<Service> services = serviceIndex.getAll();
428-
for (int s = services.size() - 1; s >= 0; s--) {
429-
services.get(s).dispose();
430-
}
427+
if (disposed) return;
428+
doDispose(true);
431429
}
432430

433431
// -- AutoCloseable methods --
@@ -580,6 +578,23 @@ private String createMissingServiceMessage(
580578
return msg.toString();
581579
}
582580

581+
private synchronized void doDispose(final boolean announce) {
582+
if (disposed) return;
583+
disposed = true;
584+
if (announce) {
585+
final EventService eventService = getService(EventService.class);
586+
if (eventService != null) eventService.publish(new ContextDisposingEvent());
587+
}
588+
589+
// NB: Dispose services in reverse order.
590+
// This may or may not actually be necessary, but seems safer, since
591+
// dependent services will be disposed *before* their dependencies.
592+
final List<Service> services = serviceIndex.getAll();
593+
for (int s = services.size() - 1; s >= 0; s--) {
594+
services.get(s).dispose();
595+
}
596+
}
597+
583598
private static PluginIndex plugins(final boolean empty) {
584599
return empty ? new PluginIndex(null) : null;
585600
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava;
31+
32+
import org.junit.Test;
33+
34+
/**
35+
* Tests disposal of {@link Context}s.
36+
*
37+
* @author Curtis Rueden
38+
*/
39+
public class ContextDisposalTest {
40+
41+
/**
42+
* Tests that a {@link Context} can be disposed more than once without
43+
* throwing an exception.
44+
*/
45+
@Test
46+
public void testDoubleDisposal() {
47+
final Context context = new Context();
48+
context.dispose();
49+
context.dispose();
50+
}
51+
}

0 commit comments

Comments
 (0)