Class SignerJar

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class SignerJar
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Generator of signed Jars. It stores some data in memory therefore it is not suited for creation of large files. The usage:
     KeyStore keystore = KeyStore.getInstance("JKS");
     keyStore.load(keystoreStream, "keystorePassword");
     SignerJar jar = new SignerJar(out, keyStore, "keyAlias", "keyPassword");
     signedJar.addManifestAttribute("Main-Class", "com.example.MainClass");
     signedJar.addManifestAttribute("Application-Name", "Example");
     signedJar.addManifestAttribute("Permissions", "all-permissions");
     signedJar.addManifestAttribute("Codebase", "*");
     signedJar.addFileContents("com/example/MainClass.class", clsData);
     signedJar.addFileContents("JNLP-INF/APPLICATION.JNLP", generateJnlpContents());
     signedJar.close();
     
    • Constructor Summary

      Constructors 
      Constructor Description
      SignerJar​(java.util.zip.ZipOutputStream out, java.util.function.Supplier<org.bouncycastle.cms.CMSSignedDataGenerator> gen, java.lang.String sig_fn, java.lang.String sig_key_fn)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFileContents​(java.lang.String filename, byte[] contents)
      Adds a file to the JAR.
      void addFileContents​(java.lang.String filename, java.io.InputStream contents)
      Adds a file to the JAR.
      void addFileContents​(java.util.zip.ZipEntry entry, byte[] contents)
      Adds a file to the JAR.
      void addFileContents​(java.util.zip.ZipEntry entry, java.io.InputStream contents)
      Adds a file to the JAR.
      void addManifestAttribute​(java.lang.String name, java.lang.String value)
      Adds a header to the manifest of the JAR.
      void close()
      Closes the JAR file by writing the manifest and signature data to it and finishing the ZIP entries.
      void finish()
      Finishes the JAR file by writing the manifest and signature data to it and finishing the ZIP entries.
      java.util.zip.ZipOutputStream getZos()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SignerJar

        public SignerJar​(java.util.zip.ZipOutputStream out,
                         java.util.function.Supplier<org.bouncycastle.cms.CMSSignedDataGenerator> gen,
                         java.lang.String sig_fn,
                         java.lang.String sig_key_fn)
    • Method Detail

      • addFileContents

        public void addFileContents​(java.lang.String filename,
                                    byte[] contents)
                             throws java.io.IOException
        Adds a file to the JAR. The file is immediately added to the zipped output stream. This method cannot be called once the stream is closed.
        Parameters:
        filename - name of the file to add (use forward slash as a path separator)
        contents - contents of the file
        Throws:
        java.io.IOException
        java.lang.NullPointerException - if any of the arguments is null
      • addFileContents

        public void addFileContents​(java.lang.String filename,
                                    java.io.InputStream contents)
                             throws java.io.IOException
        Adds a file to the JAR. The file is immediately added to the zipped output stream. This method cannot be called once the stream is closed.
        Parameters:
        filename - name of the file to add (use forward slash as a path separator)
        contents - contents of the file
        Throws:
        java.io.IOException
        java.lang.NullPointerException - if any of the arguments is null
      • addFileContents

        public void addFileContents​(java.util.zip.ZipEntry entry,
                                    byte[] contents)
                             throws java.io.IOException
        Adds a file to the JAR. The file is immediately added to the zipped output stream. This method cannot be called once the stream is closed.
        Parameters:
        entry - name of the file to add (use forward slash as a path separator)
        contents - contents of the file
        Throws:
        java.io.IOException
        java.lang.NullPointerException - if any of the arguments is null
      • addFileContents

        public void addFileContents​(java.util.zip.ZipEntry entry,
                                    java.io.InputStream contents)
                             throws java.io.IOException
        Adds a file to the JAR. The file is immediately added to the zipped output stream. This method cannot be called once the stream is closed.
        Parameters:
        entry - name of the file to add (use forward slash as a path separator)
        contents - contents of the file
        Throws:
        java.io.IOException
        java.lang.NullPointerException - if any of the arguments is null
      • addManifestAttribute

        public void addManifestAttribute​(java.lang.String name,
                                         java.lang.String value)
        Adds a header to the manifest of the JAR.
        Parameters:
        name - name of the attribute, it is placed into the main section of the manifest file
        value - value of the attribute
      • close

        public void close()
                   throws java.io.IOException
        Closes the JAR file by writing the manifest and signature data to it and finishing the ZIP entries. It closes the underlying stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException
        java.lang.RuntimeException - if the signing goes wrong
      • finish

        public void finish()
                    throws java.io.IOException
        Finishes the JAR file by writing the manifest and signature data to it and finishing the ZIP entries. It leaves the underlying stream open.
        Throws:
        java.io.IOException
        java.lang.RuntimeException - if the signing goes wrong
      • getZos

        public java.util.zip.ZipOutputStream getZos()