Skip to content

Commit

Permalink
Add a ContextCreatedEvent
Browse files Browse the repository at this point in the history
So that services can respond with behavior as soon as their context is
fully done initializing.
  • Loading branch information
ctrueden committed Oct 13, 2023
1 parent c1c5e24 commit 9960d51
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>scijava-common</artifactId>
<version>2.97.2-SNAPSHOT</version>
<version>2.98.0-SNAPSHOT</version>

<name>SciJava Common</name>
<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>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/scijava/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -293,6 +294,10 @@ public Context(final Collection<Class<? extends Service>> 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 --
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/scijava/event/ContextCreatedEvent.java
Original file line number Diff line number Diff line change
@@ -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 { }
22 changes: 22 additions & 0 deletions src/test/java/org/scijava/ContextCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 9960d51

Please sign in to comment.