diff --git a/pom.xml b/pom.xml index aede49815..4b91cb7b3 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ scijava-common - 2.97.2-SNAPSHOT + 2.98.0-SNAPSHOT SciJava Common 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. diff --git a/src/main/java/org/scijava/Context.java b/src/main/java/org/scijava/Context.java index 63ade880d..d6b777d46 100644 --- a/src/main/java/org/scijava/Context.java +++ b/src/main/java/org/scijava/Context.java @@ -38,6 +38,7 @@ import java.util.Collections; import java.util.List; +import org.scijava.event.ContextCreatedEvent; import org.scijava.event.ContextDisposingEvent; import org.scijava.event.EventHandler; import org.scijava.event.EventService; @@ -293,6 +294,10 @@ public Context(final Collection> serviceClasses, // If JVM shuts down with context still active, clean up after ourselves. Runtime.getRuntime().addShutdownHook(new Thread(() -> doDispose(false))); + + // Publish an event to indicate that context initialization is complete. + final EventService eventService = getService(EventService.class); + if (eventService != null) eventService.publish(new ContextCreatedEvent()); } // -- Context methods -- diff --git a/src/main/java/org/scijava/event/ContextCreatedEvent.java b/src/main/java/org/scijava/event/ContextCreatedEvent.java new file mode 100644 index 000000000..7ca7d529b --- /dev/null +++ b/src/main/java/org/scijava/event/ContextCreatedEvent.java @@ -0,0 +1,38 @@ +/* + * #%L + * SciJava Common shared library for SciJava software. + * %% + * Copyright (C) 2009 - 2023 SciJava developers. + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +package org.scijava.event; + +/** + * Event to be published immediately after a context has been fully created + * with all services initialized. + * + * @author Curtis Rueden + */ +public class ContextCreatedEvent extends SciJavaEvent { } diff --git a/src/test/java/org/scijava/ContextCreationTest.java b/src/test/java/org/scijava/ContextCreationTest.java index b177a75c7..db4e76ea7 100644 --- a/src/test/java/org/scijava/ContextCreationTest.java +++ b/src/test/java/org/scijava/ContextCreationTest.java @@ -42,6 +42,8 @@ import java.util.List; import org.junit.Test; +import org.scijava.event.ContextCreatedEvent; +import org.scijava.event.EventHandler; import org.scijava.plugin.Parameter; import org.scijava.plugin.PluginIndex; import org.scijava.plugin.PluginInfo; @@ -149,6 +151,14 @@ public void testSciJavaServices() { } } + /** Tests that {@link ContextCreatedEvent} is published as expected. */ + @Test + public void testContextCreatedEvent() { + assertEquals(0, ServiceNoticingContextCreated.created); + final Context context = new Context(ServiceNoticingContextCreated.class); + assertEquals(1, ServiceNoticingContextCreated.created); + } + /** * Tests that dependent {@link Service}s are automatically created and * populated in downstream {@link Service} classes. @@ -441,6 +451,18 @@ private PluginIndex pluginIndex(final Class... plugins) { // -- Helper classes -- + /** A service that notices when {@link ContextCreatedEvent} is published. */ + public static class ServiceNoticingContextCreated extends AbstractService { + + public static int created = 0; + + @EventHandler + public void onEvent(final ContextCreatedEvent evt) { + created++; + } + + } + /** A service which requires a {@link BarService}. */ public static class FooService extends AbstractService {