Class AbstractProcessService<I,​SI,​SO,​O>

    • Field Detail

      • inheritIo

        private static boolean inheritIo
      • inTrans

        private de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,​java.lang.String> inTrans
      • outTrans

        private de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,​O> outTrans
      • callbacks

        private java.util.Map<java.lang.Class<?>,​java.util.List<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>>> callbacks
      • serviceIn

        private java.io.PrintWriter serviceIn
      • proc

        private java.lang.Process proc
      • osProcess

        private oshi.software.os.OSProcess osProcess
    • Constructor Detail

      • AbstractProcessService

        protected AbstractProcessService​(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,​java.lang.String> inTrans,
                                         de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,​O> outTrans,
                                         de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback,
                                         YamlService serviceSpec)
        Creates an instance of the service with the required type translators.
        Parameters:
        inTrans - the input translator
        outTrans - the output translator
        callback - called when data from the service is available (may be null if attachIngestor(Class, DataIngestor) is called before first data processing).
        serviceSpec - the service description
    • Method Detail

      • addCallback

        private void addCallback​(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?> callback)
        Adds a callback.
        Parameters:
        callback - the callback, ignored if null
      • processQuiet

        public void processQuiet​(I data)
        Requests asynchronous processing of a data item. Calls process(Object) and handles potential exceptions.
        Parameters:
        data - the data item to be processed
      • attachIngestor

        public <P> void attachIngestor​(java.lang.Class<P> outCls,
                                       DataIngestor<P> ingestor)
        Attaches an asynchronous result data ingestor as callback.
        Type Parameters:
        P - the output type
        Parameters:
        outCls - the class representing the type
        ingestor - the ingestor instance
      • process

        public abstract void process​(I data)
                              throws java.io.IOException
        Requests to process the given data item.
        Parameters:
        data - the data to process
        Throws:
        java.io.IOException - if processing/transferring to the service fails
      • getServiceSpec

        protected YamlService getServiceSpec()
        Returns the service specification.
        Returns:
        the service specification
      • getProcessSpec

        protected YamlProcess getProcessSpec()
        Returns the process specification within getServiceSpec().
        Returns:
        the process specification, may be null
      • addProcessSpecCmdArg

        protected void addProcessSpecCmdArg​(java.util.List<java.lang.String> args)
        Adds the command line arguments from #getProcessSpec() to args.
        Parameters:
        args - the arguments to be modified as a side effect
      • notifyCallbacks

        protected <P> void notifyCallbacks​(P data)
        Returns the reception callbacks for out.
        Type Parameters:
        P - the output type
        Parameters:
        data - the output data, may be null
      • getInputTranslator

        protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,​java.lang.String> getInputTranslator()
        Returns the input translator.
        Returns:
        the translator
      • getOutputTranslator

        protected de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,​O> getOutputTranslator()
        Returns the output translator.
        Returns:
        the translatir
      • getOsArch

        public static java.lang.String getOsArch​(boolean name32)
        Returns the operating system and architecture in typical form, e.g, win32, win, win64 or linux32, linux, linux64.
        Parameters:
        name32 - shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems
        Returns:
        the operating system and architecture name
      • getExecutableSuffix

        public static java.lang.String getExecutableSuffix()
        Returns the executable file suffix.
        Returns:
        the suffix, may be empty
      • getExecutablePrefix

        public static java.lang.String getExecutablePrefix​(java.lang.String program,
                                                           java.lang.String version)
        Constructs an executable name without suffix, even on Windows, not naming 32 bit.
        Parameters:
        program - the program name
        version - the version of the program
        Returns:
        the executable prefix
        See Also:
        getExecutablePrefix(String, String, boolean)
      • getExecutablePrefix

        public static java.lang.String getExecutablePrefix​(java.lang.String program,
                                                           java.lang.String version,
                                                           boolean name32)
        Constructs an executable name without suffix, even on Windows.
        Parameters:
        program - the program name
        version - the version of the program
        name32 - shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems
        Returns:
        the executable prefix
        See Also:
        getOsArch(boolean)
      • getExecutableName

        public static java.lang.String getExecutableName​(java.lang.String program,
                                                         java.lang.String version,
                                                         boolean name32)
        Constructs an executable name.
        Parameters:
        program - the program name
        version - the version of the program
        name32 - shall the method add 32 in case of a 32 bit operating system (explicit) or be quite (implicit) and add only 64 in case of 64 bit systems
        Returns:
        the executable name
        See Also:
        getExecutablePrefix(String, String, boolean), getExecutableSuffix()
      • getExecutableName

        public static java.lang.String getExecutableName​(java.lang.String program,
                                                         java.lang.String version)
        Constructs an executable name not naming 32 bit.
        Parameters:
        program - the program name
        version - the version of the program
        Returns:
        the executable name
        See Also:
        getExecutableName(String, String, boolean)
      • createProcess

        public static java.lang.Process createProcess​(java.io.File exe,
                                                      boolean byName,
                                                      java.io.File dir,
                                                      java.util.List<java.lang.String> args)
                                               throws java.io.IOException
        Creates and starts a command line process.
        Parameters:
        exe - the executable to run
        byName - add the exe by name or (if false) by absolute name
        dir - the home dir where to execute the process within
        args - the process arguments (may be null for none)
        Returns:
        the created process instance
        Throws:
        java.io.IOException - if process creation fails
        See Also:
        createProcess(File, boolean, File, List, Consumer)
      • createProcess

        public static java.lang.Process createProcess​(java.io.File exe,
                                                      boolean byName,
                                                      java.io.File dir,
                                                      java.util.List<java.lang.String> args,
                                                      java.util.function.Consumer<java.lang.ProcessBuilder> customizer)
                                               throws java.io.IOException
        Creates and starts a command line process.
        Parameters:
        exe - the executable to run
        byName - add the exe by name or (if false) by absolute name
        dir - the home dir where to execute the process within
        args - the process arguments (may be null for none)
        customizer - allows for further customization of the process builder created in this method, may be null for none
        Returns:
        the created process instance
        Throws:
        java.io.IOException - if process creation fails
        See Also:
        setInheritIo(boolean)
      • setInheritIo

        public static boolean setInheritIo​(boolean inherit)
        Sets the default value allow or preveting the inheritance of the IO setup for a process being created in createProcess(File, boolean, File, List, Consumer). Typically switched off for conflicts with CI/surefire.
        Parameters:
        inherit - allow/disable inheriting process IO setup
        Returns:
        the value of the flag before the call
      • redirectIO

        public static AbstractProcessService.RunnableWithStop redirectIO​(java.io.InputStream in,
                                                                         java.io.PrintStream dest)
        Redirects an input stream to another stream (in parallel).
        Parameters:
        in - the input stream of the spawned process (e.g., input/error)
        dest - the destination stream within this class
        Returns:
        the runnable performing the redirection
      • waitAndDestroy

        public static void waitAndDestroy​(java.lang.Process proc)
        Waits and destroys a process with a default sleep time of 200 ms.
        Parameters:
        proc - the process to be destroyed, may be null but nothing happens then
        See Also:
        waitAndDestroy(Process, int)
      • waitAndDestroy

        public static void waitAndDestroy​(java.lang.Process proc,
                                          int sleepTime)
        Waits and destroys a process.
        Parameters:
        proc - the process to be destroyed, may be null but nothing happens then
        sleepTime - the waiting portion until Process.isAlive() is queried again
      • stop

        protected ServiceState stop()
        Preliminary: Stops the service and the background process.
        Overrides:
        stop in class AbstractRunnablesService
        Returns:
        the state to transition to, may be null for none
      • getWaitTimeBeforeDestroy

        protected int getWaitTimeBeforeDestroy()
        Returns an optional time to wait before destroying the process.
        Returns:
        the wait time in ms, may be 0 for none, default 300 ms
      • handleOutputStream

        protected void handleOutputStream​(java.io.OutputStream out)
        Handles the output stream upon process creation.
        Parameters:
        out - the process output stream
      • handleInputStream

        protected abstract void handleInputStream​(java.io.InputStream in)
        Handles the input stream upon process creation.
        Parameters:
        in - the process input stream
      • handleErrorStream

        protected void handleErrorStream​(java.io.InputStream err)
        Handles the error stream upon process creation.
        Parameters:
        err - the process error stream
      • createAndConfigureProcess

        protected java.lang.Process createAndConfigureProcess​(java.io.File exe,
                                                              boolean byName,
                                                              java.io.File dir,
                                                              java.util.List<java.lang.String> args)
                                                       throws java.util.concurrent.ExecutionException
        Creates, configures and starts a command line process.
        Parameters:
        exe - the executable to run
        byName - add the exe by name or (if false) by absolute name
        dir - the home dir where to execute the process within
        args - the process arguments (may be null for none)
        Returns:
        the created process instance
        Throws:
        java.util.concurrent.ExecutionException - if process creation fails
        See Also:
        handleInputStream(InputStream), handleErrorStream(InputStream), handleOutputStream(OutputStream)
      • attachProcessInformation

        private void attachProcessInformation()
        Attaches process information if proc is available. Does not attach process information twice.
      • getPid

        public long getPid()
        Returns the process id of the process implementing the service (if started).
        Returns:
        the id, may be negative if there is no process or the id cannot be obtained
      • getProcessId

        public static long getProcessId​(java.lang.Process proc)
        Returns the process id of a process just started.
        Parameters:
        proc - the process
        Returns:
        the id, may be negative if proc is null or the id cannot be obtained
      • getServiceIn

        protected java.io.PrintWriter getServiceIn()
        Returns the print writer wrapping the process service input stream created by handleInputStream(InputStream).
        Returns:
        the stream
      • selectNotNull

        protected static <T> T selectNotNull​(T value,
                                             T dflt)
        Selects between value and dflt, if value is null use dflt, else the value of value.
        Type Parameters:
        T - the value type
        Parameters:
        value - the primary value
        dflt - the default value to use if value is null
        Returns:
        either value or dflt
      • selectNotNull

        protected static <O,​T> T selectNotNull​(O object,
                                                     java.util.function.Function<O,​T> valueFunc,
                                                     T dflt)
        Selects between a value of object determined by valueFunc and dflt, if object is null or the result of valueFunc is null use dflt, else the value of valueFunc.
        Type Parameters:
        O - the object type
        T - the value type
        Parameters:
        object - the object to take the value from
        valueFunc - the function to apply on object
        dflt - the default value to use if object or the result of valueFunc is null
        Returns:
        either the result of valueFunc or dflt