public class SelfModifier extends Object
Create a SelfModifier with a target method, then invoke the SelfModifier with arguments to be passed to the target method. The jar file containing the target method's class (obtained by reflection) will be extracted to a temporary directory, and a new java process will be spawned to invoke the target method. The original jar file may now be modified.
If the constructor or invoke() methods fail, it is generally because secondary java processes could not be started. Requirements
Phase 1:
Constant | System property | description |
---|---|---|
BASE_KEY | self.mod.jar | base path to log file and sandbox dir |
JAR_KEY | self.mod.class | path to original jar file |
CLASS_KEY | self.mod.method | class of target method |
METHOD_KEY | self.mod.phase | name of method to be invoked in sandbox |
PHASE_KEY | self.mod.base | phase of operation to run |
Modifier and Type | Class and Description |
---|---|
static class |
SelfModifier.StreamProxy |
Modifier and Type | Field and Description |
---|---|
static String |
BASE_KEY
System property name of base for log and sandbox of secondary processes.
|
static String |
CLASS_KEY
System property name of class declaring target method.
|
private Date |
date |
private SimpleDateFormat |
isoPoint
For logging time.
|
static String |
JAR_KEY
System property name of original jar file containing application.
|
private File |
jarFile
Original jar file program was launched from.
|
private static String |
JAVA_HOME |
private static float |
JAVA_SPECIFICATION_VERSION
--------------------------------------------------------------------- Apache ant code
---------------------------------------------------------------------
|
(package private) PrintStream |
log
--------------------------------------------------------------------- Logging
---------------------------------------------------------------------
|
private File |
logFile
Log for phase 2 and 3, because we can't capture the stdio from them.
|
private Method |
method
Target method to be invoked in sandbox.
|
static String |
METHOD_KEY
System property name of target method to invoke in secondary process.
|
private int |
phase
Current phase of execution: 1, 2, or 3.
|
static String |
PHASE_KEY
System property name of phase (1, 2, or 3) indicator.
|
private String |
prefix
Base prefix name for sandbox and log, used only in phase 1.
|
private File |
sandbox
Directory which we extract too, invoke from, and finally delete.
|
Modifier | Constructor and Description |
---|---|
private |
SelfModifier()
Internal constructor where target class and method are obtained from system properties.
|
|
SelfModifier(Method method)
Creates a SelfModifier which will invoke the target method in a separate process from which
it may modify it's own jar file.
|
Modifier and Type | Method and Description |
---|---|
private static String |
addExtension(String command) |
private PrintStream |
checkLog() |
static boolean |
deleteTree(File file)
Recursively delete a file structure.
|
private void |
errlog(String msg) |
private void |
extractJarFile() |
static File |
findJarFile(Class clazz)
Retrieve the jar file the specified class was loaded from.
|
static String |
fromURI(String uri)
Constructs a file path from a
file: URI. |
private void |
initJavaExec()
This call ensures that java can be exec'd in a separate process.
|
private void |
initMethod(Method method)
Check the method for the required properties (public, static, params:(String[])).
|
void |
invoke(String[] args)
Invoke the target method in a separate process from which it may modify it's own jar file.
|
private void |
invoke2(String[] args)
Invoke phase 2, which starts phase 3, then cleans up the sandbox.
|
private void |
invoke3(String[] args)
Invoke the target method and let it run free!
|
private static String |
javaCommand() |
private void |
log(String msg) |
private void |
log(Throwable t) |
static void |
main(String[] args) |
private Process |
spawn(String[] args,
int nextPhase)
Run a new jvm with all the system parameters needed for phases 2 and 3.
|
static void |
test(String[] args) |
public static final String BASE_KEY
public static final String JAR_KEY
public static final String CLASS_KEY
public static final String METHOD_KEY
public static final String PHASE_KEY
private int phase
private SimpleDateFormat isoPoint
PrintStream log
private static final float JAVA_SPECIFICATION_VERSION
private SelfModifier() throws IOException
IOException
- for errors getting to the sandbox.SecurityException
- if access to the target method is deniedpublic SelfModifier(Method method) throws IOException
method
- a public, static method that accepts a String array as it's only parameter. Any
return value is ignored.NullPointerException
- if method
is nullIllegalArgumentException
- if method
is not public, static, and take a
String array as it's only argument, or of it's declaring class is not public.IllegalStateException
- if process was not invoked from a jar file, or an IOExceptioin
occured while accessing itIOException
- if java is unable to be executed as a separte processSecurityException
- if access to the method, or creation of a subprocess is deniedprivate void initMethod(Method method)
NullPointerException
- if method
is nullIllegalArgumentException
- if method
is not public, static, and take a
String array as it's only argument, or of it's declaring class is not public.SecurityException
- if access to the method is deniedprivate void initJavaExec() throws IOException
IOException
- if an I/O error occurs, indicating java is unable to be exec'dSecurityException
- if a security manager exists and doesn't allow creation of a
subprocesspublic void invoke(String[] args) throws IOException
System.exit(int)
.
The contents of the current jar file are extracted copied to a 'sandbox' directory from which
the method is invoked. The path to the original jar file is placed in the system property
JAR_KEY
.
args
- arguments to pass to the target method. May be empty or null to indicate no
arguments.IOException
- for lots of thingsIllegalStateException
- if method's class was not loaded from a jarprivate Process spawn(String[] args, int nextPhase) throws IOException
IOException
- if there is an error getting the cononical name of a pathpublic static File findJarFile(Class clazz)
SecurityException
- if access to is denied by SecurityManagerprivate void extractJarFile() throws IOException
IOException
private void invoke2(String[] args)
public static boolean deleteTree(File file)
private PrintStream checkLog()
public static String fromURI(String uri)
file:
URI.
Will be an absolute path if the given URI is absolute.
Swallows '%' that are not followed by two characters, doesn't deal with non-ASCII characters.
uri
- the URI designating a file in the local filesystem.private static String addExtension(String command)
private static String javaCommand()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses