/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.test.integration.deployment.jcedeployment;

import org.xnio.IoUtils;
import sun.security.tools.JarSigner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Utility for signing jars.
 *
 * @author <a href="mailto:istudens@redhat.com">Ivo Studensky</a>
 */
public class JarSignerUtil {
    private File keystore;
    private String storePass;
    private String keyPass;
    private String alias;

    public JarSignerUtil(final File keystore, final String storePass, final String keyPass, final String alias) {
        this.keystore = keystore;
        this.storePass = storePass;
        this.keyPass = keyPass;
        this.alias = alias;
    }

    public void verify(final File jar) {
        String[] args = {"-verify", "-verbose", jar.getAbsolutePath()};
        final JarSigner jarSigner = new JarSigner();
        jarSigner.run(args);
    }

    public void sign(final File jar, final File signedJar) throws IOException {
        copyFile(jar, signedJar);
        sign(signedJar);
    }

    private void sign(final File jar) {
        String[] args = {
                "-keystore", keystore.getAbsolutePath(),
                "-storepass", storePass,
                "-keypass", keyPass,
                jar.getAbsolutePath(),
                alias
        };
        final JarSigner signer = new JarSigner();
        signer.run(args);
    }

    private static void copyFile(final File src, final File dst) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            byte[] buffer = new byte[4096];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } finally {
            IoUtils.safeClose(in);
            IoUtils.safeClose(out);
        }
    }

}

@@ -1085,71 +1085,8 @@
 t;%0A%0A
-import org.xnio.IoUtils;%0Aimport sun.security.tools.JarSigner;%0A%0A
 impo
@@ -1194,16 +1194,127 @@
 ception;
+%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A%0Aimport org.xnio.IoUtils;
 %0A%0A/**%0A *
@@ -1835,32 +1835,49 @@
 (final File jar)
+ throws Exception
  %7B%0A        Strin
@@ -1947,81 +1947,1313 @@
     
-final JarSigner jarSigner = new JarSigner();%0A        jarSigner.run(args);
+run(args);%0A    %7D%0A%0A    private void run(String%5B%5D args) %7B%0A        try %7B%0A            Class%3C?%3E jdk7JarSignerClass = this.getClass().getClassLoader().loadClass(%22sun.security.tools.JarSigner%22);%0A            Object jdk7JarSigner = jdk7JarSignerClass.newInstance();%0A            Method run = jdk7JarSigner.getClass().getDeclaredMethod(%22run%22, String%5B%5D.class);%0A            run.invoke(jdk7JarSigner, new Object%5B%5D%7Bargs%7D);%0A        %7D catch (ClassNotFoundException %7C NoSuchMethodException e) %7B%0A            //not jdk7, try jdk8%0A            runJDK8(args);%0A        %7D catch (InvocationTargetException %7C IllegalAccessException %7C InstantiationException e) %7B%0A            throw new RuntimeException(%22could not sign%22, e);%0A        %7D%0A    %7D%0A%0A    private void runJDK8(String%5B%5D args) %7B%0A        try %7B%0A            Class%3C?%3E jdk8JarSigner = this.getClass().getClassLoader().loadClass(%22sun.security.tools.jarsigner.Main%22);%0A            Method run = jdk8JarSigner.getMethod(%22main%22, String%5B%5D.class);%0A            run.invoke(null, new Object%5B%5D%7Bargs%7D);%0A        %7D catch (ClassNotFoundException %7C NoSuchMethodException e) %7B%0A            throw new RuntimeException(%22Could not find JarSigner to invoke%22, e);%0A        %7D catch (InvocationTargetException %7C IllegalAccessException e) %7B%0A            throw new RuntimeException(%22could not sign%22, e);%0A        %7D%0A
 %0A   
@@ -3366,24 +3366,42 @@
 signedJar);%0A
+        try %7B%0A    
         sign
@@ -3409,24 +3409,104 @@
 signedJar);%0A
+        %7D catch (Exception e) %7B%0A            throw new IOException(e);%0A        %7D%0A
     %7D%0A%0A    p
@@ -3537,16 +3537,33 @@
 ile jar)
+ throws Exception
  %7B%0A     
@@ -3802,65 +3802,8 @@
     
-final JarSigner signer = new JarSigner();%0A        signer.
 run(

Fix JarSigner to work with JDK7 & 8