public class Capsule
extends java.lang.Object
implements java.lang.Runnable
This API is to be used by caplets (custom capsules) to programmatically (rather than declaratively) configure the capsule and possibly provide custom behavior.
All non-final protected methods may be overridden by caplets. These methods will usually be called once, but they must be idempotent,
i.e. if called numerous times they must always return the same value, and produce the same effect as if called once.
Overridden methods need not be thread-safe, and are guaranteed to be called by a single thread at a time.
Overridable (non-final) methods must never be called directly by caplet code, except by their overrides.
Final methods implement various utility or accessors, which may be freely used by caplets.
For command line option handling, see registerOption.
| Modifier and Type | Field and Description |
|---|---|
protected static int |
LOG_DEBUG |
protected static int |
LOG_NONE |
protected static int |
LOG_QUIET |
protected static int |
LOG_VERBOSE |
protected static java.lang.String |
VERSION |
| Modifier | Constructor and Description |
|---|---|
protected |
Capsule(Capsule pred)
Caplets that will be listed on the manifest's
Caplets attribute must use this constructor. |
protected |
Capsule(java.nio.file.Path jarFile,
java.nio.file.Path cacheDir)
Constructs a capsule.
|
| Modifier and Type | Method and Description |
|---|---|
protected java.lang.String[] |
buildAppId()
Computes and returns application's ID.
|
protected java.util.List<java.lang.String> |
buildArgs(java.util.List<java.lang.String> args)
Returns a list of command line arguments to pass to the application.
|
protected java.util.List<java.nio.file.Path> |
buildBootClassPath()
Compiles and returns the application's boot classpath as a list of paths.
|
protected java.util.List<java.nio.file.Path> |
buildBootClassPathA()
Compiles and returns the paths to be appended to the application's boot classpath.
|
protected java.util.List<java.nio.file.Path> |
buildBootClassPathP()
Compiles and returns the paths to be prepended to the application's boot classpath.
|
protected java.util.List<java.nio.file.Path> |
buildClassPath()
Compiles and returns the application's classpath as a list of paths.
|
protected java.util.Map<java.lang.String,java.lang.String> |
buildEnvironmentVariables(java.util.Map<java.lang.String,java.lang.String> env)
Returns a map of environment variables (property-value pairs).
|
protected java.util.Map<java.nio.file.Path,java.lang.String> |
buildJavaAgents()
Returns all Java agents that will be launched with the application.
|
protected java.util.List<java.lang.String> |
buildJVMArgs()
Returns a list of JVM arguments.
|
protected java.util.List<java.nio.file.Path> |
buildNativeLibraryPath()
Compiles and returns the application's native library as a list of paths.
|
protected java.lang.ProcessBuilder |
buildProcess()
Constructs a
ProcessBuilder that is later used to launch the capsule. |
protected java.util.Map<java.lang.String,java.lang.String> |
buildSystemProperties()
Returns a map of system properties (property-value pairs).
|
protected java.nio.file.Path |
chooseJavaHome()
Chooses which Java installation to use for running the app.
|
protected int |
chooseLogLevel()
Chooses and returns the capsules log level.
|
protected java.lang.String |
chooseMode()
Chooses this capsule's mode.
|
protected void |
cleanup()
Called when the capsule exits after a successful or failed attempt to launch the application.
|
protected java.lang.Object |
clone()
Throws a
CloneNotSupportedException |
protected static int |
compareVersions(java.lang.String a,
java.lang.String b)
Compares two dotted software versions.
|
protected static int |
compareVersions(java.lang.String a,
java.lang.String b,
int n)
Compares two dotted software versions, regarding only the first several version components.
|
protected static void |
copy(java.nio.file.Path source,
java.nio.file.Path target)
Copies the source file or directory (recursively) to the target location.
|
protected static void |
delete(java.nio.file.Path path)
Deletes the given file or directory (even if nonempty).
|
boolean |
equals(java.lang.Object obj) |
protected static java.util.List<java.lang.String> |
exec(int numLines,
java.lang.ProcessBuilder pb)
Executes a command and returns its output as a list of lines.
|
protected static java.util.List<java.lang.String> |
exec(int numLines,
java.lang.String... cmd)
Executes a command and returns its output as a list of lines.
|
protected static java.util.List<java.lang.String> |
exec(java.lang.ProcessBuilder pb)
Executes a command and returns its output as a list of lines.
|
protected static java.util.List<java.lang.String> |
exec(java.lang.String... cmd)
Executes a command and returns its output as a list of lines.
|
protected java.lang.String |
expand(java.lang.String str)
Expands occurrences of
$VARNAME in attribute values. |
protected void |
extractCapsule()
Extracts the capsule's contents into the app cache directory.
|
protected java.nio.file.Path |
getAppCache()
This capsule's cache directory, or
null if capsule has been configured not to extract. |
protected java.lang.String |
getAppId()
Returns the app's ID.
|
protected java.lang.String |
getAppName()
The app's name
|
protected java.lang.String |
getAppVersion()
The app's version or
null if unversioned. |
protected java.lang.String |
getAttribute(java.lang.String attr)
Returns the value of the given manifest attribute with consideration to the capsule's mode.
|
protected java.lang.String |
getAttribute0(java.lang.String attr) |
protected java.util.List<java.lang.String> |
getDependencies()
Returns a list of dependencies, each in the format
groupId:artifactId:version[:classifier] (classifier is optional) |
protected java.nio.file.Path |
getJarFile()
This capsule's JAR file.
|
protected java.nio.file.Path |
getJavaExecutable()
Returns the path to the executable that will be used to launch Java.
|
protected static java.nio.file.Path |
getJavaExecutable(java.nio.file.Path javaHome)
Finds the path to the executable that will be used to launch Java within the given
javaHome. |
protected java.nio.file.Path |
getJavaHome()
The path to the Java installation this capsule's app will use.
|
protected static java.util.Map<java.lang.String,java.nio.file.Path> |
getJavaHomes()
Returns all found Java installations.
|
protected java.util.List<java.lang.String> |
getListAttribute(java.lang.String attr)
Returns the value of the given attribute (with consideration to the capsule's mode) as a list.
|
protected java.nio.file.Path |
getLocalRepo()
Returns the path to the local dependency repository.
|
protected int |
getLogLevel()
Capsule's log level
|
protected java.util.Map<java.lang.String,java.lang.String> |
getMapAttribute(java.lang.String attr,
java.lang.String defaultValue)
Returns the value of the given attribute (with consideration to the capsule's mode) as a map.
|
protected java.lang.String |
getMode()
This capsule's current mode.
|
protected java.lang.String |
getModeDescription(java.lang.String mode)
Returns the description of the given mode.
|
protected java.util.Set<java.lang.String> |
getModes()
Returns the names of all modes defined in this capsule's manifest.
|
protected java.util.List<java.lang.String> |
getNativeDependencies()
Constructs this capsule's native dependency list.
|
protected java.util.List<java.nio.file.Path> |
getPlatformNativeLibraryPath()
Returns the default native library path for the Java platform the application uses.
|
protected boolean |
hasAttribute(java.lang.String attr)
Tests whether the given attribute is found in the manifest.
|
int |
hashCode() |
protected boolean |
isAppCacheUpToDate()
Whether the app cache is up to date.
|
protected boolean |
isLogging(int level)
Tests if the given log level is currently being logged
|
protected static boolean |
isMac()
Tests whether the current OS is MacOS.
|
protected static boolean |
isUnix()
Tests whether the current OS is UNIX/Linux.
|
protected static boolean |
isWindows()
Tests whether the current OS is Windows.
|
protected boolean |
isWrapperCapsule()
Whether or not this is an empty capsule
|
protected static java.util.List<java.nio.file.Path> |
listDir(java.nio.file.Path dir,
java.lang.String glob,
boolean regular)
Returns the contents of a directory.
|
protected void |
log(int level,
java.lang.String str)
Prints a message to stderr if the given log-level is being logged.
|
static void |
main(java.lang.String[] args) |
protected void |
markCache()
Called after a successful completion of launch preparation if an app cache is used
to write persistent information to the cache denoting the successful preparation.
|
protected boolean |
needsAppCache() |
protected static java.util.List<java.lang.String> |
parse(java.lang.String value)
Parses an attribute's value string into a list.
|
protected static java.util.Map<java.lang.String,java.lang.String> |
parse(java.lang.String value,
java.lang.String defaultValue)
Parses an attribute's value string into an ordered map.
|
protected java.lang.ProcessBuilder |
prelaunch(java.util.List<java.lang.String> args)
Returns a configured
ProcessBuilder that is later used to launch the capsule. |
protected java.lang.String |
processOutgoingPath(java.nio.file.Path p)
Every path emitted by the capsule to the app's command line, system properties or environment variables is
first passed through this method.
|
protected static void |
registerOption(java.lang.String optionName,
java.lang.String methodName,
java.lang.String defaultValue,
java.lang.String description)
Registers a capsule command-line option.
|
protected static java.lang.String |
systemProperty(java.lang.String propName)
Same as
System.getProperty(propName) but sets context for error reporting. |
protected boolean |
testAppCacheUpToDate()
Tests if the app cache is up to date.
|
java.lang.String |
toString() |
protected static java.lang.String |
toStringValue(java.util.Collection<?> list)
Combines collection elements into a string that can be used as the value of an attribute.
|
protected static java.lang.String |
toStringValue(java.util.Map<?,?> map)
Combines map elements into a string that can be used as the value of an attribute.
|
protected static final int LOG_DEBUG
protected static final int LOG_NONE
protected static final int LOG_QUIET
protected static final int LOG_VERBOSE
protected static final java.lang.String VERSION
protected Capsule(Capsule pred)
Caplets attribute must use this constructor.
Caplets are required to have a constructor with the same signature as this constructor, and pass their arguments to up to this constructor.pred - The capsule preceding this one in the chain (caplets must not access the passed capsule in their constructor).protected Capsule(java.nio.file.Path jarFile,
java.nio.file.Path cacheDir)
This constructor is used by a caplet that will be listed in the manifest's Main-Class attribute.
Caplets are encouraged to "override" the other constructor so that they may be listed
in the Caplets attribute.
This constructor or that of a subclass must not make use of any registered capsule options, as they may not have been properly pre-processed yet.
jarFile - the path to the JAR filecacheDir - the path to the (shared) Capsule cache directoryprotected java.lang.String[] buildAppId()
null if no version).protected java.util.List<java.lang.String> buildArgs(java.util.List<java.lang.String> args)
args - The command line arguments passed to the capsule at launchprotected java.util.List<java.nio.file.Path> buildBootClassPath()
protected java.util.List<java.nio.file.Path> buildBootClassPathA()
protected java.util.List<java.nio.file.Path> buildBootClassPathP()
protected java.util.List<java.nio.file.Path> buildClassPath()
protected java.util.Map<java.lang.String,java.lang.String> buildEnvironmentVariables(java.util.Map<java.lang.String,java.lang.String> env)
env - the current environmentprotected java.util.Map<java.nio.file.Path,java.lang.String> buildJavaAgents()
protected java.util.List<java.lang.String> buildJVMArgs()
protected java.util.List<java.nio.file.Path> buildNativeLibraryPath()
protected java.lang.ProcessBuilder buildProcess()
ProcessBuilder that is later used to launch the capsule.
The returned process builder should contain the command minus the application arguments (which are later constructed by
buildArgs and appended to the command).buildEnvironmentVariables.
This implementation tries to create a process running a startup script, and, if one has not been set, constructs a Java process.
This method should be overridden to add new types of processes the capsule can launch (like, say, Python scripts).
If all you want is to configure the returned ProcessBuilder, for example to set IO stream redirection,
you should override prelaunch.
ProcessBuilder (must never be null).protected java.util.Map<java.lang.String,java.lang.String> buildSystemProperties()
protected java.nio.file.Path chooseJavaHome()
null if the current JVM is to be used.protected int chooseLogLevel()
protected java.lang.String chooseMode()
protected void cleanup()
super.cleanup() even in the event of an abnormal termination
(i.e. when an exception is thrown). This method must not throw any exceptions. All exceptions origination by cleanup
must be wither ignored completely or printed to STDERR.protected final java.lang.Object clone()
throws java.lang.CloneNotSupportedException
CloneNotSupportedExceptionclone in class java.lang.Objectjava.lang.CloneNotSupportedExceptionprotected static final int compareVersions(java.lang.String a,
java.lang.String b)
a - first versionb - second version0 if a == b; > 0 if a > b; < 0 if a < b;protected static final int compareVersions(java.lang.String a,
java.lang.String b,
int n)
a - first versionb - second versionn - the number of (most significant) components to consider0 if a == b; > 0 if a > b; < 0 if a < b;protected static void copy(java.nio.file.Path source,
java.nio.file.Path target)
throws java.io.IOException
java.io.IOExceptionprotected static void delete(java.nio.file.Path path)
throws java.io.IOException
java.io.IOExceptionpublic final boolean equals(java.lang.Object obj)
equals in class java.lang.Objectprotected static java.util.List<java.lang.String> exec(int numLines,
java.lang.ProcessBuilder pb)
throws java.io.IOException
numLines, or if numLines < 0, then the method will wait for the child process
to terminate, and throw an exception if the command returns an exit value != 0.numLines - the maximum number of lines to read, or -1 for an unbounded numberpb - the ProcessBuilder that will be used to launch the commandjava.io.IOExceptionprotected static java.util.List<java.lang.String> exec(int numLines,
java.lang.String... cmd)
throws java.io.IOException
numLines, or if numLines < 0, then the method will wait for the child process
to terminate, and throw an exception if the command returns an exit value != 0.numLines - the maximum number of lines to read, or -1 for an unbounded numbercmd - the commandjava.io.IOExceptionprotected static java.util.List<java.lang.String> exec(java.lang.ProcessBuilder pb)
throws java.io.IOException
!= 0.
exec(-1, pb}.pb - the ProcessBuilder that will be used to launch the commandjava.io.IOExceptionprotected static java.util.List<java.lang.String> exec(java.lang.String... cmd)
throws java.io.IOException
!= 0.
exec(-1, cmd}.cmd - the commandjava.io.IOExceptionprotected java.lang.String expand(java.lang.String str)
$VARNAME in attribute values.str - the original stringprotected void extractCapsule()
throws java.io.IOException
java.io.IOExceptionprotected final java.nio.file.Path getAppCache()
null if capsule has been configured not to extract.protected final java.lang.String getAppId()
protected final java.lang.String getAppName()
protected final java.lang.String getAppVersion()
null if unversioned.protected final java.lang.String getAttribute(java.lang.String attr)
attr - the attributeprotected final java.lang.String getAttribute0(java.lang.String attr)
protected java.util.List<java.lang.String> getDependencies()
groupId:artifactId:version[:classifier] (classifier is optional)protected final java.nio.file.Path getJarFile()
protected java.nio.file.Path getJavaExecutable()
capsule.java.cmd property or the JAVACMD environment variable,
and if not set, returns the value of getJavaExecutable(getJavaHome()).protected static final java.nio.file.Path getJavaExecutable(java.nio.file.Path javaHome)
javaHome.protected final java.nio.file.Path getJavaHome()
protected static java.util.Map<java.lang.String,java.nio.file.Path> getJavaHomes()
protected final java.util.List<java.lang.String> getListAttribute(java.lang.String attr)
Same as parse(getAttribute(attr)).
attr - the attributeprotected final java.nio.file.Path getLocalRepo()
protected final int getLogLevel()
protected final java.util.Map<java.lang.String,java.lang.String> getMapAttribute(java.lang.String attr,
java.lang.String defaultValue)
Same as parse(getAttribute(attr), defaultValue).
attr - the attributedefaultValue - a default value to use for keys without a value, or null if such an event should throw an exceptionprotected final java.lang.String getMode()
protected final java.lang.String getModeDescription(java.lang.String mode)
protected final java.util.Set<java.lang.String> getModes()
protected java.util.List<java.lang.String> getNativeDependencies()
groupId:artifactId:version[:classifier][,renameTo]
(classifier and renameTo are optional).protected java.util.List<java.nio.file.Path> getPlatformNativeLibraryPath()
protected final boolean hasAttribute(java.lang.String attr)
attr - the attributepublic final int hashCode()
hashCode in class java.lang.Objectprotected final boolean isAppCacheUpToDate()
testAppCacheUpToDate().protected final boolean isLogging(int level)
protected static final boolean isMac()
protected static final boolean isUnix()
protected static final boolean isWindows()
protected final boolean isWrapperCapsule()
protected static final java.util.List<java.nio.file.Path> listDir(java.nio.file.Path dir,
java.lang.String glob,
boolean regular)
null as the glob pattern is the same as passing "*"dir - the directoryglob - the glob pattern to use to filter the entries, or null if all entries are to be returnedregular - whether only regular files should be returnedprotected final void log(int level,
java.lang.String str)
public static final void main(java.lang.String[] args)
protected void markCache()
throws java.io.IOException
java.io.IOExceptionprotected boolean needsAppCache()
true if this capsule requires an app cache; false otherwise.protected static final java.util.List<java.lang.String> parse(java.lang.String value)
value - the attribute's valueprotected static final java.util.Map<java.lang.String,java.lang.String> parse(java.lang.String value,
java.lang.String defaultValue)
value - the attribute's valuedefaultValue - a default value to use for keys without a value, or null if such an event should throw an exceptionprotected java.lang.ProcessBuilder prelaunch(java.util.List<java.lang.String> args)
ProcessBuilder that is later used to launch the capsule.
The ProcessBuilder's IO redirection is left in its default settings.
Caplets may override this method to display a message prior to launch, or to configure the process's IO streams.
For more elaborate manipulation of the Capsule's launched process, consider overriding buildProcess.args - the application command-line argumentsProcessBuilder (if null, the launch will be aborted).protected java.lang.String processOutgoingPath(java.nio.file.Path p)
p - the pathprotected static final void registerOption(java.lang.String optionName,
java.lang.String methodName,
java.lang.String defaultValue,
java.lang.String description)
Capsule options are system properties beginning with the prefix ".capsule", normally passed to the capsule as -D flags on the command line.
Options can be top-level *actions* (like print dependency tree or list JVMs), in which case the methodName argument must
be the name of a method used to launch the action instead of launching the capsule.
Options can have a default value, which will be automatically assigned to the system property if undefined. The default values
"true" and "false" are treated specially. If one of them is the assigned default value, and the system property
is defined with with a value of the empty string, then it will be re-assigned the value "true".
Simple Command Line Options for Wrapper Capsules
When the capsule serves as a wrapper (i.e. it's an empty capsule used to launch an executable artifact or another capsule)
then the options can also be passed to the capsule as simple command line options (arguments starting with a hyphen),
with the "capsule." prefix removed, and every '.' character replaced with a '-'.
These command line arguments will automatically be converted to system properties, which will take their value from the argument
following the option (i.e. -option value), unless the option is given one of the special default values
"true" or "false", in which case it is treated as a flag with no arguments (note that an option with the default
value "true" will therefore not be able to be turned off if simple options are used).
optionName - the name of the system property for the option; must begin with "capsule.".methodName - if non-null, then the option is a top-level action (like print dependency tree or list JVMs),
and this is the method which will run the action.
The method must accept a single args parameter of type List<String>.defaultValue - the option's default value ("true" and "false" are specially treated; see above).protected static final java.lang.String systemProperty(java.lang.String propName)
System.getProperty(propName) but sets context for error reporting.protected boolean testAppCacheUpToDate()
throws java.io.IOException
getAppCache.
This creates a file in the app cache, whose timestamp is compared with the capsule's JAR timestamp.java.io.IOExceptionpublic java.lang.String toString()
toString in class java.lang.Objectprotected static final java.lang.String toStringValue(java.util.Collection<?> list)
protected static final java.lang.String toStringValue(java.util.Map<?,?> map)