Skip to content

Commit 9960d51

Browse files
committed
Add a ContextCreatedEvent
So that services can respond with behavior as soon as their context is fully done initializing.
1 parent c1c5e24 commit 9960d51

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.97.2-SNAPSHOT</version>
13+
<version>2.98.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collections;
3939
import java.util.List;
4040

41+
import org.scijava.event.ContextCreatedEvent;
4142
import org.scijava.event.ContextDisposingEvent;
4243
import org.scijava.event.EventHandler;
4344
import org.scijava.event.EventService;
@@ -293,6 +294,10 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
293294

294295
// If JVM shuts down with context still active, clean up after ourselves.
295296
Runtime.getRuntime().addShutdownHook(new Thread(() -> doDispose(false)));
297+
298+
// Publish an event to indicate that context initialization is complete.
299+
final EventService eventService = getService(EventService.class);
300+
if (eventService != null) eventService.publish(new ContextCreatedEvent());
296301
}
297302

298303
// -- Context methods --
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.event;
31+
32+
/**
33+
* Event to be published immediately after a context has been fully created
34+
* with all services initialized.
35+
*
36+
* @author Curtis Rueden
37+
*/
38+
public class ContextCreatedEvent extends SciJavaEvent { }

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.List;
4343

4444
import org.junit.Test;
45+
import org.scijava.event.ContextCreatedEvent;
46+
import org.scijava.event.EventHandler;
4547
import org.scijava.plugin.Parameter;
4648
import org.scijava.plugin.PluginIndex;
4749
import org.scijava.plugin.PluginInfo;
@@ -149,6 +151,14 @@ public void testSciJavaServices() {
149151
}
150152
}
151153

154+
/** Tests that {@link ContextCreatedEvent} is published as expected. */
155+
@Test
156+
public void testContextCreatedEvent() {
157+
assertEquals(0, ServiceNoticingContextCreated.created);
158+
final Context context = new Context(ServiceNoticingContextCreated.class);
159+
assertEquals(1, ServiceNoticingContextCreated.created);
160+
}
161+
152162
/**
153163
* Tests that dependent {@link Service}s are automatically created and
154164
* populated in downstream {@link Service} classes.
@@ -441,6 +451,18 @@ private PluginIndex pluginIndex(final Class<?>... plugins) {
441451

442452
// -- Helper classes --
443453

454+
/** A service that notices when {@link ContextCreatedEvent} is published. */
455+
public static class ServiceNoticingContextCreated extends AbstractService {
456+
457+
public static int created = 0;
458+
459+
@EventHandler
460+
public void onEvent(final ContextCreatedEvent evt) {
461+
created++;
462+
}
463+
464+
}
465+
444466
/** A service which requires a {@link BarService}. */
445467
public static class FooService extends AbstractService {
446468

0 commit comments

Comments
 (0)