Package adams.core.management
Class Launcher
- java.lang.Object
-
- adams.core.management.Launcher
-
public class Launcher extends Object
Launches a new JVM process with the specified memory (-memory <amount>), a Java agent (-javaagent <jar-file>) and main class (-main <classname>).
All other command-line arguments are passed on to the new process.
In addition to parameters from the commandline, additional parameters can be defined in theADAMS_OPTSenvironment variable. Note: No checks are performed if the same parameter is defined in this env variable and on the commandline.
TheADAMS_LIBRARY_PATHallows you to supply additional libraries that you would normally supply to the JVM using '-Djava.library.path=...'.
Use-helpto output all available parameters.
When run from commandline, the methodaddShutdownHook()gets called automatically beforeexecute(), which adds a hook thread to the runtime, killing the launched process, e.g., when usingctrl+c.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
ENV_ADAMS_OPTS
-
-
Field Summary
Fields Modifier and Type Field Description static intCODE_RESTARTthe return code for restarting the main class.static intCODE_RESTART_MORE_HEAPthe return code for restarting the main class with 50% more heap.static StringENV_ADAMS_LIBRARY_PATHthe environment variable for the library path.static StringENV_ADAMS_OPTSthe environment variable with additional options.protected String[]m_Argumentsthe arguments for the process.protected List<String>m_ClassPathAugmentationsoptional classpath augmentations.protected booleanm_CollapseClassPathwhether to collapse the classpath.protected LoggingObjectm_ConsoleObjectthe console object that calls the launcher (if any).protected intm_DebugLevelthe debug level.protected List<EnvironmentModifier>m_EnvironmentModifiersoptional environment modifiers.protected List<String>m_EnvVarsoptional environment variables.protected booleanm_IgnoreEnvironmentOptionswhether to ignore the ADAMS environment options.protected Stringm_JavaAgentJarthe java agent jar to use.protected List<String>m_JVMOptionsoptional JVM options.protected Loggerm_Loggerfor logging.protected Stringm_MainClassthe main class to launch.protected Stringm_Memorythe amount of memory to use for the process.protected Classm_OutputPrinterthe output printer to use.protected List<String>m_PriorityJarsoptional priority jars.protected Processm_Processthe process that got launched.protected Runtimem_Runtimethe runtime object in use.protected OutputProcessStreamm_StdErrfor stderr.protected OutputProcessStreamm_StdOutfor stdout.protected booleanm_SuppressErrorDialogwhether to suppress error dialog.
-
Constructor Summary
Constructors Constructor Description Launcher()Initializes the launcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClassPathAugmentations(ClassPathAugmenter augmenter)Adds the augmentations that the classpath augmenter returns.voidaddClassPathAugmentations(String cmdline)Adds the augmentations that the classpath augmenter returns.voidaddEnvironmentModifier(String cmdline)Adds the augmentations that the classpath augmenter returns.voidaddEnvVar(String value)Adds the environmental variable key-value pair (key=value).voidaddJVMOption(String value)Adds the JVM option.voidaddPriorityJar(String value)Adds the priority jar.voidaddShutdownHook()Adds a shutdown hook, to kill the launched process.protected static voidcleanOptions(String[] args)Removes unnecessary double quotes.voidcollapseClassPath(boolean value)Sets whether to collapse the classpath (using '*' below dirs instead of explicit jar names).protected List<String>collapseClassPath(List<String> cpath)protected static Stringconfigure(String[] args, Launcher launcher)Configures the launcher.voiddestroy()Allows killing the process.Stringexecute()Launches the main class.protected StringgetClassPath()Assembles the classpath.OutputProcessStreamgetStdErr()Returns the stderr handler.OutputProcessStreamgetStdOut()Returns the stdout handler.voidignoreEnvironmentOptions()Sets to ignore the ADAMS environment options.protected voidincreaseHeap()Increase the heap by 50%.static voidmain(String[] args)Executes the class from the command-line.StringsetArguments(String[] value)Sets the arguments for the process.voidsetConsoleObject(LoggingObject owner)Sets the owning console object to use for the output printer if that implementsLoggingObjectOwner.StringsetDebugLevel(String value)Sets the debug level (0 = off, >0 on).StringsetJavaAgentJar(String value)Sets the java agent jar of the process.StringsetMainClass(String value)Sets the main class of the process.StringsetMemory(String value)Sets the amount of memory for the process.voidsetOutputPrinter(Class cls)Sets the output printer class to use.voidsuppressErrorDialog()Suppresses the error dialog.
-
-
-
Field Detail
-
CODE_RESTART
public static final int CODE_RESTART
the return code for restarting the main class.- See Also:
- Constant Field Values
-
CODE_RESTART_MORE_HEAP
public static final int CODE_RESTART_MORE_HEAP
the return code for restarting the main class with 50% more heap.- See Also:
- Constant Field Values
-
ENV_ADAMS_OPTS
public static final String ENV_ADAMS_OPTS
the environment variable with additional options.- See Also:
- Constant Field Values
-
ENV_ADAMS_LIBRARY_PATH
public static final String ENV_ADAMS_LIBRARY_PATH
the environment variable for the library path.- See Also:
- Constant Field Values
-
m_Memory
protected String m_Memory
the amount of memory to use for the process.
-
m_MainClass
protected String m_MainClass
the main class to launch.
-
m_JavaAgentJar
protected String m_JavaAgentJar
the java agent jar to use.
-
m_ClassPathAugmentations
protected List<String> m_ClassPathAugmentations
optional classpath augmentations.
-
m_EnvironmentModifiers
protected List<EnvironmentModifier> m_EnvironmentModifiers
optional environment modifiers.
-
m_CollapseClassPath
protected boolean m_CollapseClassPath
whether to collapse the classpath.
-
m_Arguments
protected String[] m_Arguments
the arguments for the process.
-
m_Runtime
protected Runtime m_Runtime
the runtime object in use.
-
m_Process
protected Process m_Process
the process that got launched.
-
m_DebugLevel
protected int m_DebugLevel
the debug level.
-
m_IgnoreEnvironmentOptions
protected boolean m_IgnoreEnvironmentOptions
whether to ignore the ADAMS environment options.
-
m_SuppressErrorDialog
protected boolean m_SuppressErrorDialog
whether to suppress error dialog.
-
m_OutputPrinter
protected Class m_OutputPrinter
the output printer to use.
-
m_StdOut
protected OutputProcessStream m_StdOut
for stdout.
-
m_StdErr
protected OutputProcessStream m_StdErr
for stderr.
-
m_ConsoleObject
protected LoggingObject m_ConsoleObject
the console object that calls the launcher (if any).
-
m_Logger
protected Logger m_Logger
for logging.
-
-
Method Detail
-
setMemory
public String setMemory(String value)
Sets the amount of memory for the process.- Parameters:
value- the amount- Returns:
- null if valid amount, otherwise error message
-
setDebugLevel
public String setDebugLevel(String value)
Sets the debug level (0 = off, >0 on).- Parameters:
value- the level- Returns:
- null if valid level, otherwise error message
-
increaseHeap
protected void increaseHeap()
Increase the heap by 50%.
-
setMainClass
public String setMainClass(String value)
Sets the main class of the process.- Parameters:
value- the class name- Returns:
- null if valid class, otherwise error message
-
setJavaAgentJar
public String setJavaAgentJar(String value)
Sets the java agent jar of the process.- Parameters:
value- the jar- Returns:
- null if valid class, otherwise error message
-
addJVMOption
public void addJVMOption(String value)
Adds the JVM option. Checks for duplicates.- Parameters:
value- the option
-
addClassPathAugmentations
public void addClassPathAugmentations(String cmdline)
Adds the augmentations that the classpath augmenter returns.- Parameters:
cmdline- the classname+options of the augmenter- See Also:
ClassPathAugmenter
-
addEnvironmentModifier
public void addEnvironmentModifier(String cmdline)
Adds the augmentations that the classpath augmenter returns.- Parameters:
cmdline- the classname+options of the augmenter- See Also:
ClassPathAugmenter
-
addPriorityJar
public void addPriorityJar(String value)
Adds the priority jar.- Parameters:
value- the option
-
addEnvVar
public void addEnvVar(String value)
Adds the environmental variable key-value pair (key=value).- Parameters:
value- the option
-
addClassPathAugmentations
public void addClassPathAugmentations(ClassPathAugmenter augmenter)
Adds the augmentations that the classpath augmenter returns.- Parameters:
augmenter- the classname+options of the augmenter- See Also:
ClassPathAugmenter
-
collapseClassPath
public void collapseClassPath(boolean value)
Sets whether to collapse the classpath (using '*' below dirs instead of explicit jar names).- Parameters:
value- true if to collapse
-
setArguments
public String setArguments(String[] value)
Sets the arguments for the process.- Parameters:
value- the arguments
-
addShutdownHook
public void addShutdownHook()
Adds a shutdown hook, to kill the launched process.- See Also:
m_Process
-
ignoreEnvironmentOptions
public void ignoreEnvironmentOptions()
Sets to ignore the ADAMS environment options.
-
suppressErrorDialog
public void suppressErrorDialog()
Suppresses the error dialog.
-
setOutputPrinter
public void setOutputPrinter(Class cls)
Sets the output printer class to use.- Parameters:
cls- the class to use- Throws:
IllegalArgumentException- if the class is not derived fromAbstractOutputPrinter
-
setConsoleObject
public void setConsoleObject(LoggingObject owner)
Sets the owning console object to use for the output printer if that implementsLoggingObjectOwner.- Parameters:
owner- the owner for the output printer, can be null
-
getClassPath
protected String getClassPath()
Assembles the classpath.- Returns:
- the full classpath
-
getStdOut
public OutputProcessStream getStdOut()
Returns the stdout handler.- Returns:
- the handler
-
getStdErr
public OutputProcessStream getStdErr()
Returns the stderr handler.- Returns:
- the handler
-
destroy
public void destroy()
Allows killing the process.
-
execute
public String execute()
Launches the main class.- Returns:
- null if OK, otherwise an error message
-
configure
protected static String configure(String[] args, Launcher launcher)
Configures the launcher.- Parameters:
args- the commandline arguments to use for launchinglauncher- the launcher to configure
-
cleanOptions
protected static void cleanOptions(String[] args)
Removes unnecessary double quotes.- Parameters:
args- the arguments to clean
-
-