[jboss-cvs] JBossAS SVN: r86898 - in projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded: lang/vfs and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 7 04:24:28 EDT 2009


Author: wolfc
Date: 2009-04-07 04:24:28 -0400 (Tue, 07 Apr 2009)
New Revision: 86898

Modified:
   projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/jarboot/JarBoot.java
   projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/lang/vfs/VFSClassLoader.java
Log:
RELOADED-7: refactored VFSClassLoader into a proper VFSClassLoader

Modified: projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/jarboot/JarBoot.java
===================================================================
--- projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/jarboot/JarBoot.java	2009-04-07 08:16:42 UTC (rev 86897)
+++ projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/jarboot/JarBoot.java	2009-04-07 08:24:28 UTC (rev 86898)
@@ -28,7 +28,6 @@
 import java.lang.reflect.Method;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
@@ -151,7 +150,13 @@
          }
       }
       
-      URLClassLoader nextClassLoader = new VFSClassLoader(urls.toArray(new URL[0]), null);
+      VirtualFile files[] = new VirtualFile[urls.size()];
+      for(int i = 0; i < files.length; i++)
+      {
+         files[i] = VFS.getRoot(urls.get(i));
+      }
+      
+      ClassLoader nextClassLoader = new VFSClassLoader(files, null);
       Class<?> mainClass;
       try
       {

Modified: projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/lang/vfs/VFSClassLoader.java
===================================================================
--- projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/lang/vfs/VFSClassLoader.java	2009-04-07 08:16:42 UTC (rev 86897)
+++ projects/reloaded/trunk/embedded/src/main/java/org/jboss/reloaded/embedded/lang/vfs/VFSClassLoader.java	2009-04-07 08:24:28 UTC (rev 86898)
@@ -24,17 +24,19 @@
 import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.security.CodeSigner;
 import java.security.CodeSource;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 import java.util.jar.Attributes.Name;
 
-import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
 
 /**
@@ -43,23 +45,18 @@
  */
 public class VFSClassLoader extends URLClassLoader
 {
-   private Map<URL, VirtualFile> cache = new HashMap<URL, VirtualFile>();
+   private VirtualFile files[];
    
-   public VFSClassLoader(URL urls[], ClassLoader parent) throws IOException
+   public VFSClassLoader(VirtualFile files[], ClassLoader parent) throws IOException
    {
-      super(urls, parent);
-
-      for(URL url : urls)
-      {
-         String protocol = url.getProtocol();
-         if(protocol.startsWith("vfs"))
-         {
-            cache.put(url, VFS.getRoot(url));
-         }
-      }
+      super(new URL[0], parent);
+      
+      this.files = files;
+      
+      // TODO: read manifests?
    }
    
-   protected Class<?> defineClass(String name, URL url, VirtualFile resource) throws IOException
+   protected Class<?> defineClass(String name, URL url, Manifest man, VirtualFile resource) throws IOException
    {
       int i = name.lastIndexOf('.');
       if(i != -1)
@@ -67,8 +64,6 @@
          String pkgname = name.substring(0, i);
          // Check if package already loaded.
          Package pkg = getPackage(pkgname);
-         //Manifest man = res.getManifest();
-         Manifest man = null;
          if (pkg != null)
          {
             // Package found, so check package sealing.
@@ -116,43 +111,120 @@
    protected Class<?> findClass(String name) throws ClassNotFoundException
    {
       String path = name.replace('.', '/').concat(".class");
-      URL urls[] = getURLs();
-      for(URL url : urls)
+      for(VirtualFile file : files)
       {
-         String protocol = url.getProtocol();
-         if(protocol.startsWith("vfs"))
+         try
          {
-            try
+            /*
+            URL resourceURL = new URL(url, path);
+            byte b[] = readResource(resourceURL);
+            // TODO: a lot
+            CodeSigner signers[] = null;
+            CodeSource cs = new CodeSource(url, signers);
+            int off = 0;
+            int len = b.length;
+            return defineClass(name, b, off, len, cs);
+            */
+            //VirtualFile file = VFS.getRoot(url);
+            VirtualFile resource = file.getChild(path);
+            if(resource != null)
             {
-               /*
-               URL resourceURL = new URL(url, path);
-               byte b[] = readResource(resourceURL);
-               // TODO: a lot
-               CodeSigner signers[] = null;
-               CodeSource cs = new CodeSource(url, signers);
-               int off = 0;
-               int len = b.length;
-               return defineClass(name, b, off, len, cs);
-               */
-               //VirtualFile file = VFS.getRoot(url);
-               VirtualFile root = cache.get(url);
-               assert root != null : "cache has failed for " + url;
-               VirtualFile resource = root.getChild(path);
-               if(resource != null)
-               {
-                  return defineClass(name, url, resource);
-               }
+               Manifest manifest = getManifest(file);
+               return defineClass(name, file.toURL(), manifest, resource);
             }
-            catch(IOException e1)
-            {
-               // ignore
-               //e1.printStackTrace();
-            }
          }
+         catch(IOException e1)
+         {
+            // ignore
+            //e1.printStackTrace();
+            // TODO: really!?
+         }
+         catch(URISyntaxException e)
+         {
+            // ignore
+            // TODO: really!?
+         }
       }
       return super.findClass(name);
    }
    
+   @Override
+   public URL findResource(String name)
+   {
+      for(VirtualFile file : files)
+      {
+         try
+         {
+            VirtualFile resource = file.getChild(name);
+            if(resource != null)
+               return resource.toURL();
+         }
+         catch(IOException e)
+         {
+            // ignore
+            //e1.printStackTrace();
+            // TODO: really!?
+         }
+         catch(URISyntaxException e)
+         {
+            // ignore
+            // TODO: really!?
+         }
+      }
+      return null;
+   }
+   
+   @Override
+   public Enumeration<URL> findResources(String name) throws IOException
+   {
+      List<URL> urls = new ArrayList<URL>();
+      for(VirtualFile file : files)
+      {
+         try
+         {
+            VirtualFile resource = file.getChild(name);
+            if(resource != null)
+               urls.add(resource.toURL());
+         }
+         catch(IOException e)
+         {
+            // ignore
+            //e1.printStackTrace();
+            // TODO: really!?
+         }
+         catch(URISyntaxException e)
+         {
+            // ignore
+            // TODO: really!?
+         }
+      }
+      return Collections.enumeration(urls);
+   }
+   
+   protected Manifest getManifest(VirtualFile jar) throws IOException
+   {
+      VirtualFile manifest = jar.getChild("META-INF/MANIFEST.MF");
+      if(manifest == null)
+         return null;
+      
+      BufferedInputStream in = new BufferedInputStream(manifest.openStream());
+      try
+      {
+         Manifest man = new Manifest(in);
+         return man;
+      }
+      finally
+      {
+         in.close();
+      }
+   }
+   
+   @Override
+   public URL[] getURLs()
+   {
+      throw new RuntimeException("NYI");
+   }
+   
    /**
     * Returns true if the specified package name is sealed according to the
     * given manifest.




More information about the jboss-cvs-commits mailing list