package org.jvnet.hudson.maven.plugins.hpi;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.kohsuke.stapler.framework.io.IOException2;

import java.io.IOException;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.Arrays;
import java.util.List;

/**
 * @author Kohsuke Kawaguchi
 */
class HpiUtil {
    static boolean isPlugin(Artifact artifact) throws IOException {
        try {
            // some artifacts aren't even Java, so ignore those.
            if(!artifact.getType().equals("jar"))    return false;

            // this can happened with maven 3 and doesn't have any side effect here
            if(artifact.getFile() == null ) return false;
            // could a reactor member in member (mvn test-compile with core :-) )
            if(artifact.getFile().isDirectory()) return false;
            
            JarFile jar = new JarFile(artifact.getFile());
            try {
                Manifest manifest = jar.getManifest();
                if(manifest==null)  return false;
                for( String key : Arrays.asList("Plugin-Class","Plugin-Version")) {
                    if(manifest.getMainAttributes().getValue(key) != null)
                        return true;
                }
                return false;
            } finally {
                jar.close();
            }
        } catch (IOException e) {
            throw new IOException2("Failed to open artifact "+artifact.toString()+" at "+artifact.getFile(),e);
        }
    }

    static String findHudsonVersion(MavenProject project) {
        for(Artifact a : (Set<Artifact>)project.getArtifacts()) {
            if(a.getGroupId().equals("org.jvnet.hudson.main") && a.getArtifactId().equals("hudson-core")) {
                return a.getVersion();
            }
        }
        return null;
    }
}

@@ -49,168 +49,12 @@
 ort 
-org.apache.maven.artifact.Artifact;%0Aimport org.apache.maven.model.Dependency;%0Aimport org.apache.maven.project.MavenProject;%0Aimport org.kohsuke.stapler.framework
+java
 .io.
@@ -68,12 +68,10 @@
 tion
-2
 ;%0A
-%0A
 impo
@@ -74,38 +74,35 @@
 import java.
-io.IOException
+util.Arrays
 ;%0Aimport jav
@@ -178,54 +178,150 @@
 st;%0A
+%0A
 import 
-java.util.Arrays;%0Aimport java.util.List
+org.apache.maven.artifact.Artifact;%0Aimport org.apache.maven.project.MavenProject;%0Aimport org.kohsuke.stapler.framework.io.IOException2
 ;%0A%0A/

remove unused imports