Class LayerBootstrap

java.lang.Object
org.tentackle.common.LayerBootstrap

public final class LayerBootstrap extends Object
Launcher building a ModuleLayer graph and handing off to the real application.

Applications never invoke this class themselves — it is what the tentackle-jlink-maven-plugin's generated launcher script runs instead of the real main class whenever <layers> are configured (see the jlinkpackage.md and layers.md documentation), and what an IDE run/debug configuration points at to run an application with isolated layers locally.

main(String[]) reads a LayerGraphSpec from a properties file (see LAYERS_PROPERTY), builds the layer graph via ModuleLayerBuilder, sets the leaf layer's classloader as the thread context classloader — which is what makes every existing, layer-unaware ServiceFactory.getServiceFinder() call transparently resolve services from within the leaf layer — and then reflectively invokes the real application's main(String[]) method.

Relative layer directories in the properties file are resolved against the directory given by LAYERS_BASEDIR_PROPERTY, or against the current working directory if that property is not set — never against the directory of the properties file.

Author:
harald
  • Field Details

    • LAYERS_PROPERTY

      public static final String LAYERS_PROPERTY
      System property naming the layers configuration file.
      Resolved relative to the current working directory if not absolute.
      See Also:
    • DEFAULT_LAYERS_FILE

      public static final String DEFAULT_LAYERS_FILE
      Default location of the layers configuration file, relative to the current working directory.
      See Also:
    • LAYERS_BASEDIR_PROPERTY

      public static final String LAYERS_BASEDIR_PROPERTY
      System property naming the base directory relative layer directories are resolved against.
      Defaults to the current working directory. Necessary whenever the launcher cannot change into the image root first, such as the native launcher of a jpackage installation.
      See Also:
  • Method Details

    • getLayer

      public static ModuleLayer getLayer(String name)
      Gets a layer built by this bootstrap, by name.
      This is how application code reaches a layer other than its own leaf layer — e.g., one pinning a specific version of a dependency — typically to then resolve a service from it via ServiceFactory.getServiceFinder(ModuleLayer, String). See the layers.md documentation for the full pattern, including why a layer must not have two parent layers that define the same module (for example, two versions of the same library).
      Parameters:
      name - the layer name, as configured
      Returns:
      the layer
      Throws:
      TentackleRuntimeException - if no such layer, or called before main(String[]) has run
    • getLayers

      public static Map<String, ModuleLayer> getLayers()
      Gets all layers built by this bootstrap, keyed by name.
      Returns:
      the layers, empty if main(String[]) has not run yet
    • main

      public static void main(String[] args) throws Exception
      Builds the configured module layer graph and invokes the real application's main method.
      Parameters:
      args - the application arguments, passed through unchanged
      Throws:
      Exception - if the layer graph cannot be built or the application's main method fails (the latter wrapped in a TentackleRuntimeException)