[jboss-cvs] JBossAS SVN: r88043 - projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 29 23:40:45 EDT 2009


Author: marius.bogoevici
Date: 2009-04-29 23:40:45 -0400 (Wed, 29 Apr 2009)
New Revision: 88043

Added:
   projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolvingHelper.java
   projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSServletContextResourcePatternResolver.java
Modified:
   projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolver.java
Log:
Refactoring VFS pattern resolution for reuse.

Modified: projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolver.java
===================================================================
--- projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolver.java	2009-04-30 03:40:18 UTC (rev 88042)
+++ projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolver.java	2009-04-30 03:40:45 UTC (rev 88043)
@@ -1,6 +1,6 @@
 /*
  * JBoss, Home of Professional Open Source.
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * Copyright 2009, 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.
  *
@@ -23,15 +23,9 @@
 
 import java.io.IOException;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Enumeration;
 
-import org.jboss.logging.Logger;
 import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
-import org.jboss.virtual.VirtualFileVisitor;
-import org.jboss.virtual.VisitorAttributes;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
@@ -39,11 +33,10 @@
  * VFS based ResourcePatternResolver.
  *
  * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
  */
 public class VFSResourcePatternResolver extends PathMatchingResourcePatternResolver
 {
-   private Logger log = Logger.getLogger(VFSResourcePatternResolver.class);
-
    public VFSResourcePatternResolver()
    {
       super(new VFSResourceLoader());
@@ -58,39 +51,10 @@
    {
       if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX))
          locationPattern = locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length());
-
       String rootDirPath = determineRootDir(locationPattern);
-      String subPattern = locationPattern.substring(rootDirPath.length());
-      if (rootDirPath.startsWith("/"))
-         rootDirPath = rootDirPath.substring(1);
-
-      List<Resource> resources = new ArrayList<Resource>();
-      Enumeration<URL> urls = getClassLoader().getResources(rootDirPath);
-      while(urls.hasMoreElements())
-         resources.addAll(getVFSResources(urls.nextElement(), subPattern));
-
-      return resources.toArray(new Resource[resources.size()]);
+      return VFSResourcePatternResolvingHelper.locateResources(locationPattern, rootDirPath, getClassLoader(), getPathMatcher());
    }
 
-   /**
-    * Get VFS resources.
-    *
-    * @param rootURL the root URL
-    * @param subPattern the sub pattern
-    * @return vfs resources list
-    * @throws IOException for any error
-    */
-   protected List<Resource> getVFSResources(URL rootURL, String subPattern) throws IOException
-   {
-      log.debug("Scanning url: " + rootURL + ", sub-pattern: " + subPattern);
-      VirtualFile root = VFS.getRoot(rootURL);
-      PatternVirtualFileVisitor visitor = new PatternVirtualFileVisitor(subPattern);
-      root.visit(visitor);
-      if (log.isTraceEnabled())
-         log.trace("Found resources: " + visitor);
-      return visitor.getResources();
-   }
-
    protected Resource convertClassLoaderURL(URL url)
    {
       try
@@ -104,54 +68,4 @@
       }
    }
 
-   /**
-    * Get visitor attributes.
-    * Allows for override, if necessary.
-    *
-    * @return  visitor attributes
-    */
-   protected VisitorAttributes getVisitorAttributes()
-   {
-      return VisitorAttributes.RECURSE_LEAVES_ONLY;
-   }
-
-   private class PatternVirtualFileVisitor implements VirtualFileVisitor
-   {
-      private String subPattern;
-      private List<Resource> resources = new ArrayList<Resource>();
-
-      private PatternVirtualFileVisitor(String subPattern)
-      {
-         this.subPattern = subPattern;
-      }
-
-      public VisitorAttributes getAttributes()
-      {
-         return getVisitorAttributes();
-      }
-
-      public void visit(VirtualFile vf)
-      {
-         if (getPathMatcher().match(subPattern, vf.getPathName()))
-            resources.add(new VFSResource(vf));
-      }
-
-      public List<Resource> getResources()
-      {
-         return resources;
-      }
-
-      public int size()
-      {
-         return resources.size();
-      }
-
-      public String toString()
-      {
-         StringBuffer buffer = new StringBuffer();
-         buffer.append("sub-pattern: ").append(subPattern);
-         buffer.append(", resources: ").append(resources);
-         return buffer.toString();
-      }
-   }
 }
\ No newline at end of file

Added: projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolvingHelper.java
===================================================================
--- projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolvingHelper.java	                        (rev 0)
+++ projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSResourcePatternResolvingHelper.java	2009-04-30 03:40:45 UTC (rev 88043)
@@ -0,0 +1,126 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.spring.vfs;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import org.jboss.logging.Logger;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VirtualFileVisitor;
+import org.jboss.virtual.VisitorAttributes;
+import org.springframework.core.io.Resource;
+import org.springframework.util.PathMatcher;
+
+
+/**
+ * Helper class implementing class-path scanning pattern resolution strategy for
+ * the VFS filesystem.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class VFSResourcePatternResolvingHelper
+{
+
+    private static Logger log = Logger.getLogger(VFSResourcePatternResolvingHelper.class);
+
+    public static Resource[] locateResources(String locationPattern, String rootDirPath, ClassLoader classLoader, PathMatcher pathMatcher)
+            throws IOException
+    {
+        String subPattern = locationPattern.substring(rootDirPath.length());
+        if (rootDirPath.startsWith("/"))
+            rootDirPath = rootDirPath.substring(1);
+
+        List<Resource> resources = new ArrayList<Resource>();
+        Enumeration<URL> urls = classLoader.getResources(rootDirPath);
+        while (urls.hasMoreElements())
+            resources.addAll(getVFSResources(urls.nextElement(), subPattern, pathMatcher));
+
+        return resources.toArray(new Resource[resources.size()]);
+    }
+
+    /**
+     * Get VFS resources.
+     *
+     * @param rootURL    the root URL
+     * @param subPattern the sub pattern
+     * @return vfs resources list
+     * @throws java.io.IOException for any error
+     */
+    public static List<Resource> getVFSResources(URL rootURL, String subPattern, PathMatcher pathMatcher) throws IOException
+    {
+        log.debug("Scanning url: " + rootURL + ", sub-pattern: " + subPattern);
+        VirtualFile root = VFS.getRoot(rootURL);
+        PatternVirtualFileVisitor visitor = new PatternVirtualFileVisitor(subPattern, pathMatcher);
+        root.visit(visitor);
+        if (log.isTraceEnabled())
+            log.trace("Found resources: " + visitor);
+        return visitor.getResources();
+    }
+
+    protected static class PatternVirtualFileVisitor implements VirtualFileVisitor
+    {
+        private final String subPattern;
+        private final List<Resource> resources = new ArrayList<Resource>();
+        private final PathMatcher pathMatcher;
+
+        private PatternVirtualFileVisitor(String subPattern, PathMatcher pathMatcher)
+        {
+            this.subPattern = subPattern;
+            this.pathMatcher = pathMatcher;
+        }
+
+        public VisitorAttributes getAttributes()
+        {
+            return VisitorAttributes.RECURSE_LEAVES_ONLY;
+        }
+
+        public void visit(VirtualFile vf)
+        {
+            if (pathMatcher.match(subPattern, vf.getPathName()))
+                resources.add(new VFSResource(vf));
+        }
+
+        public List<Resource> getResources()
+        {
+            return resources;
+        }
+
+        public int size()
+        {
+            return resources.size();
+        }
+
+        public String toString()
+        {
+            StringBuffer buffer = new StringBuffer();
+            buffer.append("sub-pattern: ").append(subPattern);
+            buffer.append(", resources: ").append(resources);
+            return buffer.toString();
+        }
+    }
+}

Added: projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSServletContextResourcePatternResolver.java
===================================================================
--- projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSServletContextResourcePatternResolver.java	                        (rev 0)
+++ projects/spring-int/trunk/vfs/src/main/java/org/jboss/spring/vfs/VFSServletContextResourcePatternResolver.java	2009-04-30 03:40:45 UTC (rev 88043)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.spring.vfs;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.web.context.support.ServletContextResourcePatternResolver;
+
+/**
+ *  VFS based ServletContextResourcePatternResolver
+ *
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class VFSServletContextResourcePatternResolver extends ServletContextResourcePatternResolver
+{
+    public VFSServletContextResourcePatternResolver(ResourceLoader resourceLoader)
+    {
+        super(resourceLoader);
+    }
+
+   protected Resource[] findPathMatchingResources(String locationPattern) throws IOException
+   {
+      if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX))
+      {
+          locationPattern = locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length());
+          String rootDirPath = determineRootDir(locationPattern);
+          return VFSResourcePatternResolvingHelper.locateResources(locationPattern, rootDirPath, getClassLoader(), getPathMatcher());
+      }
+      else
+      {
+          return super.findPathMatchingResources(locationPattern);
+      }
+   }
+
+   protected Resource convertClassLoaderURL(URL url)
+   {
+      try
+      {
+         VirtualFile file = VFS.getRoot(url);
+         return new VFSResource(file);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list