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_OPTS
environment variable. Note: No checks are performed if the same parameter is defined in this env variable and on the commandline.
TheADAMS_LIBRARY_PATH
allows you to supply additional libraries that you would normally supply to the JVM using '-Djava.library.path=...'.
Use-help
to 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 int
CODE_RESTART
the return code for restarting the main class.static int
CODE_RESTART_MORE_HEAP
the return code for restarting the main class with 50% more heap.static String
ENV_ADAMS_LIBRARY_PATH
the environment variable for the library path.static String
ENV_ADAMS_OPTS
the environment variable with additional options.protected String[]
m_Arguments
the arguments for the process.protected List<String>
m_ClassPathAugmentations
optional classpath augmentations.protected boolean
m_CollapseClassPath
whether to collapse the classpath.protected LoggingObject
m_ConsoleObject
the console object that calls the launcher (if any).protected int
m_DebugLevel
the debug level.protected List<EnvironmentModifier>
m_EnvironmentModifiers
optional environment modifiers.protected List<String>
m_EnvVars
optional environment variables.protected boolean
m_IgnoreEnvironmentOptions
whether to ignore the ADAMS environment options.protected String
m_JavaAgentJar
the java agent jar to use.protected List<String>
m_JVMOptions
optional JVM options.protected Logger
m_Logger
for logging.protected String
m_MainClass
the main class to launch.protected String
m_Memory
the amount of memory to use for the process.protected Class
m_OutputPrinter
the output printer to use.protected List<String>
m_PriorityJars
optional priority jars.protected Process
m_Process
the process that got launched.protected Runtime
m_Runtime
the runtime object in use.protected OutputProcessStream
m_StdErr
for stderr.protected OutputProcessStream
m_StdOut
for stdout.protected boolean
m_SuppressErrorDialog
whether 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 void
addClassPathAugmentations(ClassPathAugmenter augmenter)
Adds the augmentations that the classpath augmenter returns.void
addClassPathAugmentations(String cmdline)
Adds the augmentations that the classpath augmenter returns.void
addEnvironmentModifier(String cmdline)
Adds the augmentations that the classpath augmenter returns.void
addEnvVar(String value)
Adds the environmental variable key-value pair (key=value).void
addJVMOption(String value)
Adds the JVM option.void
addPriorityJar(String value)
Adds the priority jar.void
addShutdownHook()
Adds a shutdown hook, to kill the launched process.protected static void
cleanOptions(String[] args)
Removes unnecessary double quotes.void
collapseClassPath(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 String
configure(String[] args, Launcher launcher)
Configures the launcher.void
destroy()
Allows killing the process.String
execute()
Launches the main class.protected String
getClassPath()
Assembles the classpath.OutputProcessStream
getStdErr()
Returns the stderr handler.OutputProcessStream
getStdOut()
Returns the stdout handler.void
ignoreEnvironmentOptions()
Sets to ignore the ADAMS environment options.protected void
increaseHeap()
Increase the heap by 50%.static void
main(String[] args)
Executes the class from the command-line.String
setArguments(String[] value)
Sets the arguments for the process.void
setConsoleObject(LoggingObject owner)
Sets the owning console object to use for the output printer if that implementsLoggingObjectOwner
.String
setDebugLevel(String value)
Sets the debug level (0 = off, >0 on).String
setJavaAgentJar(String value)
Sets the java agent jar of the process.String
setMainClass(String value)
Sets the main class of the process.String
setMemory(String value)
Sets the amount of memory for the process.void
setOutputPrinter(Class cls)
Sets the output printer class to use.void
suppressErrorDialog()
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.- 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
-
-