public class Util
extends java.lang.Object
This class also has some general-purpose utility methods
such as loadPropertiesFromResource(ClassLoader, String)
that might be useful for applications.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MKFIFO_PATH
Path to mkfifo executable, or null if it can't be found.
|
static java.lang.String |
PS_PATH
Path to ps executable, or null if it can't be found.
|
static java.lang.String |
SH_PATH
Path to sh executable, or null if it can't be found.
|
| Constructor and Description |
|---|
Util() |
| Modifier and Type | Method and Description |
|---|---|
static void |
cleanup(java.lang.String instanceName,
java.lang.Integer pid)
Clean up the pid file and FIFO used by the given instance.
|
static void |
deleteFile(java.lang.String fileName)
Delete a file.
|
static int |
exec(java.lang.String... cmd)
Execute a process.
|
static java.lang.String |
findCodeBase(java.lang.Class<?> cls)
Return the codebase from which the given class was loaded,
in a form suitable for using as a component of a classpath.
|
static java.lang.String |
findExe(java.lang.String exeName,
java.lang.String... path)
Find the executable with the given name by checking the directories
in the given path in order.
|
static java.lang.String[] |
getenvp()
Get the current environment variables in a form that can
be passed as an envp array to
Runtime.exec(String[], String[]). |
static java.lang.String |
getExternalizedFileName(java.lang.ClassLoader clsLoader,
java.lang.String resourceName)
Deprecated.
don't call this: call
getExternalizedFileName(ClassLoader, String, File) so that
the temporary directory where the externalized file will be placed is specified
explicitly |
static java.lang.String |
getExternalizedFileName(java.lang.ClassLoader clsLoader,
java.lang.String resourceName,
java.io.File tempDir)
Take a resource that is loadable from the given classloader
and make it available as a file in the filesystem.
|
static java.lang.String |
getFifoName(java.lang.String instanceName,
java.lang.Integer pid)
Get the name of the FIFO for given instance name and process id.
|
static java.lang.Object |
getJvmExecutablePath()
Get the path of the JVM executable.
|
static java.lang.Integer |
getPid()
Get this process's pid.
|
static java.lang.String |
getPidFileName(java.lang.String instanceName)
Get the name of the pid file for given instance name.
|
static boolean |
hasShellMetaCharacters(java.lang.String s)
Determine whether or not given string contains any shell metacharacters.
|
static boolean |
isRunning(java.lang.Integer pid)
Is there a process running with the given pid?
|
static java.util.Properties |
loadPropertiesFromResource(java.lang.ClassLoader clsLoader,
java.lang.String resourcePath)
Load properties from a resource embedded in the classpath.
|
static java.lang.Integer |
readPid(java.lang.String instanceName)
Read the pid file for given instance, returning the pid.
|
static java.io.BufferedReader |
readProcess(java.lang.String... cmd)
Read lines of text from a process.
|
static void |
sendCommand(java.lang.String instanceName,
java.lang.Integer pid,
java.lang.String command)
Send a command to the daemon process.
|
static void |
waitForExit(java.lang.Integer pid,
java.lang.Runnable callback)
Wait for given process to exit.
|
static void |
writePid(java.lang.String instanceName,
java.lang.Integer pid)
Write a pid file for instance.
|
public static final java.lang.String SH_PATH
public static final java.lang.String PS_PATH
public static final java.lang.String MKFIFO_PATH
public static java.lang.String findExe(java.lang.String exeName,
java.lang.String... path)
exeName - an executable name (e.g., "mkfifo")path - a path (e.g., "/bin", "/usr/bin")DaemonExceptionpublic static java.lang.Integer readPid(java.lang.String instanceName)
throws DaemonException
instanceName - the instance namejava.io.IOExceptionDaemonExceptionpublic static void writePid(java.lang.String instanceName,
java.lang.Integer pid)
throws DaemonException
instanceName - name of the instancepid - the pidDaemonExceptionpublic static java.lang.String getPidFileName(java.lang.String instanceName)
instanceName - an instance namepublic static java.lang.String getFifoName(java.lang.String instanceName,
java.lang.Integer pid)
instanceName - the instance namepid - the pidpublic static boolean isRunning(java.lang.Integer pid)
throws DaemonException
pid - the pidjava.io.IOExceptionDaemonExceptionpublic static java.io.BufferedReader readProcess(java.lang.String... cmd)
throws java.io.IOException
cmd - command (array of strings, first is program) to be used to start the processBufferedReader reading from the processjava.io.IOExceptionpublic static java.lang.String[] getenvp()
Runtime.exec(String[], String[]).public static java.lang.String findCodeBase(java.lang.Class<?> cls)
cls - the class to determine the codebase ofpublic static void deleteFile(java.lang.String fileName)
fileName - name of the file to deletepublic static void sendCommand(java.lang.String instanceName,
java.lang.Integer pid,
java.lang.String command)
throws DaemonException
instanceName - the instance namepid - the pid of the daemon processcommand - the command to sendDaemonExceptionpublic static int exec(java.lang.String... cmd)
throws DaemonException
cmd - the command (program and arguments)DaemonExceptionpublic static java.lang.Integer getPid()
throws DaemonException
DaemonExceptionhttp://blog.igorminar.com/2007/03/how-java-application-can-discover-its.htmlpublic static void waitForExit(java.lang.Integer pid,
java.lang.Runnable callback)
throws DaemonException
pid - the pid of the process to wait forcallback - callback whose run() method is called periodically (once per second)
if the process is still runningDaemonExceptionpublic static void cleanup(java.lang.String instanceName,
java.lang.Integer pid)
instanceName - the instance namepid - the pid of the exited instancepublic static boolean hasShellMetaCharacters(java.lang.String s)
s - public static java.lang.Object getJvmExecutablePath()
public static java.util.Properties loadPropertiesFromResource(java.lang.ClassLoader clsLoader,
java.lang.String resourcePath)
clsLoader - the ClassLoader to load the resource fromresourcePath - the resource from which the properties should be loadedjava.lang.IllegalStateException - if the properties cannot be loadedpublic static java.lang.String getExternalizedFileName(java.lang.ClassLoader clsLoader,
java.lang.String resourceName)
throws java.io.IOException
getExternalizedFileName(ClassLoader, String, File) so that
the temporary directory where the externalized file will be placed is specified
explicitlyclsLoader - the ClassLoader containing the resourceresourceName - the name of the resource to externalizejava.io.IOException - if the resource can't be externalizedpublic static java.lang.String getExternalizedFileName(java.lang.ClassLoader clsLoader,
java.lang.String resourceName,
java.io.File tempDir)
throws java.io.IOException
clsLoader - the ClassLoader containing the resourceresourceName - the name of the resource to externalizejava.io.IOException - if the resource can't be externalized