Class DefaultJvmLauncher

  • All Implemented Interfaces:
    JvmLauncher

    public class DefaultJvmLauncher
    extends Object
    implements JvmLauncher
    A JVM launcher that launches a new Process, that can be forcibly killed if needed.
    • Field Detail

      • shutdownInProgress

        public static boolean shutdownInProgress
        Whether a JVM shutdown is in progress.
    • Constructor Detail

      • DefaultJvmLauncher

        public DefaultJvmLauncher()
        Creates a new launcher.
    • Method Detail

      • setWorkingDirectory

        public void setWorkingDirectory​(File workingDirectory)
        Sets the working directory for the forked JVM.
        Specified by:
        setWorkingDirectory in interface JvmLauncher
        Parameters:
        workingDirectory - The working directory for the forked JVM, may be null.
      • setJvm

        public void setJvm​(String command)
        Sets the command to launch the JVM.
        Specified by:
        setJvm in interface JvmLauncher
        Parameters:
        command - The command to lauch the JVM, may be null.
      • addJvmArgument

        public void addJvmArgument​(File file)
        Adds the specified pathname as an argument to the JVM.
        Specified by:
        addJvmArgument in interface JvmLauncher
        Parameters:
        file - The pathname to add, may be null.
      • addJvmArguments

        public void addJvmArguments​(String... values)
        Adds the specified values as arguments to the JVM.
        Specified by:
        addJvmArguments in interface JvmLauncher
        Parameters:
        values - The values to add, may be null.
      • addJvmArgumentLine

        public void addJvmArgumentLine​(String line)
        Adds the specified comma separated argument line as arguments to the JVM.
        Specified by:
        addJvmArgumentLine in interface JvmLauncher
        Parameters:
        line - The arguments to add, may be be null.
      • addClasspathEntries

        public void addClasspathEntries​(String... paths)
        Adds the specified paths to the system classpath of the JVM.
        Specified by:
        addClasspathEntries in interface JvmLauncher
        Parameters:
        paths - The classpath entries, may be null.
      • addClasspathEntries

        public void addClasspathEntries​(List<String> paths)
        Adds additional classpath entries.
        Parameters:
        paths - The additional classpath entries.
      • addClasspathEntries

        public void addClasspathEntries​(File... paths)
        Adds the specified paths to the system classpath of the JVM.
        Specified by:
        addClasspathEntries in interface JvmLauncher
        Parameters:
        paths - The classpath entries, may be null.
      • getClasspath

        public String getClasspath()
        Gets the currently configured system classpath.
        Specified by:
        getClasspath in interface JvmLauncher
        Returns:
        The currently configured system classpath, never null.
      • setSystemProperty

        public void setSystemProperty​(String name,
                                      String value)
        Sets a system property for the JVM.
        Specified by:
        setSystemProperty in interface JvmLauncher
        Parameters:
        name - The property name, may be null.
        value - The property value, may be null.
      • setEnvironmentVariable

        public void setEnvironmentVariable​(String name,
                                           String value)
        Sets an environment variable for the JVM.
        Specified by:
        setEnvironmentVariable in interface JvmLauncher
        Parameters:
        name - The variable name, may be null.
        value - The property value, may be null.
      • getEnvironmentVariable

        public String getEnvironmentVariable​(String name)
        Gets an environment variable, as configured for the JVM. In case of manipulation of an existing variable, instead of replacing it (eg: when adding an additional directory to PATH) it is recommended to retrieve the value using this method instead of using System.getenv(java.lang.String).
        Specified by:
        getEnvironmentVariable in interface JvmLauncher
        Parameters:
        name - The variable name, may be null.
        Returns:
        Either the previously set value, the system value or null.
      • addAppArgument

        public void addAppArgument​(File file)
        Adds the specified pathname as an argument to the application.
        Specified by:
        addAppArgument in interface JvmLauncher
        Parameters:
        file - The pathname to add, may be null.
      • addAppArguments

        public void addAppArguments​(String... values)
        Adds the specified values as arguments to the application.
        Specified by:
        addAppArguments in interface JvmLauncher
        Parameters:
        values - The values to add, may be null.
      • addAppArgumentLine

        public void addAppArgumentLine​(String line)
        Adds the specified comma separated argument line as arguments to the application.
        Specified by:
        addAppArgumentLine in interface JvmLauncher
        Parameters:
        line - The arguments to add, may be null.
      • setOutputFile

        public void setOutputFile​(File outputFile)
        Sets the file to which the output of the JVM is redirected.. This takes precendence over setOutputLogger(Logger, String).
        Specified by:
        setOutputFile in interface JvmLauncher
        Parameters:
        outputFile - The file to which the output of the JVM is redirected, may be null.
      • setAppendOutput

        public void setAppendOutput​(boolean appendOutput)
        Controls whether the redirected output should be appended to an existing output file (if any).
        Specified by:
        setAppendOutput in interface JvmLauncher
        Parameters:
        appendOutput - true to append the output, false to overwrite the file.
        See Also:
        JvmLauncher.setOutputFile(File)
      • setOutputLogger

        public void setOutputLogger​(Logger outputLogger,
                                    String category)
        Sets the logger to which the output of the JVM is redirected.. If setOutputFile(File) is set, that will take precedence.
        Specified by:
        setOutputLogger in interface JvmLauncher
        Parameters:
        outputLogger - The logger to which the output of the JVM is redirected, may be null.
        category - the log category to use when logging the JVM's outputs, should not be null.
      • getCommandLine

        public String getCommandLine()
        Gets a string representation of the currently configured command line.
        Specified by:
        getCommandLine in interface JvmLauncher
        Returns:
        The currently configured command line, never null.
      • kill

        public void kill()
        Forcibly kill the process that was launched, if supported by the JvmLauncher implementation.
        Specified by:
        kill in interface JvmLauncher
      • setTimeout

        public void setTimeout​(long millis)
        Sets the timeout in milliseconds after which the process will be killed if still running.
        Specified by:
        setTimeout in interface JvmLauncher
        Parameters:
        millis - The timeout, may be non-positive to disable process watching.
      • setSpawn

        public void setSpawn​(boolean spawn)
        Sets whether the JVM should be launched in spawn mode
        Specified by:
        setSpawn in interface JvmLauncher
        Parameters:
        spawn - true to launch JVM in spawn, false to launch normal JVM.
      • start

        public void start()
                   throws JvmLauncherException
        Launches a JVM according to the configuration of this launcher. The method returns as soon as the JVM got launched and does not wait for its termination.
        Specified by:
        start in interface JvmLauncher
        Throws:
        JvmLauncherException - If the JVM could not be launched.
      • execute

        public int execute()
                    throws JvmLauncherException
        Launches a JVM according to the configuration of this launcher and waits for its termination.
        Specified by:
        execute in interface JvmLauncher
        Returns:
        The exit code of the JVM.
        Throws:
        JvmLauncherException - If the JVM could not be launched.
      • translateCommandline

        public static String[] translateCommandline​(String toProcess)
        Turn a string command line to an array of arguments. The logic takes into account the spaces between arguments, as well as single/double quotes for escaping arguments with spaces in them.
        Parameters:
        toProcess - the command line to process.
        Returns:
        the command line broken into strings. An empty or null toProcess parameter results in a zero sized array.