Class DefaultJvmLauncher
- java.lang.Object
-
- org.codehaus.cargo.container.spi.jvm.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 Summary
Fields Modifier and Type Field Description static boolean
shutdownInProgress
Whether a JVM shutdown is in progress.
-
Constructor Summary
Constructors Constructor Description DefaultJvmLauncher()
Creates a new launcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAppArgument(File file)
Adds the specified pathname as an argument to the application.void
addAppArgumentLine(String line)
Adds the specified comma separated argument line as arguments to the application.void
addAppArguments(String... values)
Adds the specified values as arguments to the application.void
addClasspathEntries(File... paths)
Adds the specified paths to the system classpath of the JVM.void
addClasspathEntries(String... paths)
Adds the specified paths to the system classpath of the JVM.void
addClasspathEntries(List<String> paths)
Adds additional classpath entries.void
addJvmArgument(File file)
Adds the specified pathname as an argument to the JVM.void
addJvmArgumentLine(String line)
Adds the specified comma separated argument line as arguments to the JVM.void
addJvmArguments(String... values)
Adds the specified values as arguments to the JVM.int
execute()
Launches a JVM according to the configuration of this launcher and waits for its termination.String
getClasspath()
Gets the currently configured system classpath.String
getCommandLine()
Gets a string representation of the currently configured command line.String
getEnvironmentVariable(String name)
Gets an environment variable, as configured for the JVM.void
kill()
Forcibly kill the process that was launched, if supported by the JvmLauncher implementation.void
setAppendOutput(boolean appendOutput)
Controls whether the redirected output should be appended to an existing output file (if any).void
setEnvironmentVariable(String name, String value)
Sets an environment variable for the JVM.void
setJarFile(File jarFile)
Sets the JAR file containing the main class to execute.void
setJvm(String command)
Sets the command to launch the JVM.void
setMainClass(String mainClass)
Sets the qualified name of the Java class to execute.void
setOutputFile(File outputFile)
Sets the file to which the output of the JVM is redirected.void
setOutputLogger(Logger outputLogger, String category)
Sets the logger to which the output of the JVM is redirected.void
setSpawn(boolean spawn)
Sets whether the JVM should be launched in spawn modevoid
setSystemProperty(String name, String value)
Sets a system property for the JVM.void
setTimeout(long millis)
Sets the timeout in milliseconds after which the process will be killed if still running.void
setWorkingDirectory(File workingDirectory)
Sets the working directory for the forked JVM.void
start()
Launches a JVM according to the configuration of this launcher.static String[]
translateCommandline(String toProcess)
Turn a string command line to an array of arguments.
-
-
-
Method Detail
-
setWorkingDirectory
public void setWorkingDirectory(File workingDirectory)
Sets the working directory for the forked JVM.- Specified by:
setWorkingDirectory
in interfaceJvmLauncher
- Parameters:
workingDirectory
- The working directory for the forked JVM, may benull
.
-
setJvm
public void setJvm(String command)
Sets the command to launch the JVM.- Specified by:
setJvm
in interfaceJvmLauncher
- Parameters:
command
- The command to lauch the JVM, may benull
.
-
addJvmArgument
public void addJvmArgument(File file)
Adds the specified pathname as an argument to the JVM.- Specified by:
addJvmArgument
in interfaceJvmLauncher
- Parameters:
file
- The pathname to add, may benull
.
-
addJvmArguments
public void addJvmArguments(String... values)
Adds the specified values as arguments to the JVM.- Specified by:
addJvmArguments
in interfaceJvmLauncher
- Parameters:
values
- The values to add, may benull
.
-
addJvmArgumentLine
public void addJvmArgumentLine(String line)
Adds the specified comma separated argument line as arguments to the JVM.- Specified by:
addJvmArgumentLine
in interfaceJvmLauncher
- Parameters:
line
- The arguments to add, may be benull
.
-
addClasspathEntries
public void addClasspathEntries(String... paths)
Adds the specified paths to the system classpath of the JVM.- Specified by:
addClasspathEntries
in interfaceJvmLauncher
- Parameters:
paths
- The classpath entries, may benull
.
-
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 interfaceJvmLauncher
- Parameters:
paths
- The classpath entries, may benull
.
-
getClasspath
public String getClasspath()
Gets the currently configured system classpath.- Specified by:
getClasspath
in interfaceJvmLauncher
- 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 interfaceJvmLauncher
- Parameters:
name
- The property name, may benull
.value
- The property value, may benull
.
-
setEnvironmentVariable
public void setEnvironmentVariable(String name, String value)
Sets an environment variable for the JVM.- Specified by:
setEnvironmentVariable
in interfaceJvmLauncher
- Parameters:
name
- The variable name, may benull
.value
- The property value, may benull
.
-
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 usingSystem.getenv(java.lang.String)
.- Specified by:
getEnvironmentVariable
in interfaceJvmLauncher
- Parameters:
name
- The variable name, may benull
.- Returns:
- Either the previously set value, the system value or null.
-
setJarFile
public void setJarFile(File jarFile)
Sets the JAR file containing the main class to execute. Only one ofJvmLauncher.setJarFile(File)
andJvmLauncher.setMainClass(String)
may be invoked.- Specified by:
setJarFile
in interfaceJvmLauncher
- Parameters:
jarFile
- The JAR file to execute, may benull
.
-
setMainClass
public void setMainClass(String mainClass)
Sets the qualified name of the Java class to execute. Only one ofJvmLauncher.setJarFile(File)
andJvmLauncher.setMainClass(String)
may be invoked.- Specified by:
setMainClass
in interfaceJvmLauncher
- Parameters:
mainClass
- The qualified name of the Java class to execute, may benull
.
-
addAppArgument
public void addAppArgument(File file)
Adds the specified pathname as an argument to the application.- Specified by:
addAppArgument
in interfaceJvmLauncher
- Parameters:
file
- The pathname to add, may benull
.
-
addAppArguments
public void addAppArguments(String... values)
Adds the specified values as arguments to the application.- Specified by:
addAppArguments
in interfaceJvmLauncher
- Parameters:
values
- The values to add, may benull
.
-
addAppArgumentLine
public void addAppArgumentLine(String line)
Adds the specified comma separated argument line as arguments to the application.- Specified by:
addAppArgumentLine
in interfaceJvmLauncher
- Parameters:
line
- The arguments to add, may benull
.
-
setOutputFile
public void setOutputFile(File outputFile)
Sets the file to which the output of the JVM is redirected.. This takes precendence oversetOutputLogger(Logger, String)
.- Specified by:
setOutputFile
in interfaceJvmLauncher
- Parameters:
outputFile
- The file to which the output of the JVM is redirected, may benull
.
-
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 interfaceJvmLauncher
- 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.. IfsetOutputFile(File)
is set, that will take precedence.- Specified by:
setOutputLogger
in interfaceJvmLauncher
- Parameters:
outputLogger
- The logger to which the output of the JVM is redirected, may benull
.category
- the log category to use when logging the JVM's outputs, should not benull
.
-
getCommandLine
public String getCommandLine()
Gets a string representation of the currently configured command line.- Specified by:
getCommandLine
in interfaceJvmLauncher
- 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 interfaceJvmLauncher
-
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 interfaceJvmLauncher
- 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 interfaceJvmLauncher
- 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 interfaceJvmLauncher
- 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 interfaceJvmLauncher
- 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.
-
-