Class AbstractProcessService<I,SI,SO,O>

Type Parameters:
I - the input data type
SI - the service input data type
SO - the service output data type
O - the output data type
All Implemented Interfaces:
MonitoringService, ParameterConfigurerProvider, Service, ServiceBase
Direct Known Subclasses:
AbstractRestProcessService, AbstractStringProcessService

public abstract class AbstractProcessService<I,SI,SO,O> extends AbstractRunnablesService implements MonitoringService
Implements an abstract asynchronous process-based service for a single pair of input-output types. A created service/process is stopped on JVM shutdown.
Author:
Holger Eichelberger, SSE
  • Field Details

    • inheritIo

      private static boolean inheritIo
    • inTrans

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

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

      private Map<Class<?>,List<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>>> callbacks
    • serviceSpec

      private YamlService serviceSpec
    • serviceIn

      private PrintWriter serviceIn
    • proc

      private Process proc
    • osProcess

      private de.iip_ecosphere.platform.support.processInfo.ProcessInfoFactory.ProcessInfo osProcess
  • Constructor Details

    • AbstractProcessService

      protected AbstractProcessService(de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I,String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<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 Details

    • 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(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 IOException
      Requests to process the given data item.
      Parameters:
      data - the data to process
      Throws:
      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(List<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
    • getCallbacks

      protected Iterable<de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<?>> getCallbacks(Class<?> cls)
      Returns the callbacks for a given type.
      Parameters:
      cls - the type
      Returns:
      the callbacks, may be null if there are none
    • getInputTranslator

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

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

      public static 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 String getExecutableSuffix()
      Returns the executable file suffix.
      Returns:
      the suffix, may be empty
    • getExecutablePrefix

      public static String getExecutablePrefix(String program, 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

      public static String getExecutablePrefix(String program, 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:
    • getExecutableName

      public static String getExecutableName(String program, 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:
    • getExecutableName

      public static String getExecutableName(String program, 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:
    • createProcess

      public static Process createProcess(File exe, boolean byName, File dir, List<String> args) throws 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:
      IOException - if process creation fails
      See Also:
    • createProcess

      public static Process createProcess(File exe, boolean byName, File dir, List<String> args, Consumer<ProcessBuilder> customizer) throws 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:
      IOException - if process creation fails
      See Also:
    • 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(InputStream in, 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(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

      public static void waitAndDestroy(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(OutputStream out)
      Handles the output stream upon process creation.
      Parameters:
      out - the process output stream
    • handleInputStream

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

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

      protected Process createAndConfigureProcess(File exe, boolean byName, File dir, List<String> args) throws 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:
      ExecutionException - if process creation fails
      See Also:
    • 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(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
    • configure

      protected void configure(ProcessBuilder builder)
      Allows to configure a process builder for this service. Called from createAndConfigureProcess(File, boolean, File, List)
      Parameters:
      builder - the process builder to configure
    • getServiceIn

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

      public void attachMetricsProvider(MetricsProvider provider)
      Attaches the metrics provider.
      Specified by:
      attachMetricsProvider in interface MonitoringService
      Parameters:
      provider - the metrics provider instance
    • 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, 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