[jboss-cvs] JBossAS SVN: r72623 - in projects/jboss-cl/trunk: classloading/src/main/org/jboss/classloading/spi and 5 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 23 10:05:05 EDT 2008


Author: adrian at jboss.org
Date: 2008-04-23 10:05:04 -0400 (Wed, 23 Apr 2008)
New Revision: 72623

Added:
   projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/plugins/vfs/VFSResourceVisitor.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassFilter.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassVisitor.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceContext.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceFilter.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceVisitor.java
Modified:
   projects/jboss-cl/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java
   projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/spi/vfs/dependency/VFSClassLoaderPolicyModule.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/Module.java
   projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/policy/ClassLoaderPolicyModule.java
Log:
[JBCL-9] - First (untested) cut at adding a resource/class visitor to the classloading modules

Modified: projects/jboss-cl/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java
===================================================================
--- projects/jboss-cl/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java	2008-04-23 14:01:33 UTC (rev 72622)
+++ projects/jboss-cl/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -63,6 +63,20 @@
    }
 
    /**
+    * Convert a resource name to a class name
+    * 
+    * @param resourceName the resource name
+    * @return the class name or null if it is not a class
+    */
+   public static final String resourceNameToClassName(String resourceName)
+   {
+      if (resourceName.endsWith(".class") == false)
+         return null;
+      resourceName = resourceName.substring(0, resourceName.length()-6);
+      return resourceName.replace('/', '.');
+   }
+
+   /**
     * Convert a class's package name into a path
     * 
     * @param className the class name
@@ -138,6 +152,37 @@
    }
    
    /**
+    * Load bytes from a stream
+    * 
+    * @param is the input stream
+    * @return the bytes
+    * @throws IOException for any error
+    */
+   public static final byte[] loadBytes(final InputStream is) throws IOException
+   {
+      try
+      {
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         byte[] tmp = new byte[1024];
+         int read = 0;
+         while ( (read = is.read(tmp)) >= 0 )
+            baos.write(tmp, 0, read);
+         return baos.toByteArray();
+      }
+      finally
+      {
+         try
+         {
+            is.close();
+         }
+         catch (IOException e)
+         {
+            // pointless
+         }
+      }
+   }
+   
+   /**
     * Formats the class as a string
     * 
     * @param clazz the class

Modified: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/Module.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/Module.java	2008-04-23 14:01:33 UTC (rev 72622)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/Module.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -39,6 +39,8 @@
 import org.jboss.classloading.spi.metadata.ExportAll;
 import org.jboss.classloading.spi.metadata.ExportPackages;
 import org.jboss.classloading.spi.metadata.Requirement;
+import org.jboss.classloading.spi.visitor.ResourceFilter;
+import org.jboss.classloading.spi.visitor.ResourceVisitor;
 import org.jboss.dependency.spi.Controller;
 import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ControllerState;
@@ -276,6 +278,33 @@
    }
 
    /**
+    * Visit the resources in this module
+    * using the filter defined on the visitor
+    * 
+    * @param visitor the visitor
+    */
+   public void visit(ResourceVisitor visitor)
+   {
+      if (visitor == null)
+         throw new IllegalArgumentException("Null visitor");
+      visit(visitor, visitor.getFilter());
+   }
+
+   /**
+    * Visit the resources in this module
+    * using the given filter
+    * 
+    * @param visitor the visitor
+    * @param filter the filter
+    */
+   public void visit(ResourceVisitor visitor, ResourceFilter filter)
+   {
+      if (visitor == null)
+         throw new IllegalArgumentException("Null visitor");
+      throw new UnsupportedOperationException("The module " + getContextName() + " does not support filtering: " + getClass().getName());
+   }
+   
+   /**
     * Get the delegate loaders for this module
     * 
     * @return the delegates

Modified: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/policy/ClassLoaderPolicyModule.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/policy/ClassLoaderPolicyModule.java	2008-04-23 14:01:33 UTC (rev 72622)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/dependency/policy/ClassLoaderPolicyModule.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -53,6 +53,9 @@
    
    /** The classloader system we are registered with */
    private ClassLoaderSystem system;
+   
+   /** The classloader */
+   private ClassLoader classLoader;
 
    /**
     * Create a new ClassLoaderPolicyModule.
@@ -81,6 +84,7 @@
       String parentName = getDeterminedParentDomainName();
       ClassLoader result = system.registerClassLoaderPolicy(domainName, parentPolicy, parentName, getPolicy());
       this.system = system;
+      this.classLoader = result;
       return result;
    }
    
@@ -99,7 +103,9 @@
          throw new IllegalArgumentException("Null parent");
 
       Loader loader = new ClassLoaderToLoaderAdapter(parent);
-      return registerClassLoaderPolicy(system, loader);
+      ClassLoader result = registerClassLoaderPolicy(system, loader); 
+      this.classLoader = result;
+      return result;
    }
    
    /**
@@ -118,6 +124,7 @@
       ParentPolicy parentPolicy = getDeterminedParentPolicy();
       ClassLoader result = system.registerClassLoaderPolicy(domainName, parentPolicy, loader, getPolicy());
       this.system = system;
+      this.classLoader = result;
       return result;
    }
 
@@ -138,6 +145,7 @@
    {
       if (system != null && policy != null)
          system.unregisterClassLoaderPolicy(policy);
+      classLoader = null;
       system = null;
       policy = null;
    }
@@ -149,6 +157,16 @@
     */
    protected abstract ClassLoaderPolicy determinePolicy();
    
+   /**
+    * Get the classloader
+    * 
+    * @return the classloader
+    */
+   protected ClassLoader getClassLoader()
+   {
+      return classLoader;
+   }
+   
    @Override
    public DelegateLoader createLazyDelegateLoader(Domain domain, RequirementDependencyItem item)
    {

Added: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassFilter.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassFilter.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassFilter.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,40 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2007, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.classloading.spi.visitor;
+
+/**
+ * ClassFilter.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassFilter implements ResourceFilter
+{
+   /** The class filter instance */
+   public static final ClassFilter INSTANCE = new ClassFilter();
+   
+   public boolean accepts(ResourceContext resource)
+   {
+      return resource.isClass();
+   }
+
+}

Added: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassVisitor.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassVisitor.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ClassVisitor.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,36 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2007, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.classloading.spi.visitor;
+
+/**
+ * ClassVisitor.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class ClassVisitor implements ResourceVisitor
+{
+   public ResourceFilter getFilter()
+   {
+      return ClassFilter.INSTANCE;
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceContext.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceContext.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceContext.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,156 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2007, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.classloading.spi.visitor;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+
+/**
+ * ResourceContext.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ResourceContext
+{
+   /** The url of the resource */
+   private URL url;
+   
+   /** The classloader */
+   private ClassLoader classLoader;
+   
+   /** The resource name */
+   private String resourceName;
+   
+   /**
+    * Create a new ResourceContext.
+    * 
+    * @param url the url
+    * @param resourceName the resource name
+    * @param classLoader the classloader
+    */
+   public ResourceContext(URL url, String resourceName, ClassLoader classLoader)
+   {
+      if (url == null)
+         throw new IllegalArgumentException("Null url");
+      if (resourceName == null)
+         throw new IllegalArgumentException("Null resourceName");
+      if (classLoader == null)
+         throw new IllegalArgumentException("Null classloader");
+      this.url = url;
+      this.resourceName = resourceName;
+      this.classLoader = classLoader;
+   }
+
+   /**
+    * Get the url.
+    * 
+    * @return the url.
+    */
+   public URL getUrl()
+   {
+      return url;
+   }
+
+   /**
+    * Get the classLoader.
+    * 
+    * @return the classLoader.
+    */
+   public ClassLoader getClassLoader()
+   {
+      return classLoader;
+   }
+
+   /**
+    * Get the resourceName.
+    * 
+    * @return the resourceName.
+    */
+   public String getResourceName()
+   {
+      return resourceName;
+   }
+   
+   /**
+    * Get the class name
+    * 
+    * @return the class name or null if it is not a class
+    */
+   public String getClassName()
+   {
+      return ClassLoaderUtils.resourceNameToClassName(getResourceName());
+   }
+   
+   /**
+    * Whether the resource is a class
+    * 
+    * @return true when the resource name ends with .class
+    */
+   public boolean isClass()
+   {
+      return resourceName.endsWith(".class");
+   }
+   
+   /**
+    * Load a class
+    * 
+    * @return the class or null if it is not a class
+    */
+   public Class<?> loadClass()
+   {
+      String className = getClassName();
+      try
+      {
+         return classLoader.loadClass(className);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new RuntimeException("Unexpected error loading class: " + className, e);
+      }
+   }
+   
+   /**
+    * Get the input stream for the resource
+    * 
+    * @return the input stream
+    * @throws IOException for any error
+    */
+   public InputStream getInputStream() throws IOException
+   {
+      return url.openStream();
+   }
+   
+   /**
+    * Get the input stream for the resource
+    * 
+    * @return the input stream
+    * @throws IOException for any error
+    */
+   public byte[] getBytes() throws IOException
+   {
+      return ClassLoaderUtils.loadBytes(getInputStream());
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceFilter.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceFilter.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceFilter.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2007, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.classloading.spi.visitor;
+
+/**
+ * ResourceFilter.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public interface ResourceFilter
+{
+   /**
+    * Controls whether to visit a particular resource
+    * 
+    * @param resource the resource context
+    * @return true to visit the resource
+    */
+   boolean accepts(ResourceContext resource);
+}

Added: projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceVisitor.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceVisitor.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/org/jboss/classloading/spi/visitor/ResourceVisitor.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,45 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2007, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.classloading.spi.visitor;
+
+/**
+ * ResourceVisitor.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public interface ResourceVisitor
+{
+   /**
+    * Get the filter for this visitor
+    * 
+    * @return the filter or null if no filtering
+    */
+   ResourceFilter getFilter();
+   
+   /**
+    * Visit a resource
+    * 
+    * @param resource the resource being visited
+    */
+   void visit(ResourceContext resource);
+}

Added: projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/plugins/vfs/VFSResourceVisitor.java
===================================================================
--- projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/plugins/vfs/VFSResourceVisitor.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/plugins/vfs/VFSResourceVisitor.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -0,0 +1,188 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, 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.classloading.plugins.vfs;
+
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloading.spi.visitor.ResourceContext;
+import org.jboss.classloading.spi.visitor.ResourceFilter;
+import org.jboss.classloading.spi.visitor.ResourceVisitor;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VirtualFileVisitor;
+import org.jboss.virtual.VisitorAttributes;
+import org.jboss.virtual.plugins.vfs.helpers.AbstractVirtualFileFilterWithAttributes;
+
+/**
+ * Visits a virtual file system recursively
+ * to determine resources
+ * 
+ * @author <a href="adrian at jboss.org">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class VFSResourceVisitor extends AbstractVirtualFileFilterWithAttributes implements VirtualFileVisitor
+{
+   /** The roots */
+   private VirtualFile[] roots;
+   
+   /** The current root */
+   private VirtualFile root;
+   
+   /** The root */
+   private String rootPath;
+   
+   /** The root with slash*/
+   private String rootPathWithSlash;
+
+   /** The included packages */
+   private ClassFilter included;
+
+   /** The excluded packages */
+   private ClassFilter excluded;
+
+   /** The classLoader */
+   private ClassLoader classLoader;
+
+   /** The resource visitor */
+   private ResourceVisitor visitor;
+   
+   /** The resource filter */
+   private ResourceFilter filter;
+   
+   /**
+    * Visit the resources
+    * 
+    * @param roots the roots
+    * @param included the included packages
+    * @param excluded the excluded packages
+    * @param classLoader the classLoader
+    * @param visitor the visitor
+    * @param filter the filter
+    */
+   public static void visit(VirtualFile[] roots, ClassFilter included, ClassFilter excluded, ClassLoader classLoader, ResourceVisitor visitor, ResourceFilter filter)
+   {
+      VFSResourceVisitor vfsVisitor = new VFSResourceVisitor(roots, included, excluded, classLoader, visitor, filter);
+      for (VirtualFile root : roots)
+      {
+         try
+         {
+            vfsVisitor.setRoot(root);
+            root.visit(vfsVisitor);
+         }
+         catch (Exception e)
+         {
+            throw new Error("Error visiting " + root, e);
+         }
+      }
+   }
+
+   /**
+    * Create a new VFSResourceVisitor.
+    *
+    * @param roots the roots
+    * @param included the included packages
+    * @param excluded the excluded packages
+    * @param classLoader the classloader
+    * @param visitor the visitor
+    * @param filter the filter
+    */
+   VFSResourceVisitor(VirtualFile[] roots, ClassFilter included, ClassFilter excluded, ClassLoader classLoader, ResourceVisitor visitor, ResourceFilter filter)
+   {
+      this.roots = roots;
+      this.included = included;
+      this.excluded = excluded;
+      this.classLoader = classLoader;
+      this.visitor = visitor;
+      this.filter = filter;
+   }
+
+   /**
+    * Set the root
+    * 
+    * @param root the root
+    * @throws IllegalArgumentException for a null root
+    */
+   void setRoot(VirtualFile root)
+   {
+      if (root == null)
+         throw new IllegalArgumentException("Null root");
+      this.root = root;
+      rootPath = root.getPathName();
+      rootPathWithSlash = rootPath + "/";
+   }
+
+   public VisitorAttributes getAttributes()
+   {
+      VisitorAttributes attributes = new VisitorAttributes();
+      attributes.setIncludeRoot(false);
+      attributes.setRecurseFilter(this);
+      return attributes;
+   }
+   
+   public boolean accepts(VirtualFile file)
+   {
+      // This is our current root
+      if (file.equals(root))
+         return true;
+
+      // Some other root, it will be handled later
+      for (VirtualFile other : roots)
+      {
+         if (file.equals(other))
+            return false;
+      }
+      
+      // Ok
+      return true;
+   }
+   
+   public void visit(VirtualFile file)
+   {
+      try
+      {
+         // We don't want directories
+         if (file.isLeaf() == false)
+            return;
+
+         // Determine the resource name
+         String path = file.getPathName();
+         if (path.equals(rootPath))
+            path = "";
+         else if (path.startsWith(rootPathWithSlash))
+            path = path.substring(rootPathWithSlash.length());
+
+         // Check for inclusions/exclusions
+         if (included != null && included.matchesResourcePath(path) == false)
+            return;
+         if (excluded != null && excluded.matchesResourcePath(path))
+            return;
+         
+         ResourceContext resource = new ResourceContext(file.toURL(), path, classLoader);
+         
+         //Check the filter and visit
+         if (filter.accepts(resource))
+            visitor.visit(resource);
+      }
+      catch (Exception e)
+      {
+         throw new Error("Error visiting " + file, e);
+      }
+   }
+}
\ No newline at end of file

Modified: projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/spi/vfs/dependency/VFSClassLoaderPolicyModule.java
===================================================================
--- projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/spi/vfs/dependency/VFSClassLoaderPolicyModule.java	2008-04-23 14:01:33 UTC (rev 72622)
+++ projects/jboss-cl/trunk/classloading-vfs/src/main/org/jboss/classloading/spi/vfs/dependency/VFSClassLoaderPolicyModule.java	2008-04-23 14:05:04 UTC (rev 72623)
@@ -28,12 +28,15 @@
 
 import org.jboss.classloader.spi.filter.ClassFilter;
 import org.jboss.classloading.plugins.vfs.PackageVisitor;
+import org.jboss.classloading.plugins.vfs.VFSResourceVisitor;
 import org.jboss.classloading.spi.dependency.policy.ClassLoaderPolicyModule;
 import org.jboss.classloading.spi.metadata.Capability;
 import org.jboss.classloading.spi.metadata.ClassLoadingMetaDataFactory;
 import org.jboss.classloading.spi.metadata.ExportAll;
 import org.jboss.classloading.spi.vfs.metadata.VFSClassLoaderFactory;
 import org.jboss.classloading.spi.vfs.policy.VFSClassLoaderPolicy;
+import org.jboss.classloading.spi.visitor.ResourceFilter;
+import org.jboss.classloading.spi.visitor.ResourceVisitor;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
 import org.jboss.kernel.spi.dependency.KernelControllerContextAware;
 import org.jboss.virtual.VFS;
@@ -209,4 +212,20 @@
       super.reset();
       vfsRoots = null;
    }
+
+   @Override
+   public void visit(ResourceVisitor visitor, ResourceFilter filter)
+   {
+      ClassLoader classLoader = getClassLoader();
+      if (classLoader == null)
+         throw new IllegalStateException("ClassLoader has not been constructed for " + getContextName());
+
+      VirtualFile[] roots = determineVFSRoots();
+      if (roots != null)
+      {
+         ClassFilter included = getIncluded();
+         ClassFilter excluded = getExcluded();
+         VFSResourceVisitor.visit(roots, included, excluded, classLoader, visitor, filter);
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list