Libraries for plugins
Two plugins that do part of the same work should not copy each other. A library is the code they share, in marginalia/libs/<name>: a plain Maven jar (not a bundle)
that each plugin embeds in its own bundle.
Why embedded, and what that means
Plugins are OSGi bundles and do not see each other's classes (Class loading): a bundle that imported the packages of another
would be wired to one revision of it, and after the other bundle was unloaded and installed again it would keep the old classes (and the JVM remembers a class
reference that failed to resolve). A library is therefore not an extension and is not wired to anything. It is compiled into every plugin that uses it
(Embed-Dependency with inline=true: the classes land in the root of the bundle JAR, next to the plugin's own), so each bundle has its own copy, loaded by its
own class loader. The consequences are the rules of a library:
-
Shared source, not shared runtime.
staticfields exist once per plugin. A library holds no state that two plugins would have to share, and no registry both would have to see; what the plugins have to agree on goes through the application (the core registries, attributes, the services), as for any two plugins (Sharing data between extensions). -
What has to differ is a parameter, not a constant. A library that registers a passage kind takes its id; one that puts a CSS class on a mark takes the class; the texts a plugin shows (an export field's labels) are given by the plugin. Two plugins that both use the library then never collide in the application.
-
Objects do not cross. The
Spanof one plugin is not an instance of theSpanof the other (different class loaders, same name). They exchange data (the JSON in the attributes of a passage), never objects. -
The library may use everything the application has (
provided, as a plugin does, see thepom.xmlof a plugin) and must be woven by AspectJ if it has@Configurableclasses (its POM has the sameaspectj-maven-pluginas a plugin's; the plugin only embeds the woven classes). -
A library is never loaded as an extension. It is kept out of
marginalia/plugins/on purpose: the install script, the pluginDockerfileand the workflows collectplugins/*/target/*.jarinto the extensions folder.
Using a library in a plugin
-
The library is a module of the root
pom.xml(<module>marginalia/libs/translator-common</module>, before the plugins that use it), and a dependency of the plugin, with the version of the project:<dependency> <groupId>com.github.enerccio.marginalia.extensions</groupId> <artifactId>translator-common</artifactId> <version>${project.version}</version> </dependency> -
The bundle plugin embeds it (everything else of the plugin is
provided, so only the library is embedded):<Embed-Dependency>translator-common;inline=true</Embed-Dependency>Keep the negated
Import-Packageentry of every plugin (!com.github.enerccio.marginalia.extensions.<yourplugin>.*): the library's packages are private to the bundle, they are not exported. -
Build the library before the plugin:
mvn install -DskipTestsinmarginalia/libs/translator-commonputs it in the local Maven repository, where a plugin built on its own (mvn packagein its folder) finds it.mvn package/mvn installin the repository root builds in the right order by itself.install-plugins.sh, the rootDockerfile(stageplugins-build) and the workflowsserver.ymlanddesktop.ymlhave a step for it: a library you add needs no change in them when it lives inmarginalia/libs/. -
The CSS and the texts of a library's screens are in the library (
translatorcommon.css,TranslatorCommonLocalization). The plugin'sExtensionHasCssreturns the library's CSS together with its own (CommonStyles.css() + "\n" + MyStyles.css()); the same rules from two plugins are harmless.
Writing a library
Start from the POM of translator-common (a plain jar, the provided dependencies of a plugin, the aspect plugin). Put what is pure in it first (data, text
functions, loops) and test it without the application; add the screens that are the same in both plugins last. Document each package in a package-info.java and
each public class with what it is for, what it needs from the plugin and what it must not hold: the next plugin author reads that, not your plugin.