Class PluginManager

java.lang.Object
de.iip_ecosphere.platform.support.plugins.PluginManager

public class PluginManager extends Object
Manages plugins to separate overlapping classpaths and dependencies of alternatives and optionals. Identifies plugins via PluginSetupDescriptor and loads plugin on demand and via the plugin id. Additionally, considers comma/semicolon separated paths in env/system property "okto.plugins" to be loaded as unpacked plugins. The PluginManager does not load plugins automatically as the first point in time to address the PluginManager may be too early, see PluginSetup. Call loadPlugins(), loadAllFrom(File, PluginSetupDescriptor...) loadAllFrom(File, PluginFilter, PluginSetupDescriptor...) explicitly.
Author:
Holger Eichelberger, SSE
  • Field Details

    • POSTFIX_ID_DEFAULT

      public static final String POSTFIX_ID_DEFAULT
      Postfix of plugin id to indicate a default plugin that is also returned if no plugin id matches.
      See Also:
    • KEY_SETUP_DESCRIPTOR

      public static final String KEY_SETUP_DESCRIPTOR
      Prefix identification key for classpath meta-information on the setup descriptor.
      See Also:
    • KEY_SEQUENCE_NR

      public static final String KEY_SEQUENCE_NR
      Prefix identification key for sequence number meta-information on the setup descriptor.
      See Also:
    • KEY_PLUGIN_IDS

      public static final String KEY_PLUGIN_IDS
      Prefix identification key for classpath meta-information on the optional plugin ids.
      See Also:
    • FILE_PLUGINS_PROPERTY

      public static final String FILE_PLUGINS_PROPERTY
      See Also:
    • plugins

      private static Map<String,Plugin<?>> plugins
    • pluginsByType

      private static Map<Class<?>,List<Plugin<?>>> pluginsByType
    • descriptors

      private static Map<String,PluginDescriptor<?>> descriptors
    • pluginCpFilter

      private static Predicate<File> pluginCpFilter
  • Constructor Details

    • PluginManager

      public PluginManager()
  • Method Details

    • getPlugin

      public static Plugin<?> getPlugin(String id)
      Returns a specific plugin.
      Parameters:
      id - the unique id of the plugin
      Returns:
      the plugin, may be null for none
    • getPlugin

      public static <T> Plugin<T> getPlugin(String id, Class<T> cls)
      Returns a specific plugin.
      Type Parameters:
      T - the type of plugin to return
      Parameters:
      id - the id of the plugin
      cls - the class the plugin shall be of
      Returns:
      the plugin (primary or for secondary ids the first registered one), may be null for none
    • getPlugin

      public static <T> Plugin<T> getPlugin(Class<T> cls)
      Returns a plugin for a specific type, possibly the default plugin.
      Type Parameters:
      T - the type of plugin to return
      Parameters:
      cls - the class the plugin shall be of
      Returns:
      the plugin, preferrably the default plugin, may be null for none
    • getPluginInstance

      public static <T, I extends PluginInstanceDescriptor<T>> T getPluginInstance(Class<T> pluginCls, Class<I> iCls)
      Returns an instance of a plugin, either via this plugin manager or via an optional JSL descriptor.
      Type Parameters:
      T - the type of the plugin
      I - the type of the JSL instance descriptor
      Parameters:
      pluginCls - the plugin class
      iCls - the instance descriptor class, may be null
      Returns:
      the plugin instance, null if none was found/created
    • getPlugin

      public static <T> Plugin<T> getPlugin(Class<T> cls, String id)
      Returns a plugin for a specific type.
      Type Parameters:
      T - the type of plugin to return
      Parameters:
      cls - the class the plugin shall be of
      id - the optional id of the plugin to return, may be null or empty leading to the default/first registered plugin
      Returns:
      the plugin, preferrably the default plugin if id does not match, may be null for none
    • plugins

      public static Iterable<Plugin<?>> plugins()
      Returns all known plugins.
      Returns:
      all known plugins
    • loadPlugins

      public static void loadPlugins()
      Loads new plugins from all known plugin setup descriptors.
    • cleanup

      public static void cleanup()
      Calls Plugin.cleanup() on all known plugins.
    • loadPlugins

      private static void loadPlugins(boolean onlyNew)
      Loads plugins from all known plugin setup descriptors.
      Parameters:
      onlyNew - if true considers only unknown/new plugins, if false consides all plugins and issues warnings
    • registerPlugin

      public static void registerPlugin(PluginSetupDescriptor desc)
      Explicitly registers the given plugin (setup) descriptor. Obtains the class loader of the descriptor and loads the known plugin descriptors.
      Parameters:
      desc - the plugin setup descriptor
      See Also:
    • registerPlugin

      private static void registerPlugin(PluginSetupDescriptor desc, boolean onlyNew)
      Registers the given plugin (setup) descriptor. Obtains the class loader of the descriptor and loads the known plugin descriptors. Mandates that known plugin descriptors are loaded by the class loader(s) of desc.
      Parameters:
      desc - the plugin setup descriptor
      onlyNew - if true considers only unknown/new plugins, if false consides all plugins and issues warnings
      See Also:
    • loadedBy

      private static boolean loadedBy(Object obj, ClassLoader loader)
      Returns whether obj was loaded by loader.
      Parameters:
      obj - the object to inspect
      loader - the class loader to compare
      Returns:
      true if obj was loaded by loader, false else
    • registerPlugin

      public static void registerPlugin(PluginDescriptor<?> desc)
      Registers the given plugin descriptor as a new plugin without installation directory.
      Parameters:
      desc - the descriptor to register
    • registerPlugin

      public static void registerPlugin(PluginDescriptor<?> desc, boolean onlyNew, File installDir)
      Registers the given plugin descriptor. May warn if a plugin id is already registered and ignores then desc.
      Parameters:
      desc - the descriptor to register
      onlyNew - if true considers only unknown/new plugins, if false considers all plugins and issues warnings
      installDir - the installation directory, may be null
    • getPluginLoader

      public static ClassLoader getPluginLoader(String id)
      Returns the class loader of the specified plugin.
      Parameters:
      id - the plugin id
      Returns:
      the class loader or the current cpntext class loader if unknown
    • getPluginLoader

      public static ClassLoader getPluginLoader(String id, ClassLoader dflt)
      Returns the class loader of the specified plugin.
      Parameters:
      id - the plugin id
      Returns:
      the class loader or dflt if unknown
    • loadAllFrom

      public static void loadAllFrom(File folder, PluginSetupDescriptor... local)
      Loads all plugins in folder, either in individual folders per plugin or merged into on jar folder with individual classpath files.
      Parameters:
      folder - the parent folder of the plugins folder or of the classpaths (jars in parent folder)
      local - additional local plugin setup descriptors to be considered before the file-based ones
      See Also:
    • loadAllFrom

      public static void loadAllFrom(File folder, PluginManager.PluginFilter filter, PluginSetupDescriptor... local)
      Loads all plugins in folder, either in individual folders per plugin or merged into on jar folder with individual classpath files.
      Parameters:
      folder - the parent folder of the plugins folder or of the classpaths (jars in parent folder)
      filter - optional filter removing those plugins that shall not be loaded, e.g., as covered by local; may be null for none
      local - additional local plugin setup descriptors to be considered before the file-based ones
      See Also:
    • setPluginClasspathFilter

      public static Predicate<File> setPluginClasspathFilter(Predicate<File> filter)
      Parameters:
      filter - the filter, ignored if null; default filter accepts all files
      Returns:
      the filter before modification
    • extractSuffix

      private static String extractSuffix(String prefix, String line, String dflt)
      Extracts the suffix after removing the prefix.
      Parameters:
      prefix - the prefix to look for, may be null
      line - the line to extract the suffix from
      dflt - the default value if there is no prefix, usually line
      Returns:
      line or the line without the prefix
    • addToLoad

      private static void addToLoad(List<PluginManager.PluginInfo> toLoad, File cpFile, int sequenceNr, Supplier<PluginSetupDescriptor> supplier)
      Creates a PluginManager.PluginInfo and adds it to the set of plugins to load.
      Parameters:
      toLoad - the list of plugins to load
      cpFile - the classpath file defining the plugin
      sequenceNr - the indicative sequence number to load the plugin
      supplier - the supplier to create the setup descriptor
    • addToLoad

      private static void addToLoad(List<PluginManager.PluginInfo> toLoad, File cpFile, int sequenceNr, Supplier<PluginSetupDescriptor> supplier, List<String> pluginIds)
      Creates a PluginManager.PluginInfo and adds it to the set of plugins to load.
      Parameters:
      toLoad - the list of plugins to load
      cpFile - the classpath file defining the plugin
      sequenceNr - the indicative sequence number to load the plugin
      supplier - the supplier to create the setup descriptor
      pluginIds - the dependent plugin ids to be considered, may be null
    • parseSequenceNr

      private static int parseSequenceNr(File cpFile, String text, int dflt)
      Parses the plugin sequence number.
      Parameters:
      cpFile - the plugin being processed (for logging)
      text - the text representing the sequence number, may be null
      dflt - the default sequence number to use in case of parsing problems
      Returns:
      the sequence number
    • loadPluginFrom

      private static void loadPluginFrom(File cpFile, int maxFiles, PluginManager.PluginFilter filter, PluginSetupDescriptor... local)
      Loads the plugin specified by its classpath file and its jar file folder based on metadata in the classpath file.
      Parameters:
      cpFile - the classpath file
      filter - optional filter removing those plugins that shall not be loaded, e.g., as covered by local; may be null for none
      local - additional local plugin setup descriptors to be considered before the file-based ones
    • sortPlugins

      private static List<PluginManager.PluginInfo> sortPlugins(List<PluginManager.PluginInfo> infos)
      Sorts the plugins, basically according to their initial sequence number and then, if given, after the last specified dependent plugin (or if not matching at the end).
      Parameters:
      infos - the plugin informations to sort