Skip to content

Commit caa9640

Browse files
ctruedenclaude
andcommitted
scijava-context: the container core
Phase 2 begins: Service, Context, lazy creation, priority selection, lifecycle, disposal, and one EventBus per context. Services are discovered as Service, never as their own interface, and this is forced rather than chosen: ServiceLoader resolves `uses` against its caller's module, so a container cannot look up an arbitrary third-party interface on that module's behalf -- it would have to declare `uses com.example.LogService`, which it cannot know. Declaring one base type that this module does `uses` is what makes discovery work under JPMS at all; the container then sorts implementations by the interfaces they implement. Provider.type() keeps that cheap, since implementations of other types are never constructed. Services are created lazily, where SciJava Common built every service it could find at context creation. Asking for one constructs it, initializes it, and constructs whatever it asks for in turn. scijava-context-test is a sibling module because that is the only honest way to test what matters: a module must really declare `provides`, and its implementation package must really be unexported, or the test proves nothing. It verifies that a service in a package that is neither exported nor opened is still discovered and constructed, and asserts that it is running on the module path rather than skipping quietly should the build ever stop being modular. Two bugs the tests caught, both ordering: - Context.of initialized each service as it registered, so a service depending on one declared after it failed. Registration and initialization are now separate phases. - Services were recorded for disposal on the way in, before their dependencies had resolved, so reversing that list tore dependencies down before their dependents -- exactly backwards. They are now recorded once initialization finishes, so dependencies land earlier and reversing is correct. Note a consequence of acquiring dependencies in initialize rather than in the constructor: two services may depend on each other and it resolves, where constructor injection could not do it at all. The cost, documented on the method, is that a service in such a cycle may briefly observe a peer that has not finished initializing. Contextual, AbstractContextual and NullContextException are not ported: a service receives its context in initialize, so it need not carry one. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gJpzGd3ksbME4CN6Tu2ni
1 parent f65c690 commit caa9640

15 files changed

Lines changed: 1402 additions & 2 deletions

File tree

‎docs/migration-plan.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,30 @@ No application context required by anything in this phase.
504504
lifecycle and dependency injection; plugin metadata from `scijava-index`;
505505
instantiation via `Discoverer`; ordering via `scijava-priority`. Plus the
506506
core services: object registry, prefs, thread, app/status.
507+
- **The container core is done**: `Service`, `Context`, lazy creation,
508+
priority selection, lifecycle, disposal, and one `EventBus` per context.
509+
`scijava-context-test` is a sibling module — the only way to test the
510+
claims that matter, since a module must really declare `provides`, and its
511+
implementation package must really be unexported, for the test to mean
512+
anything. It verifies that a service in a package that is neither exported
513+
nor opened is still discovered and constructed.
514+
- **Services are discovered as `Service`, never as their own interface.**
515+
`ServiceLoader` resolves `uses` against its *caller's* module, so a
516+
container cannot look up an arbitrary third-party interface on that
517+
module's behalf. Declaring one base type that this module `uses` is what
518+
makes discovery work under JPMS at all; the container then sorts
519+
implementations by the interfaces they implement. `Provider.type()` keeps
520+
that cheap, since implementations of other types are never constructed.
521+
- **Two services may depend on each other**, because dependencies are
522+
acquired in `initialize` rather than in the constructor — a cycle that
523+
constructor injection could not resolve at all. The cost is that a service
524+
in a cycle may briefly see a peer that has not finished initializing.
525+
- **Disposal reverses the order in which services finished initializing**,
526+
not the order they were requested, so a service is always torn down before
527+
the ones it depends on.
528+
- Still to do here: the `@Plugin` tier via `scijava-index`, declarative
529+
field injection through a qualified `opens`, `@EventHandler` scanning, the
530+
one-way legacy bridge, and the core services themselves.
507531
- **Dependencies are not constructor arguments.** Constructor injection
508532
would publish every dependency in a signature, so changing an internal
509533
dependency would be an API change and a binary compatibility break unless

‎docs/migration.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ A **dropped** row is a successful outcome, not a gap.
8282
| `Cancelable` | **not ported** — cancellation is thread interruption, and the client API is `Future.cancel(true)`. See the cancellation section of [migration-plan.md](migration-plan.md) | dropped |
8383
| `ItemIO` | `org.scijava.struct.ItemIO` | ported |
8484
| `ItemVisibility` | input harvesting (Phase 4) | open |
85-
| `Context`, `Contextual`, `AbstractContextual`, `NullContextException`, `NoSuchServiceException` | `org.scijava.context` (Phase 2) | planned |
85+
| `Context`, `NoSuchServiceException` | `org.scijava.context.Context` and `org.scijava.context.NoSuchServiceException`. Services are created lazily rather than all at once, and a context is `AutoCloseable` | ported |
86+
| `Contextual`, `AbstractContextual`, `NullContextException` | dropped — a service receives its context in `Service.initialize(Context)`, so it need not carry one, and there is no null-context state to report | dropped |
8687
| `Gateway`, `AbstractGateway`, `SciJava` | Phase 2, if retained at all | open |
8788
| `Instantiable`, `InstantiableException` | subsumed by `Discovery` | planned |
8889
| `BasicDetails`, `AbstractBasicDetails`, `UIDetails`, `AbstractUIDetails`, `MenuEntry`, `MenuPath` | plugin/module metadata (Phases 2–4) | open |
@@ -111,7 +112,7 @@ A **dropped** row is a successful outcome, not a gap.
111112
| `thread` | `org.scijava.concurrent` | ported |
112113
| `task` | `org.scijava.progress` | ported |
113114
| `plugin` | `org.scijava.context` + `org.scijava.index` + `Discoverer` (Phase 2) | planned |
114-
| `service` | `org.scijava.context` — discovered via `ServiceLoader`; `Service` no longer extends `SciJavaPlugin` (Phase 2) | planned |
115+
| `service` | `org.scijava.context`: `Service` (no longer a plugin, and discovered via `ServiceLoader`), with `Context` holding them. `SciJavaService` has no equivalent — a marker is not needed. `ServiceHelper` and `ServiceIndex` are internal to `Context` | ported |
115116
| `object`, `prefs`, `app` | `org.scijava.context` core services (Phase 2) | planned |
116117
| `convert` | `scijava-convert3` (Phase 2) | planned |
117118
| `module`, `module.process`, `command` | execution layer on `scijava-struct` (Phase 3) | planned |

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
<module>scijava-collections</module>
6161
<module>scijava-common3</module>
62+
<module>scijava-context</module>
63+
<module>scijava-context-test</module>
6264
<module>scijava-concurrent</module>
6365
<module>scijava-discovery</module>
6466
<module>scijava-discovery-test</module>

‎scijava-context-test/pom.xml‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.scijava</groupId>
7+
<artifactId>pom-scijava</artifactId>
8+
<version>44.0.0</version>
9+
<relativePath />
10+
</parent>
11+
12+
<artifactId>scijava-context-test</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
15+
<name>SciJava Context Test</name>
16+
<description>Integration tests for the scijava-context library.</description>
17+
<url>https://github.com/scijava/scijava</url>
18+
<inceptionYear>2021</inceptionYear>
19+
<organization>
20+
<name>SciJava</name>
21+
<url>https://scijava.org/</url>
22+
</organization>
23+
<licenses>
24+
<license>
25+
<name>Simplified BSD License</name>
26+
<distribution>repo</distribution>
27+
</license>
28+
</licenses>
29+
30+
<developers>
31+
<developer>
32+
<id>ctrueden</id>
33+
<name>Curtis Rueden</name>
34+
<roles>
35+
<role>founder</role>
36+
<role>lead</role>
37+
<role>reviewer</role>
38+
<role>support</role>
39+
<role>maintainer</role>
40+
</roles>
41+
</developer>
42+
<developer>
43+
<id>gselzer</id>
44+
<name>Gabriel Selzer</name>
45+
<roles>
46+
<role>founder</role>
47+
<role>debugger</role>
48+
<role>reviewer</role>
49+
<role>support</role>
50+
</roles>
51+
</developer>
52+
</developers>
53+
<contributors>
54+
<contributor>
55+
<name>None</name>
56+
</contributor>
57+
</contributors>
58+
59+
<mailingLists>
60+
<mailingList>
61+
<name>Image.sc Forum</name>
62+
<archive>https://forum.image.sc/tag/scijava</archive>
63+
</mailingList>
64+
</mailingLists>
65+
66+
<scm>
67+
<connection>scm:git:https://github.com/scijava/scijava</connection>
68+
<developerConnection>scm:git:git@github.com:scijava/scijava</developerConnection>
69+
<tag>HEAD</tag>
70+
<url>https://github.com/scijava/scijava</url>
71+
</scm>
72+
<issueManagement>
73+
<system>GitHub Issues</system>
74+
<url>https://github.com/scijava/scijava/issues</url>
75+
</issueManagement>
76+
<ciManagement>
77+
<system>GitHub Actions</system>
78+
<url>https://github.com/scijava/scijava/actions</url>
79+
</ciManagement>
80+
81+
<properties>
82+
<package-name>org.scijava.context.test</package-name>
83+
84+
<license.licenseName>bsd_2</license.licenseName>
85+
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
86+
87+
<!-- NB: Do not install/deploy this non-library component. -->
88+
<maven.deploy.skip>true</maven.deploy.skip>
89+
<maven.install.skip>true</maven.install.skip>
90+
91+
<!-- Note: These versions must match across all component POMs! -->
92+
<scijava-context.version>1.0.0-SNAPSHOT</scijava-context.version>
93+
</properties>
94+
95+
<dependencies>
96+
<dependency>
97+
<groupId>org.scijava</groupId>
98+
<artifactId>scijava-context</artifactId>
99+
<version>${scijava-context.version}</version>
100+
</dependency>
101+
<!-- SciJava dependencies -->
102+
103+
<!-- Test scope dependencies -->
104+
<dependency>
105+
<groupId>org.junit.jupiter</groupId>
106+
<artifactId>junit-jupiter-api</artifactId>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
111+
<build>
112+
<plugins>
113+
<plugin>
114+
<artifactId>maven-enforcer-plugin</artifactId>
115+
<executions>
116+
<execution>
117+
<id>enforce-package-rules</id>
118+
<goals>
119+
<goal>enforce</goal>
120+
</goals>
121+
<phase>test</phase>
122+
<configuration>
123+
<rules>
124+
<NoPackageCyclesRule implementation="org.scijava.maven.plugin.enforcer.NoPackageCyclesRule" />
125+
<NoSubpackageDependenceRule implementation="org.scijava.maven.plugin.enforcer.NoSubpackageDependenceRule" />
126+
</rules>
127+
</configuration>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
<plugin>
132+
<artifactId>maven-javadoc-plugin</artifactId>
133+
<configuration>
134+
<links>
135+
<link>https://javadoc.scijava.org/Java11/</link>
136+
</links>
137+
</configuration>
138+
</plugin>
139+
</plugins>
140+
</build>
141+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-
2+
* #%L
3+
* Integration tests for the scijava-context library.
4+
* %%
5+
* Copyright (C) 2021 - 2025 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+
module org.scijava.context.test {
31+
32+
// NB: the implementation package is deliberately NOT exported.
33+
exports org.scijava.context.test;
34+
35+
requires org.scijava.context;
36+
37+
provides org.scijava.context.Service with
38+
org.scijava.context.test.impl.HiddenGreeter;
39+
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* #%L
3+
* Integration tests for the scijava-context library.
4+
* %%
5+
* Copyright (C) 2021 - 2025 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.context.test;
31+
32+
import org.scijava.context.Service;
33+
34+
/** A service interface, exported for callers to ask for. */
35+
public interface GreeterService extends Service {
36+
37+
String greet();
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* #%L
3+
* Integration tests for the scijava-context library.
4+
* %%
5+
* Copyright (C) 2021 - 2025 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.context.test.impl;
31+
32+
import org.scijava.context.test.GreeterService;
33+
34+
/**
35+
* A service implementation in a package that is neither exported nor opened.
36+
* <p>
37+
* Callers can therefore never import it, cast to it, or call anything on it
38+
* beyond {@link GreeterService} - while the container still discovers and
39+
* constructs it, because {@code provides} grants
40+
* {@link java.util.ServiceLoader} the access it needs.
41+
* </p>
42+
*/
43+
public class HiddenGreeter implements GreeterService {
44+
45+
@Override
46+
public String greet() {
47+
return "hello from a hidden implementation";
48+
}
49+
}

0 commit comments

Comments
 (0)