| java.lang.Object com.izforge.izpack.uninstaller.SelfModifier
SelfModifier | public class SelfModifier (Code) | | Allows an application to modify the jar file from which it came, including outright deletion. The
jar file of an app is usually locked when java is run so this is normally not possible.
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
- The target method, and all it's required classes must be in a jar file.
- The Self Modifier, and its inner classes must also be in the jar file.
There are three system processes (or "phases") involved, the first invoked by the user, the
second and third by the SelfModifier.
Phase 1:
- Program is launched, SelfModifier is created, invoke(String[]) is called
- A temporary directory (or "sandbox") is created in the default temp directory, and the jar
file contents ar extracted into it
- Phase 2 is spawned using the sandbox as it's classpath, SelfModifier as the main class, the
arguments to "invoke(String[])" as the main arguments, and the SelfModifier system properties set.
- Immidiately exit so the system unlocks the jar file
Phase 2:
- Initializes from system properties.
- Spawn phase 3 exactly as phase 2 except the self.modifier.phase system properties set to 3.
- Wait for phase 3 to die
- Delete the temporary sandbox
Phase 3:
- Initializes from system properties.
- Redirect std err stream to the log
- Invoke the target method with arguments we were given
- The target method is expected to call exit(), or to not start any looping threads (e.g. AWT
thread). In other words, the target is the new "main" method.
SelfModifier system properties used to pass information
between processes.
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 |
author: Chadwick McHenry version: 1.0 |
Inner Class :public static class StreamProxy extends Thread | |
Field Summary | |
final public static String | BASE_KEY System property name of base for log and sandbox of secondary processes. | final public static String | CLASS_KEY System property name of class declaring target method. | final public static String | JAR_KEY System property name of original jar file containing application. | final public static String | METHOD_KEY System property name of target method to invoke in secondary process. | final public static String | PHASE_KEY System property name of phase (1, 2, or 3) indicator. | PrintStream | log |
Constructor Summary | |
public | 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.
The target method must be public, static, and take a single array of strings as its only
parameter. |
Method Summary | |
public static boolean | deleteTree(File file) Recursively delete a file structure. | public static File | findJarFile(Class<MultiVolumeInstaller> clazz) Retrieve the jar file the specified class was loaded from. | public static String | fromURI(String uri) Constructs a file path from a 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.
Parameters: uri - the URI designating a file in the local filesystem. | public void | invoke(String[] args) Invoke the target method in a separate process from which it may modify it's own jar file.
This method does not normally return. | public static void | main(String[] args) | public static void | test(String[] args) |
BASE_KEY | final public static String BASE_KEY(Code) | | System property name of base for log and sandbox of secondary processes.
|
CLASS_KEY | final public static String CLASS_KEY(Code) | | System property name of class declaring target method.
|
JAR_KEY | final public static String JAR_KEY(Code) | | System property name of original jar file containing application.
|
METHOD_KEY | final public static String METHOD_KEY(Code) | | System property name of target method to invoke in secondary process.
|
PHASE_KEY | final public static String PHASE_KEY(Code) | | System property name of phase (1, 2, or 3) indicator.
|
log | PrintStream log(Code) | | --------------------------------------------------------------------- Logging
---------------------------------------------------------------------
|
SelfModifier | public SelfModifier(Method method) throws IOException(Code) | | Creates a SelfModifier which will invoke the target method in a separate process from which
it may modify it's own jar file.
The target method must be public, static, and take a single array of strings as its only
parameter. The class which declares the method must also be public. Reflection is used to
ensure this.
Parameters: method - a public, static method that accepts a String array as it's only parameter. Anyreturn value is ignored. throws: NullPointerException - if method is null throws: IllegalArgumentException - if method is not public, static, and take aString array as it's only argument, or of it's declaring class is not public. throws: IllegalStateException - if process was not invoked from a jar file, or an IOExceptioinoccured while accessing it throws: IOException - if java is unable to be executed as a separte process throws: SecurityException - if access to the method, or creation of a subprocess is denied |
deleteTree | public static boolean deleteTree(File file)(Code) | | Recursively delete a file structure.
|
fromURI | public static String fromURI(String uri)(Code) | | Constructs a file path from a 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.
Parameters: uri - the URI designating a file in the local filesystem. the local file system path for the file. |
invoke | public void invoke(String[] args) throws IOException(Code) | | Invoke the target method in a separate process from which it may modify it's own jar file.
This method does not normally return. After spawning the secondary process, the current
process must die before the jar file is unlocked, therefore calling this method is akin to
calling
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
SelfModifier.JAR_KEY .
Parameters: args - arguments to pass to the target method. May be empty or null to indicate noarguments. throws: IOException - for lots of things throws: IllegalStateException - if method's class was not loaded from a jar |
|
|