Package org.eclipse.steady.java
Class JarWriter
- java.lang.Object
-
- org.eclipse.steady.java.JarWriter
-
public class JarWriter extends Object
Utility class to extract and rewrite JAR files, offering the following possibilities to modify the JAR: Skip original manifest file entries, add new manifest file entries, add new JAR entries, replace the content of existing JAR entries (using JarEntryWriter).
-
-
Field Summary
Fields Modifier and Type Field Description SimpleDateFormatdateFormatprotected static char[]hexArrayConstanthexArraystatic StringMANIFEST_ENTRY_ORIG_SHA1ConstantMANIFEST_ENTRY_ORIG_SHA1="VULAS-originalSHA1"static StringMANIFEST_ENTRY_VULAS_MODIFIncluded in the manifest file of every JAR rewritten by Vulas.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFile(String _target_dir, Path _path, boolean _overwrite)addFile.voidaddFiles(String _target_dir, Set<Path> _paths, boolean _overwrite)addFiles.voidaddManifestEntry(String _key, String _val)Additional manifest file entries to be included in re-written archives.static PathappendToClasspath(Set<Path> _classpath, Path _to_append, boolean _preprocess)Appends the givenPathto the given set.static voidappendToClasspath(Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)appendToClasspath.Pathextract(Path _todir)Extract the JAR to a given directory, or to a new temporary directory if null.longgetFileSize()Returns the size of the original JAR file.longgetInstrumentedFileSize()Returns the size of the instrumented JAR file or -1 if no instrumentation took place.PathgetOriginalJarFileName()Returns the file name of the original JAR file.ManifestgetOriginalManifest()Returns the original manifest.PathgetRewriteJarFileName()Returns the file name of the to-be-rewritten JAR.FilegetRewrittenJarFile()The rewritten JAR file.StringgetSHA1()Returns the SHA1 digest of the JAR.booleanhasEntry(String _entry_name)hasEntry.booleanhasManifestEntry(String _key)Returns true if the given manifest file entry exists, false otherwise.booleanisRewrittenByVulas()Returns true if the JAR has been rewritten by Vulas.voidregister(String _regex, JarEntryWriter _writer)Register a JarEntryWriter for a given pattern.Pathrewrite(Path _todir)Rewrites the JAR into the directory specified by argument _todir.voidsetClassifier(String _string)Will be appended to the file name of re-written archives (if any).voidskipManifestEntry(String _entry)Entries of the original manifest which will not be rewritten.
-
-
-
Field Detail
-
dateFormat
public final SimpleDateFormat dateFormat
-
MANIFEST_ENTRY_VULAS_MODIF
public static final String MANIFEST_ENTRY_VULAS_MODIF
Included in the manifest file of every JAR rewritten by Vulas.- See Also:
- Constant Field Values
-
MANIFEST_ENTRY_ORIG_SHA1
public static final String MANIFEST_ENTRY_ORIG_SHA1
ConstantMANIFEST_ENTRY_ORIG_SHA1="VULAS-originalSHA1"- See Also:
- Constant Field Values
-
hexArray
protected static final char[] hexArray
ConstanthexArray
-
-
Constructor Detail
-
JarWriter
public JarWriter(Path _jar) throws IOException
Constructor for JarWriter.
- Parameters:
_jar- aPathobject.- Throws:
IOException- if any.
-
-
Method Detail
-
getFileSize
public long getFileSize()
Returns the size of the original JAR file.- Returns:
- a long.
- See Also:
getInstrumentedFileSize()
-
getInstrumentedFileSize
public long getInstrumentedFileSize()
Returns the size of the instrumented JAR file or -1 if no instrumentation took place.- Returns:
- a long.
- See Also:
getFileSize()
-
getOriginalManifest
public Manifest getOriginalManifest()
Returns the original manifest.- Returns:
- a
Manifestobject.
-
extract
public Path extract(Path _todir) throws IOException
Extract the JAR to a given directory, or to a new temporary directory if null.- Parameters:
_todir- aPathobject.- Returns:
- a
Pathobject. - Throws:
IOException- if any.
-
getSHA1
public String getSHA1()
Returns the SHA1 digest of the JAR. Either taken from the manifest (entry VULAS-originalSHA1, in case the original JAR has been instrumented offline), or by computing it on the fly.- Returns:
- the SHA1 digest of the JAR
-
skipManifestEntry
public void skipManifestEntry(String _entry)
Entries of the original manifest which will not be rewritten. Must be called before "rewrite".- Parameters:
_entry- aStringobject.
-
addManifestEntry
public void addManifestEntry(String _key, String _val)
Additional manifest file entries to be included in re-written archives. Must be called before "rewrite".
-
hasManifestEntry
public boolean hasManifestEntry(String _key)
Returns true if the given manifest file entry exists, false otherwise.- Parameters:
_key- aStringobject.- Returns:
- a boolean.
-
isRewrittenByVulas
public boolean isRewrittenByVulas()
Returns true if the JAR has been rewritten by Vulas. Implemented by checking manifest file entries.- Returns:
- a boolean.
-
setClassifier
public void setClassifier(String _string)
Will be appended to the file name of re-written archives (if any). Must be called before "rewrite".- Parameters:
_string- aStringobject.
-
register
public void register(String _regex, JarEntryWriter _writer)
Register a JarEntryWriter for a given pattern.- Parameters:
_regex- aStringobject._writer- aJarEntryWriterobject.
-
getOriginalJarFileName
public Path getOriginalJarFileName()
Returns the file name of the original JAR file.- Returns:
- a
Pathobject.
-
getRewriteJarFileName
public Path getRewriteJarFileName()
Returns the file name of the to-be-rewritten JAR.- Returns:
- a
Pathobject.
-
hasEntry
public boolean hasEntry(String _entry_name)
hasEntry.
- Parameters:
_entry_name- aStringobject.- Returns:
- a boolean.
-
getRewrittenJarFile
public File getRewrittenJarFile()
The rewritten JAR file. Must be called after "rewrite". If rewrite is called multiple times, this method only returns the last rewritten JAR file.- Returns:
- a
Fileobject.
-
rewrite
public Path rewrite(Path _todir) throws JarAnalysisException
Rewrites the JAR into the directory specified by argument _todir. If _todir is equal to null, the JAR will be rewritten to a temporary directory. If the target JAR already exists, it will not be written (seegetRewriteJarFileName()). See here: http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html- Parameters:
_todir- aPathobject.- Returns:
- a
Pathobject. - Throws:
JarAnalysisException
-
appendToClasspath
public static final void appendToClasspath(Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess)
appendToClasspath.
-
appendToClasspath
public static final Path appendToClasspath(Set<Path> _classpath, Path _to_append, boolean _preprocess)
Appends the givenPathto the given set. In case of Java archives, it is checked whether it contains a manifest entry "Class-Path", in which case the archive is re-written to a temporary file w/o this entry. The method returns the path that has been appended, which is identical to the given path unless an archive has been rewritten. TODO: Maybe add a parameter to specify problematic entries, rather than hardcoding "Class-Path" here.
-
-