Class 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 Detail

      • 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
        Constant MANIFEST_ENTRY_ORIG_SHA1="VULAS-originalSHA1"
        See Also:
        Constant Field Values
      • hexArray

        protected static final char[] hexArray
        Constant hexArray
    • Constructor Detail

      • JarWriter

        public JarWriter​(Path _jar)
                  throws IOException

        Constructor for JarWriter.

        Parameters:
        _jar - a Path object.
        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 Manifest object.
      • 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 - a Path object.
        Returns:
        a Path object.
        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 - a String object.
      • addManifestEntry

        public void addManifestEntry​(String _key,
                                     String _val)
        Additional manifest file entries to be included in re-written archives. Must be called before "rewrite".
        Parameters:
        _key - a String object.
        _val - a String object.
      • hasManifestEntry

        public boolean hasManifestEntry​(String _key)
        Returns true if the given manifest file entry exists, false otherwise.
        Parameters:
        _key - a String object.
        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 - a String object.
      • getOriginalJarFileName

        public Path getOriginalJarFileName()
        Returns the file name of the original JAR file.
        Returns:
        a Path object.
      • getRewriteJarFileName

        public Path getRewriteJarFileName()
        Returns the file name of the to-be-rewritten JAR.
        Returns:
        a Path object.
      • addFiles

        public void addFiles​(String _target_dir,
                             Set<Path> _paths,
                             boolean _overwrite)

        addFiles.

        Parameters:
        _target_dir - a String object.
        _paths - a Set object.
        _overwrite - a boolean.
      • addFile

        public void addFile​(String _target_dir,
                            Path _path,
                            boolean _overwrite)

        addFile.

        Parameters:
        _target_dir - a String object.
        _path - a Path object.
        _overwrite - a boolean.
      • hasEntry

        public boolean hasEntry​(String _entry_name)

        hasEntry.

        Parameters:
        _entry_name - a String object.
        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 File object.
      • 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 (see getRewriteJarFileName()). See here: http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html
        Parameters:
        _todir - a Path object.
        Returns:
        a Path object.
        Throws:
        JarAnalysisException
      • appendToClasspath

        public static final void appendToClasspath​(Set<Path> _classpath,
                                                   Set<Path> _to_append,
                                                   boolean _preprocess)

        appendToClasspath.

        Parameters:
        _classpath - a Set object.
        _to_append - a Set object.
        _preprocess - a boolean.
      • appendToClasspath

        public static final Path appendToClasspath​(Set<Path> _classpath,
                                                   Path _to_append,
                                                   boolean _preprocess)
        Appends the given Path to 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.
        Parameters:
        _classpath - a Set object.
        _to_append - a Path object.
        _preprocess - a boolean.
        Returns:
        a Path object.