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 booleanshutdownInProgressWhether a JVM shutdown is in progress.
-
Constructor Summary
Constructors Constructor Description DefaultJvmLauncher()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAppArgument(File file)Adds the specified pathname as an argument to the application.voidaddAppArgumentLine(String line)Adds the specified comma separated argument line as arguments to the application.voidaddAppArguments(String... values)Adds the specified values as arguments to the application.voidaddClasspathEntries(File... paths)Adds the specified paths to the system classpath of the JVM.voidaddClasspathEntries(String... paths)Adds the specified paths to the system classpath of the JVM.voidaddClasspathEntries(List<String> paths)Adds additional classpath entries.voidaddJvmArgument(File file)Adds the specified pathname as an argument to the JVM.voidaddJvmArgumentLine(String line)Adds the specified comma separated argument line as arguments to the JVM.voidaddJvmArguments(String... values)Adds the specified values as arguments to the JVM.intexecute()Launches a JVM according to the configuration of this launcher and waits for its termination.StringgetClasspath()Gets the currently configured system classpath.StringgetCommandLine()Gets a string representation of the currently configured command line.StringgetEnvironmentVariable(String name)Gets an environment variable, as configured for the JVM.voidkill()Forcibly kill the process that was launched, if supported by the JvmLauncher implementation.voidsetAppendOutput(boolean appendOutput)Controls whether the redirected output should be appended to an existing output file (if any).voidsetEnvironmentVariable(String name, String value)Sets an environment variable for the JVM.voidsetJarFile(File jarFile)Sets the JAR file containing the main class to execute.voidsetJvm(String command)Sets the command to launch the JVM.voidsetMainClass(String mainClass)Sets the qualified name of the Java class to execute.voidsetOutputFile(File outputFile)Sets the file to which the output of the JVM is redirected.voidsetOutputLogger(Logger outputLogger, String category)Sets the logger to which the output of the JVM is redirected.voidsetSpawn(boolean spawn)Sets whether the JVM should be launched in spawn modevoidsetSystemProperty(String name, String value)Sets a system property for the JVM.voidsetTimeout(long millis)Sets the timeout in milliseconds after which the process will be killed if still running.voidsetWorkingDirectory(File workingDirectory)Sets the working directory for the forked JVM.voidstart()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:
setWorkingDirectoryin 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:
setJvmin 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:
addJvmArgumentin 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:
addJvmArgumentsin 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:
addJvmArgumentLinein 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:
addClasspathEntriesin 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:
addClasspathEntriesin interfaceJvmLauncher- Parameters:
paths- The classpath entries, may benull.
-
getClasspath
public String getClasspath()
Gets the currently configured system classpath.- Specified by:
getClasspathin 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:
setSystemPropertyin 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:
setEnvironmentVariablein 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:
getEnvironmentVariablein 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:
setJarFilein 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:
setMainClassin 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:
addAppArgumentin 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:
addAppArgumentsin 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:
addAppArgumentLinein 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:
setOutputFilein 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:
setAppendOutputin interfaceJvmLauncher- Parameters:
appendOutput-trueto append the output,falseto 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:
setOutputLoggerin 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:
getCommandLinein 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:
killin interfaceJvmLauncher
-
setTimeout
public void setTimeout(long millis)
Sets the timeout in milliseconds after which the process will be killed if still running.- Specified by:
setTimeoutin 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:
setSpawnin interfaceJvmLauncher- Parameters:
spawn-trueto launch JVM in spawn,falseto launch normal JVM.
-
start
public void start() throws JvmLauncherExceptionLaunches 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:
startin interfaceJvmLauncher- Throws:
JvmLauncherException- If the JVM could not be launched.
-
execute
public int execute() throws JvmLauncherExceptionLaunches a JVM according to the configuration of this launcher and waits for its termination.- Specified by:
executein 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.
-
-