changeset 336:e21f13506c3c

Added check for main class in jar manifest(s)
author Deepak Bhole <dbhole@redhat.com>
date Tue, 28 Feb 2012 11:35:41 -0500
parents d3b97728550a
children c93ef2cc3535
files ChangeLog netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
diffstat 2 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Feb 28 10:05:57 2012 -0500
+++ b/ChangeLog	Tue Feb 28 11:35:41 2012 -0500
@@ -1,3 +1,10 @@
+2012-02-28  Deepak Bhole <dbhole@redhat.com>
+
+	* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
+	(checkForMain): Also check manifest file of main jar.
+	(getMainClassName): New method. Looks in a jar manifest to see if there is
+	a Main-Class specified.
+
 2012-02-28  Deepak Bhole <dbhole@redhat.com>
 
 	* plugin/icedteanp/IcedTeaPluginRequestProcessor.cc
--- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Tue Feb 28 10:05:57 2012 -0500
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Tue Feb 28 11:35:41 2012 -0500
@@ -593,6 +593,39 @@
             mainClass = ad.getMainClass();
         } else
             return;
+
+        // The main class may be specified in the manifest
+
+        // Check main jar
+        if (mainClass == null) {
+            JARDesc mainJarDesc = file.getResources().getMainJAR();
+            mainClass = getMainClassName(mainJarDesc.getLocation());
+        }
+
+        // Check first jar
+        if (mainClass == null) {
+            JARDesc firstJarDesc = jars.get(0);
+            mainClass = getMainClassName(firstJarDesc.getLocation());
+        }
+
+        // Still not found? Iterate and set if only 1 was found
+        if (mainClass == null) {
+
+            for (JARDesc jarDesc: jars) {
+                String mainClassInThisJar = getMainClassName(jarDesc.getLocation());
+
+                if (mainClassInThisJar != null) {
+
+                    if (mainClass == null) { // first main class
+                        mainClass = mainClassInThisJar;
+                    } else { // There is more than one main class. Set to null and break.
+                        mainClass = null;
+                        break;
+                    }
+                }
+            }
+        }
+
         String desiredJarEntryName = mainClass + ".class";
 
         for (int i = 0; i < jars.size(); i++) {
@@ -630,6 +663,30 @@
     }
 
     /**
+     * Gets the name of the main method if specified in the manifest
+     *
+     * @param location The JAR location
+     * @return the main class name, null if there isn't one of if there was an error
+     */
+    private String getMainClassName(URL location) {
+
+        String mainClass = null;
+        File f = tracker.getCacheFile(location);
+
+        if( f != null) {
+            try {
+                JarFile mainJar = new JarFile(f);
+                mainClass = mainJar.getManifest().
+                        getMainAttributes().getValue("Main-Class");
+            } catch (IOException ioe) {
+                mainClass = null;
+            }
+        }
+
+        return mainClass;
+    }
+
+    /**
      * Is called by checkForMain() to check if the jar file is signed and if it
      * contains a signed JNLP file.
      *