changeset 771:9bf2d89511a4

JNLPClassLoader cleanup, avoid Enumerations and use strict typing. Iteration over Enumerations refactored to instead view Enumerations as Lists using Collections library. Type parameters added to some local variables and return types. For-loops refactored into for-each-loops.
author Andrew Azores <aazores@redhat.com>
date Wed, 13 Nov 2013 11:54:42 -0500
parents 2c9d20a900e6
children 719431dbd56c
files ChangeLog netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
diffstat 2 files changed, 59 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Nov 13 09:59:33 2013 -0500
+++ b/ChangeLog	Wed Nov 13 11:54:42 2013 -0500
@@ -1,3 +1,10 @@
+2013-11-13  Andrew Azores  <aazores@redhat.com>
+
+	* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java: add
+	parameterized type information to several return types and local
+	variables. Refactor for-loops and Enumeration iterations into
+	for-each-loops.
+
 2013-11-13  Andrew Azores  <aazores@redhat.com>
 
 	* netx/net/sourceforge/jnlp/util/BasicExceptionDialog.java: centers
--- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Wed Nov 13 09:59:33 2013 -0500
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Wed Nov 13 11:54:42 2013 -0500
@@ -512,7 +512,7 @@
      * Load the extensions specified in the JNLP file.
      */
     void initializeExtensions() {
-        ExtensionDesc[] ext = resources.getExtensions();
+        ExtensionDesc[] extDescs = resources.getExtensions();
 
         List<JNLPClassLoader> loaderList = new ArrayList<JNLPClassLoader>();
 
@@ -530,17 +530,15 @@
             }
         }
 
-        //if (ext != null) {
-        for (int i = 0; i < ext.length; i++) {
+        for (ExtensionDesc ext : extDescs) {
             try {
                 String uniqueKey = this.getJNLPFile().getUniqueKey();
-                JNLPClassLoader loader = getInstance(ext[i].getLocation(), uniqueKey, ext[i].getVersion(), updatePolicy, mainClass);
+                JNLPClassLoader loader = getInstance(ext.getLocation(), uniqueKey, ext.getVersion(), updatePolicy, mainClass);
                 loaderList.add(loader);
             } catch (Exception ex) {
                 ex.printStackTrace();
             }
         }
-        //}
 
         loaders = loaderList.toArray(new JNLPClassLoader[loaderList.size()]);
     }
@@ -552,13 +550,13 @@
         resourcePermissions = new ArrayList<Permission>();
 
         JARDesc jars[] = resources.getJARs();
-        for (int i = 0; i < jars.length; i++) {
-            Permission p = CacheUtil.getReadPermission(jars[i].getLocation(),
-                                                       jars[i].getVersion());
+        for (JARDesc jar : jars) {
+            Permission p = CacheUtil.getReadPermission(jar.getLocation(),
+                                                       jar.getVersion());
 
             if (JNLPRuntime.isDebug()) {
                 if (p == null)
-                    System.out.println("Unable to add permission for " + jars[i].getLocation());
+                    System.out.println("Unable to add permission for " + jar.getLocation());
                 else
                     System.out.println("Permission added: " + p.toString());
             }
@@ -646,20 +644,18 @@
         */
         List<JARDesc> initialJars = new ArrayList<JARDesc>();
 
-        for (int i = 0; i < jars.length; i++) {
+        for (JARDesc jar : jars) {
 
-            available.add(jars[i]);
-
-            if (jars[i].isEager())
-                initialJars.add(jars[i]); // regardless of part
+            available.add(jar);
 
-            tracker.addResource(jars[i].getLocation(),
-                                jars[i].getVersion(),
-                                file.getDownloadOptions(),
-                                jars[i].isCacheable() ? JNLPRuntime.getDefaultUpdatePolicy() : UpdatePolicy.FORCE
-                               );
+            if (jar.isEager())
+                initialJars.add(jar); // regardless of part
+
+            tracker.addResource(jar.getLocation(),
+                                jar.getVersion(), file.getDownloadOptions(),
+                                jar.isCacheable() ? JNLPRuntime.getDefaultUpdatePolicy() : UpdatePolicy.FORCE);
         }
-        
+
         //If there are no eager jars, initialize the first jar
         if(initialJars.size() == 0)
             initialJars.add(jars[0]);
@@ -869,27 +865,23 @@
 
         String desiredJarEntryName = mainClass + ".class";
 
-        for (int i = 0; i < jars.size(); i++) {
+        for (JARDesc jar : jars) {
 
             try {
                 File localFile = tracker
-                        .getCacheFile(jars.get(i).getLocation());
+                        .getCacheFile(jar.getLocation());
 
                 if (localFile == null) {
-                    System.err.println("JAR " + jars.get(i).getLocation() + " not found. Continuing.");
+                    System.err.println("JAR " + jar.getLocation() + " not found. Continuing.");
                     continue; // JAR not found. Keep going.
                 }
 
                 JarFile jarFile = new JarFile(localFile);
-                Enumeration<JarEntry> entries = jarFile.entries();
-                JarEntry je;
-
-                while (entries.hasMoreElements()) {
-                    je = entries.nextElement();
-                    String jeName = je.getName().replaceAll("/", ".");
+                for (JarEntry entry : Collections.list(jarFile.entries())) {
+                    String jeName = entry.getName().replaceAll("/", ".");
                     if (jeName.equals(desiredJarEntryName)) {
                         foundMainJar = true;
-                        verifySignedJNLP(jars.get(i), jarFile);
+                        verifySignedJNLP(jar, jarFile);
                         break;
                     }
                 }
@@ -988,11 +980,7 @@
             // calling jcv.verifyJars(desc, tracker) here should have no affect.
             if (jcv.isFullySigned()) {
 
-                Enumeration<JarEntry> entries = jarFile.entries();
-                JarEntry je;
-
-                while (entries.hasMoreElements()) {
-                    je = entries.nextElement();
+                for (JarEntry je : Collections.list(jarFile.entries())) {
                     String jeName = je.getName().toUpperCase();
 
                     if (jeName.equals(TEMPLATE) || jeName.equals(APPLICATION)) {
@@ -1185,20 +1173,19 @@
                     }
                 }
 
-                Enumeration<Permission> e = permissions.elements();
-                while (e.hasMoreElements()) {
-                    result.add(e.nextElement());
+                for (Permission perm : Collections.list(permissions.elements())) {
+                    result.add(perm);
                 }
             }
 
             // add in permission to read the cached JAR files
-            for (int i = 0; i < resourcePermissions.size(); i++) {
-                result.add(resourcePermissions.get(i));
+            for (Permission perm : resourcePermissions) {
+                result.add(perm);
             }
 
             // add in the permissions that the user granted.
-            for (int i = 0; i < runtimePermissions.size(); i++) {
-                result.add(runtimePermissions.get(i));
+            for (Permission perm : runtimePermissions) {
+                result.add(perm);
             }
 
             // Class from host X should be allowed to connect to host X
@@ -1225,12 +1212,9 @@
      * in the same part).
      */
     protected void fillInPartJars(List<JARDesc> jars) {
-        for (int i = 0; i < jars.size(); i++) {
-            String part = jars.get(i).getPart();
-
-            for (int a = 0; a < available.size(); a++) {
-                JARDesc jar = available.get(a);
-
+        for (JARDesc desc : jars) {
+            String part = desc.getPart();
+            for (JARDesc jar : available) {
                 if (part != null && part.equals(jar.getPart()))
                     if (!jars.contains(jar))
                         jars.add(jar);
@@ -1254,9 +1238,7 @@
                 // transfer the Jars
                 waitForJars(jars);
 
-                for (int i = 0; i < jars.size(); i++) {
-                    JARDesc jar = jars.get(i);
-
+                for (JARDesc jar : jars) {
                     available.remove(jar);
 
                     // add jar
@@ -1276,9 +1258,7 @@
                             // particularly when using The FileManager applet from Webmin.
 
                             JarFile jarFile = new JarFile(localFile);
-                            Enumeration<JarEntry> e = jarFile.entries();
-                            while (e.hasMoreElements()) {
-                                JarEntry je = e.nextElement();
+                            for (JarEntry je : Collections.list(jarFile.entries())) {
 
                                 // another jar in my jar? it is more likely than you think
                                 if (je.getName().endsWith(".jar")) {
@@ -1523,11 +1503,11 @@
      * Try to find the library path from another peer classloader.
      */
     protected String findLibraryExt(String lib) {
-        for (int i = 0; i < loaders.length; i++) {
+        for (JNLPClassLoader loader : loaders) {
             String result = null;
 
-            if (loaders[i] != this)
-                result = loaders[i].findLibrary(lib);
+            if (loader != this)
+                result = loader.findLibrary(lib);
 
             if (result != null)
                 return result;
@@ -1542,11 +1522,11 @@
      *
      * @param jars the jars
      */
-    private void waitForJars(List jars) {
+    private void waitForJars(List<JARDesc> jars) {
         URL urls[] = new URL[jars.size()];
 
         for (int i = 0; i < jars.size(); i++) {
-            JARDesc jar = (JARDesc) jars.get(i);
+            JARDesc jar = jars.get(i);
 
             urls[i] = jar.getLocation();
         }
@@ -1557,14 +1537,14 @@
     /**
      * Find the loaded class in this loader or any of its extension loaders.
      */
-    protected Class findLoadedClassAll(String name) {
-        for (int i = 0; i < loaders.length; i++) {
-            Class result = null;
+    protected Class<?> findLoadedClassAll(String name) {
+        for (JNLPClassLoader loader : loaders) {
+            Class<?> result = null;
 
-            if (loaders[i] == this) {
+            if (loader == this) {
                 result = JNLPClassLoader.super.findLoadedClass(name);
             } else {
-                result = loaders[i].findLoadedClassAll(name);
+                result = loader.findLoadedClassAll(name);
             }
 
             if (result != null)
@@ -1768,10 +1748,10 @@
      * Find the class in this loader or any of its extension loaders.
      */
     @Override
-    protected Class findClass(String name) throws ClassNotFoundException {
-        for (int i = 0; i < loaders.length; i++) {
+    protected Class<?> findClass(String name) throws ClassNotFoundException {
+        for (JNLPClassLoader loader : loaders) {
             try {
-                if (loaders[i] == this) {
+                if (loader == this) {
                     final String fName = name;
                     return AccessController.doPrivileged(
                             new PrivilegedExceptionAction<Class<?>>() {
@@ -1780,7 +1760,7 @@
                                 }
                             }, getAccessControlContextForClassLoading());
                 } else {
-                    return loaders[i].findClass(name);
+                    return loader.findClass(name);
                 }
             } catch (ClassNotFoundException ex) {
             } catch (ClassFormatError cfe) {
@@ -1804,7 +1784,7 @@
      * classloader and its extension classloaders until the resource
      * is found.
      */
-    private Class loadClassExt(String name) throws ClassNotFoundException {
+    private Class<?> loadClassExt(String name) throws ClassNotFoundException {
         // make recursive
         addAvailable();
 
@@ -1898,12 +1878,12 @@
         List<URL> resources = new ArrayList<URL>();
         Enumeration<URL> e = null;
 
-        for (int i = 0; i < loaders.length; i++) {
+        for (JNLPClassLoader loader : loaders) {
             // TODO check if this will blow up or not
             // if loaders[1].getResource() is called, wont it call getResource() on
             // the original caller? infinite recursion?
 
-            if (loaders[i] == this) {
+            if (loader == this) {
                 final String fName = name;
                 try {
                     e = AccessController.doPrivileged(
@@ -1915,7 +1895,7 @@
                 } catch (PrivilegedActionException pae) {
                 }
             } else {
-                e = loaders[i].findResources(name);
+                e = loader.findResources(name);
             }
 
             final Enumeration<URL> fURLEnum = e;