java.lang.Object
de.iip_ecosphere.platform.services.environment.Starter

public class Starter extends Object
Service environment starter reading command server information from the command line.
Author:
Holger Eichelberger, SSE
  • Field Details

    • PARAM_IIP_PROTOCOL

      public static final String PARAM_IIP_PROTOCOL
      See Also:
    • PARAM_IIP_PORT

      public static final String PARAM_IIP_PORT
      See Also:
    • PARAM_IIP_APP_ID

      public static final String PARAM_IIP_APP_ID
      See Also:
    • PARAM_IIP_APP_PLUGINS

      public static final String PARAM_IIP_APP_PLUGINS
      See Also:
    • PARAM_IIP_APP_PLUGINS_NO_PLUGINS

      public static final String PARAM_IIP_APP_PLUGINS_NO_PLUGINS
      See Also:
    • PARAM_IIP_TRANSPORT_GLOBAL

      public static final String PARAM_IIP_TRANSPORT_GLOBAL
      See Also:
    • PARAM_IIP_START_SERVER

      public static final String PARAM_IIP_START_SERVER
      See Also:
    • PARAM_IIP_START_SERVER_ONLY

      public static final String PARAM_IIP_START_SERVER_ONLY
      See Also:
    • IIP_APP_PREFIX

      public static final String IIP_APP_PREFIX
      See Also:
    • IIP_APP_NOAAS

      public static final String IIP_APP_NOAAS
      See Also:
    • PARAM_IIP_TEST_TRANSPORT_PORT

      public static final String PARAM_IIP_TEST_TRANSPORT_PORT
      See Also:
    • PARAM_IIP_TEST_AAS_PORT

      public static final String PARAM_IIP_TEST_AAS_PORT
      See Also:
    • PARAM_IIP_TEST_SM_PORT

      public static final String PARAM_IIP_TEST_SM_PORT
      See Also:
    • PARAM_IIP_TEST_AASREG_PORT

      public static final String PARAM_IIP_TEST_AASREG_PORT
      See Also:
    • PARAM_IIP_TEST_SMREG_PORT

      public static final String PARAM_IIP_TEST_SMREG_PORT
      See Also:
    • PARAM_IIP_TEST_SERVICE_AUTOSTART

      public static final String PARAM_IIP_TEST_SERVICE_AUTOSTART
      See Also:
    • PARAM_IIP_ENFORCE_START_SEQUENCE

      public static final String PARAM_IIP_ENFORCE_START_SEQUENCE
      See Also:
    • ARG_AAS_NOTIFICATION

      public static final String ARG_AAS_NOTIFICATION
      See Also:
    • PROPERTY_JAVA8

      public static final String PROPERTY_JAVA8
      See Also:
    • IIP_TEST

      public static final String IIP_TEST
      See Also:
    • IIP_TEST_PREFIX

      public static final String IIP_TEST_PREFIX
      See Also:
    • IIP_TEST_PLUGIN

      public static final String IIP_TEST_PLUGIN
      See Also:
    • builder

      private static de.iip_ecosphere.platform.support.aas.ProtocolServerBuilder builder
    • cmdServer

      private static de.iip_ecosphere.platform.support.Server cmdServer
    • appServer

      private static de.iip_ecosphere.platform.support.Server appServer
    • servicePorts

      private static Map<String,Integer> servicePorts
    • mappedServices

      private static Map<String,Service> mappedServices
    • serviceAutostart

      private static boolean serviceAutostart
    • enforceStartSequence

      private static boolean enforceStartSequence
    • onServiceAutostartAttachShutdownHook

      private static boolean onServiceAutostartAttachShutdownHook
    • transportPort

      private static int transportPort
    • transportHost

      private static String transportHost
    • transportGlobal

      private static boolean transportGlobal
    • setup

      private static EnvironmentSetup setup
    • appId

      private static String appId
    • plugins

      private static Map<String,Starter.Plugin> plugins
    • oktoPluginsLoaded

      private static boolean oktoPluginsLoaded
    • cmdServerConfigurer

      private static Consumer<de.iip_ecosphere.platform.support.aas.ProtocolServerBuilder> cmdServerConfigurer
    • afterPluginInitializer

      private static Runnable afterPluginInitializer
    • maxServiceStartWaitingTime

      private static int maxServiceStartWaitingTime
    • DFLT_LOCAL_TRANSPORT_SETUP_SUPPLIER

      protected static final Function<EnvironmentSetup,de.iip_ecosphere.platform.transport.connectors.TransportSetup> DFLT_LOCAL_TRANSPORT_SETUP_SUPPLIER
      Default supplier for the local transport setup. This basic implementation is a bit heuristic as it assumes the same authentication/port as the global setup, which may not work in certain container settings.
    • localTransportSetupSupplier

      private static Function<EnvironmentSetup,de.iip_ecosphere.platform.transport.connectors.TransportSetup> localTransportSetupSupplier
      Defines the supplier for the local transport setup. Called only in getSetup() if a need for a separation of global/local transport was detected. Specific service execution implementations may override this using setLocalTransportSetupSupplier(Function). Default is DFLT_LOCAL_TRANSPORT_SETUP_SUPPLIER.
  • Constructor Details

    • Starter

      public Starter()
  • Method Details

    • registerPlugin

      protected static void registerPlugin(String name, Starter.Plugin plugin)
      Registers a functional plugin.
      Parameters:
      name - the name (turned to lower case)
      plugin - the plugin instance
    • registerDefaultPlugins

      protected static void registerDefaultPlugins(Starter.Plugin dflt)
      Registers the default plugins.
      Parameters:
      dflt - the default plugin to be used if no command line argument is given for "iip.test.plugin".
    • inTest

      public static boolean inTest()
      Returns whether we are in test mode, i.e., system or env property "iip.test" is true.
      Returns:
      true for test mode, false else
    • addAppEnvironment

      public static void addAppEnvironment(List<String> args)
      Adds all environment properties starting with IIP_APP_PREFIX or IIP_TEST_PREFIX to the command line of the service to be started. The plugin parent folder is taken from the system property PARAM_IIP_APP_PLUGINS with unset default value "".
      Parameters:
      args - the arguments to add the application environment settings
      See Also:
    • addAppEnvironment

      public static void addAppEnvironment(List<String> args, File pluginParent)
      Adds all environment properties starting with IIP_APP_PREFIX or IIP_TEST_PREFIX to the command line of the service to be started.
      Parameters:
      args - the arguments to add the application environment settings
      pluginParent - the folder containing the "jars" and the "plugins" folder if the do exist
    • setAasNotificationMode

      public static de.iip_ecosphere.platform.support.iip_aas.ActiveAasBase.NotificationMode setAasNotificationMode(String[] args, de.iip_ecosphere.platform.support.iip_aas.ActiveAasBase.NotificationMode dflt)
      Retrieves the AAS notification mode from cmd line argument "iip.test.aas.notification" and sets this mode for AAS interactions. [testing]
      Parameters:
      args - the command line arguments
      dflt - the default value if no argument is present, may be null to keep the actual mode if not set explicitly
      Returns:
      the actual AAS notification mode, may be null for none
    • considerInstalledDependencies

      public static void considerInstalledDependencies()
      Considers installed dependencies properties, -D"iip.test.java8".
    • transferArgsToEnvironment

      public static void transferArgsToEnvironment(String[] args)
      Transfers IIP_APP_PREFIX as system property or usual command line argument to the system properties if not already set.
      Parameters:
      args - the arguments to be analyzed
    • startServer

      protected static boolean startServer(String[] args)
      Starts a server instance passed in by PARAM_IIP_START_SERVER.
      Parameters:
      args - the command line arguments
      Returns:
      true for handled, false for not handled
    • getAppId

      public static String getAppId()
      Returns the application/application instance id passed in by PARAM_IIP_APP_ID.
      Returns:
      the app/application instance id (separated by ServiceBase.APPLICATION_SEPARATOR, may be empty
    • getServiceId

      public static String getServiceId(Service service)
      Returns the id of service taking getAppId() into account.
      Parameters:
      service - the service
      Returns:
      the service including appId if known/specified
    • getServiceId

      public static String getServiceId(String sId)
      Returns the completed service id sId taking getAppId() into account.
      Parameters:
      sId - the service id
      Returns:
      the service including appId if known/specified
    • getServiceCommandNetworkMgrKey

      public static String getServiceCommandNetworkMgrKey(String serviceId)
      Returns the network manager key used by this descriptor to allocate dynamic network ports for service commands.
      Parameters:
      serviceId - the service id
      Returns:
      the key
    • getServiceProcessNetworkMgrKey

      public static String getServiceProcessNetworkMgrKey(String serviceId)
      Returns the network manager key used by this descriptor to allocate dynamic network ports for a non-Java realization process.
      Parameters:
      serviceId - the service id
      Returns:
      the key
    • composeArgument

      public static String composeArgument(String argName, Object value)
      Composes a command line argument for the starter.
      Parameters:
      argName - the argument name
      value - the value
      Returns:
      the composed command line argument
    • getServicePortName

      public static String getServicePortName(String serviceId)
      Returns the argument name carrying the delegation port of serviceId. Arguments of this kind will be collected for getServicePort(String)
      Parameters:
      serviceId - the service id (will be normalized to command line requirements)
      Returns:
      the argument name
    • getServicePort

      public static int getServicePort(String serviceId)
      Returns a service port obtained in parse(String...).
      Parameters:
      serviceId - the service id (will be normalized to command line requirements)
      Returns:
      the port number, negative if invalid or unknown
    • normalizeServiceId

      public static String normalizeServiceId(String serviceId)
      Returns the normalized service id.
      Parameters:
      serviceId - the service id
      Returns:
      the /normalized) service id
    • setServiceAutostart

      public static void setServiceAutostart(boolean autostart)
      Enables service autostart for the next services to be mapped. Disabled by default. Shall be called before #main(String[]). Usually done when needed by platform during service lifecycle. [testing]
      Parameters:
      autostart - true enables autostart, false disables autostart
    • getServiceAutostart

      public static boolean getServiceAutostart()
      Returns whether service autostart is enabled.
      Returns:
      true if autostart is enabled, false if autostart is disabled
    • enforceStartSequence

      public static boolean enforceStartSequence()
      Returns whether the service start sequence shall be enforced.
      Returns:
      true if the sequence shall be enforced, false if else
    • setOnServiceAutostartAttachShutdownHook

      public static void setOnServiceAutostartAttachShutdownHook(boolean hook)
      Enables/disable shutdown hooks on service autostarts for service autostops. Enabled by default. Shall be called before #main(String[]). [testing]
      Parameters:
      hook - true enables creation of shutdown hooks, false disables shutdown hooks on autostart
    • getMappedService

      public static Service getMappedService(String serviceId)
      Returns service mapped by this starter. These are typically all services executed within the same JVM.
      Parameters:
      serviceId - the serviceId, ignored if null
      Returns:
      the service if known, null else
    • startDataForMappedServices

      public static void startDataForMappedServices(Predicate<Integer> enable)
      Calls Service.startData() if either enable is null or enable leads to true for the actual number of mapped services.
      Parameters:
      enable - optional predicate over the number of actually mapped services
    • configurePort

      private static void configurePort(String[] args, String paramName, Consumer<Integer> consumer)
      Configures a port.
      Parameters:
      args - the command line arguments
      paramName - the parameter name to look for
      consumer - the port consumer
    • expectAas

      public static boolean expectAas()
      Returns whether the execution may expect a running AAS.
      Returns:
      true for running AAS, false else
    • parse

      public static void parse(String... args)
      Parses command line arguments. Collects information for getServicePort(String).
      Parameters:
      args - the command line arguments
    • setCmdServerConfigurer

      protected static void setCmdServerConfigurer(Consumer<de.iip_ecosphere.platform.support.aas.ProtocolServerBuilder> configurer)
      Sets a configurer for the AAS asset command server.
      Parameters:
      configurer - the configurer
    • start

      public static void start()
      Starts the server instance(s).
      See Also:
    • getLogger

      private static de.iip_ecosphere.platform.support.logging.Logger getLogger()
      Returns the logger instance.
      Returns:
      the logger instance
    • getProtocolBuilder

      public static de.iip_ecosphere.platform.support.aas.ProtocolServerBuilder getProtocolBuilder()
      Returns the protocol builder for mapping services.
      Returns:
      the protocol builder, null if parse(String[]) was not called before
    • getServiceMapper

      public static ServiceMapper getServiceMapper()
      Returns the service mapper linked to getProtocolBuilder().
      Returns:
      the service mapper, null if parse(String[]) was not called before
    • mapService

      public static void mapService(ServiceMapper mapper, Service service, boolean enableAutostart)
      Maps a service through a given mapper and metrics client. No mapping will take place if either service, mapper or getProtocolBuilder() is null. The specific mapping for the metrics will only take place if metricsClient is not null.
      Parameters:
      mapper - the service mapper instance (may be null, no mapping will happen then)
      service - the service to be mapped (may be null, no mapping will happen then)
      enableAutostart - whether service autostart shall be performed
      See Also:
    • autostartService

      public static void autostartService(Service service)
      Auto-starts the specified service (if serviceAutostart and Service.isTopLevel().
      Parameters:
      service - the service to be started
    • extractProcessArtifacts

      public static File extractProcessArtifacts(String sId, ProcessSpec pSpec, File artFile, File processBaseDir) throws IOException
      Extracts artifacts that are required for a service being realized of external processes.
      Parameters:
      sId - the service id
      pSpec - the process specification
      artFile - the ZIP/JAR service artifact
      processBaseDir - the base directory to be used to create a process home directory within if ProcessSpec.getHomePath() is null
      Returns:
      the folder into which the process has been extracted. May be ProcessSpec.getHomePath() or a temporary directory.
      Throws:
      IOException - if accessing files fails
    • findArtifact

      public static InputStream findArtifact(File artFile, String artPath)
      Finds a file artifact within an application file/artifact.
      Parameters:
      artFile - the artifact file
      artPath - the file artifact path within artFile
      Returns:
      the input stream (may be null for not found)
    • mapService

      public static void mapService(Service service, boolean enableAutostart)
      Maps a service through the default mapper and the default metrics client. [Convenience method for generation]
      Parameters:
      service - the service to be mapped (may be null, no mapping will happen then)
      enableAutostart - whether service autostart shall be performed if , e.g., not for family members
      See Also:
    • mapService

      public static void mapService(Service service)
      Maps a service through the default mapper and the default metrics client. [Convenience method for generation] By default, do autostart.
      Parameters:
      service - the service to be mapped (may be null, no mapping will happen then)
      See Also:
    • shutdown

      public static void shutdown()
      Terminates running server instances.
    • getSetup

      public static EnvironmentSetup getSetup()
      Returns the environment setup.
      Returns:
      the setup
    • enablesLocalTransport

      protected static final boolean enablesLocalTransport(de.iip_ecosphere.platform.transport.connectors.TransportSetup globalSetup)
      Returns whether globalSetup enables local transport.
      Parameters:
      globalSetup - the global setup
      Returns:
      true for enabled, false for local transport is sufficient, e.g., in local testing
    • getApplicationSetupAsStream

      public static InputStream getApplicationSetupAsStream()
      Returns the application setup as stream.
      Returns:
      the application setup as stream
    • setLocalTransportSetupSupplier

      protected static void setLocalTransportSetupSupplier(Function<EnvironmentSetup,de.iip_ecosphere.platform.transport.connectors.TransportSetup> supplier)
      Changes the local transport supplier determining the setup for the local transport.
      Parameters:
      supplier - the new supplier, may be null for none
    • runPlugin

      protected static void runPlugin(String[] args)
      Selects the actual functional plugin via cmd line argument of "iip.test.plugin". If none is given, start the default plugin.
      Parameters:
      args - command line arguments
    • registerAfterPluginInitializer

      public static void registerAfterPluginInitializer(Runnable initializer)
      Sets an initializer to be executed after loading plugins in loadOktoPlugins().
      Parameters:
      initializer - the initializer
    • loadOktoPlugins

      public static void loadOktoPlugins()
      Loads the oktoflow plugins if present. Loads plugins only once. Needs args transferred to environment, i.e., after parse. Runs and clears afterPluginInitializer.
      See Also:
    • main

      public static void main(String[] args)
      Simple default start main program without mapping any services before startup. This can be done on-demand through getProtocolBuilder() and getServiceMapper().
      Parameters:
      args - the command line arguments