public class Capsule extends Object implements Runnable
This API is to be used by custom capsules to programmatically (rather than declaratively) configure the capsule and possibly provide custom behavior.
All non-final protected methods may be overridden by custom capsules. 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 custom capsule code, except by their overrides.
Final methods implement various utility or accessors, which may be freely used by custom capsules.
| 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 String |
VERSION |
| Modifier | Constructor and Description |
|---|---|
protected |
Capsule(Path jarFile,
Path cacheDir)
Constructs a capsule from the given JAR file.
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
buildAppId()
Computes and returns application's ID
|
protected List<String> |
buildArgs(List<String> args)
Returns a list of command line arguments to pass to the application.
|
protected List<Path> |
buildBootClassPath()
Compiles and returns the application's boot classpath as a list of paths.
|
protected List<Path> |
buildBootClassPathA()
Compiles and returns the paths to be appended to the application's boot classpath.
|
protected List<Path> |
buildBootClassPathP()
Compiles and returns the paths to be prepended to the application's boot classpath.
|
protected List<Path> |
buildClassPath()
Compiles and returns the application's classpath as a list of paths.
|
protected void |
buildEnvironmentVariables(Map<String,String> env)
Returns a map of environment variables (property-value pairs).
|
protected List<String> |
buildJVMArgs()
Returns a list of JVM arguments.
|
protected ProcessBuilder |
buildProcess()
Constructs a
ProcessBuilder that is later used to launch the capsule. |
protected Map<String,String> |
buildSystemProperties()
Returns a map of system properties (property-value pairs).
|
protected Path |
chooseJavaHome()
Chooses which Java installation to use for running the app.
|
protected int |
chooseLogLevel()
Chooses and returns the capsules log level.
|
protected String |
chooseMode()
Chooses this capsule's mode.
|
protected Object |
clone()
Throws a
CloneNotSupportedException |
protected static int |
compareVersions(String a,
String b)
Compares two dotted software versions.
|
protected static int |
compareVersions(String a,
String b,
int n)
Compares two dotted software versions, regarding only the first several version components.
|
boolean |
equals(Object obj) |
protected static List<String> |
exec(int numLines,
ProcessBuilder pb)
Executes a command and returns its output as a list of lines.
|
protected static List<String> |
exec(int numLines,
String... cmd)
Executes a command and returns its output as a list of lines.
|
protected static List<String> |
exec(ProcessBuilder pb)
Executes a command and returns its output as a list of lines.
|
protected static List<String> |
exec(String... cmd)
Executes a command and returns its output as a list of lines.
|
protected String |
expand(String str)
Expands occurrences of
$VARNAME in attribute values. |
protected void |
extractCapsule()
Extracts the capsule's contents into the app cache directory.
|
protected Path |
getAppCache()
This capsule's cache directory, or
null if capsule has been configured not to extract. |
protected String |
getAppId(List<String> args)
Returns the app's ID.
|
protected String |
getAttribute(String attr)
Returns the value of the given manifest attribute with consideration to the capsule's mode.
|
protected List<String> |
getDependencies()
Returns a list of dependencies, each in the format
groupId:artifactId:version[:classifier] (classifier is optional) |
protected Path |
getJarFile()
This capsule's JAR file.
|
protected Path |
getJavaExecutable()
Returns the path to the executable that will be used to launch Java.
|
protected static Path |
getJavaExecutable(Path javaHome)
Finds the path to the executable that will be used to launch Java within the given
javaHome. |
protected Path |
getJavaHome()
The path to the Java installation this capsule's app will use.
|
protected static Map<String,Path> |
getJavaHomes()
Returns all found Java installations.
|
protected List<String> |
getListAttribute(String attr)
Returns the value of the given attribute (with consideration to the capsule's mode) as a list.
|
protected Map<String,String> |
getMapAttribute(String attr,
String defaultValue)
Returns the value of the given attribute (with consideration to the capsule's mode) as a map.
|
protected String |
getMode()
This capsule's current mode.
|
protected String |
getModeDescription(String mode)
Returns the description of the given mode.
|
protected Set<String> |
getModes()
Returns the names of all modes defined in this capsule's manifest.
|
protected List<String> |
getNativeDependencies()
Constructs this capsule's native dependency list.
|
protected boolean |
hasAttribute(String attr)
Tests whether the given attribute is found in the manifest.
|
int |
hashCode() |
protected boolean |
isAppCacheUpToDate()
Tests if the app cache is up to date.
|
protected boolean |
isEmptyCapsule()
Tests if this is an empty capsule.
|
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 static String |
join(Collection<?> coll,
String separator)
Joins a collections into a string, separating elements with the given separator string.
|
protected static List<Path> |
listDir(Path dir)
Returns the contents of a directory
|
protected void |
log(int level,
String str)
Prints a message to stderr if the given log-level is being logged.
|
static void |
main(String[] args0)
Launches the capsule.
|
protected static Map<String,String> |
mapSplit(String map,
char kvSeparator,
String separator,
String defaultValue)
Splits a string into a map
|
protected static Capsule |
myCapsule()
Returns the singleton Capsule instance.
|
protected ProcessBuilder |
prelaunch(List<String> args)
Returns a configured
ProcessBuilder that is later used to launch the capsule. |
protected static List<String> |
split(String str,
String separator)
Splits a string into a list using a regex separator
|
String |
toString() |
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 String VERSION
protected String buildAppId()
protected List<String> buildArgs(List<String> args)
args - The command line arguments passed to the capsule at launchprotected List<Path> buildBootClassPath()
protected List<Path> buildBootClassPathA()
protected List<Path> buildBootClassPathP()
protected List<Path> buildClassPath()
protected void buildEnvironmentVariables(Map<String,String> env)
env - the current environmentprotected 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.
ProcessBuilderprotected Map<String,String> buildSystemProperties()
protected Path chooseJavaHome()
null if the current JVM is to be used.protected int chooseLogLevel()
protected String chooseMode()
protected final Object clone() throws CloneNotSupportedException
CloneNotSupportedExceptionclone in class ObjectCloneNotSupportedExceptionprotected static final int compareVersions(String a, String b)
a - first versionb - second version0 if a == b; > 0 if a > b; < 0 if a < b;protected static final int compareVersions(String a, 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 List<String> exec(int numLines, ProcessBuilder pb) throws 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 commandIOExceptionprotected static List<String> exec(int numLines, String... cmd) throws 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 commandIOExceptionprotected static List<String> exec(ProcessBuilder pb) throws IOException
!= 0.
exec(-1, pb}.pb - the ProcessBuilder that will be used to launch the commandIOExceptionprotected static List<String> exec(String... cmd) throws IOException
!= 0.
exec(-1, cmd}.cmd - the commandIOExceptionprotected String expand(String str)
$VARNAME in attribute values.str - the original stringprotected void extractCapsule()
protected final Path getAppCache()
null if capsule has been configured not to extract.protected final String getAppId(List<String> args)
args - the command line arguments; may be null.protected final String getAttribute(String attr)
attr - the attributeprotected List<String> getDependencies()
groupId:artifactId:version[:classifier] (classifier is optional)protected final Path getJarFile()
protected Path getJavaExecutable()
capsule.java.cmd property or the JAVACMD environment variable,
and if not set, returns the value of getJavaExecutable(getJavaHome()).protected static final Path getJavaExecutable(Path javaHome)
javaHome.protected final Path getJavaHome()
protected static Map<String,Path> getJavaHomes()
protected final List<String> getListAttribute(String attr)
attr - the attributeprotected final Map<String,String> getMapAttribute(String attr, String defaultValue)
attr - the attributeprotected final String getMode()
protected final String getModeDescription(String mode)
mode - null if no description is found.protected final Set<String> getModes()
protected List<String> getNativeDependencies()
groupId:artifactId:version[:classifier][,renameTo]
(classifier and renameTo are optional).protected final boolean hasAttribute(String attr)
attr - the attributeprotected boolean isAppCacheUpToDate()
getAppCache.
This creates a file in the app cache, whose timestamp is compared with the capsule's JAR timestamp.protected final boolean isEmptyCapsule()
protected final boolean isLogging(int level)
protected static final boolean isMac()
protected static final boolean isUnix()
protected static final boolean isWindows()
protected static final String join(Collection<?> coll, String separator)
protected static final List<Path> listDir(Path dir)
dir - the directoryprotected final void log(int level,
String str)
public static final void main(String[] args0)
protected static final Map<String,String> mapSplit(String map, char kvSeparator, String separator, String defaultValue)
map - the stringkvSeparator - the character separating each key from its corresponding valueseparator - a regex separator between key-value pairsdefaultValue - A default value to use for keys without a value, or null if such an event should throw an exceptionprotected static Capsule myCapsule()
protected ProcessBuilder prelaunch(List<String> args)
ProcessBuilder that is later used to launch the capsule.
The ProcessBuilder's IO redirection is left in its default settings.
Custom capsules may override this method to display a message prior to launch, or to configure the process's IO streams.
Other, more elaborate customization of the command are best done by overriding buildProcess.args - the application command-line argumentsProcessBuilderprotected static final List<String> split(String str, String separator)