[jboss-cvs] JBossAS SVN: r102927 - in projects/scanning/trunk: scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 24 18:23:56 EDT 2010


Author: alesj
Date: 2010-03-24 18:23:56 -0400 (Wed, 24 Mar 2010)
New Revision: 102927

Added:
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/CachingResourceOwnerFinder.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentResourceOwnerFinderFactory.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinder.java
Modified:
   projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PackageVisitor.java
   projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternVisitor.java
   projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java
   projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/SingleAnnotationVisitor.java
Log:
Add the notion of caching the url owner lookup.

Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PackageVisitor.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PackageVisitor.java	2010-03-24 22:07:57 UTC (rev 102926)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PackageVisitor.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -53,7 +53,7 @@
 
    public void visit(ResourceContext resource)
    {
-      URL ownerURL = ScannerImpl.getOwnerURL(resource);
+      URL ownerURL = scanner.getOwnerURL(resource);
       String className = resource.getClassName();
       scanner.addPackage(ownerURL, className.substring(0, className.lastIndexOf(".")));
    }

Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternVisitor.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternVisitor.java	2010-03-24 22:07:57 UTC (rev 102926)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/PatternVisitor.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -56,7 +56,7 @@
 
    public void visit(ResourceContext resource)
    {
-      URL ownerURL = ScannerImpl.getOwnerURL(resource);
+      URL ownerURL = scanner.getOwnerURL(resource);
       scanner.addFile(ownerURL, filter.getPattern(), new LazyNamedInputStream(resource));      
    }
 }
\ No newline at end of file

Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java	2010-03-24 22:07:57 UTC (rev 102926)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/ScannerImpl.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -22,7 +22,6 @@
 
 package org.jboss.scanning.hibernate;
 
-import java.io.File;
 import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.net.URISyntaxException;
@@ -36,6 +35,8 @@
 import org.jboss.classloading.spi.visitor.ResourceVisitor;
 import org.jboss.deployers.spi.deployer.helpers.AttachmentLocator;
 import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.scanning.plugins.helpers.DeploymentResourceOwnerFinderFactory;
+import org.jboss.scanning.plugins.helpers.ResourceOwnerFinder;
 import org.jboss.scanning.plugins.helpers.WeakClassLoaderHolder;
 import org.jboss.scanning.spi.ScanningHandle;
 import org.jboss.vfs.VFS;
@@ -63,6 +64,8 @@
 
    /** The deployment unit */
    private DeploymentUnit unit;
+   /** The resource finder */
+   private ResourceOwnerFinder finder;
    /** Do we allow query invocation search */
    private boolean allowQueryInvocationSearch; // by default false, as we expect things to be pre-indexed
    /** Do we cache new results */
@@ -79,12 +82,13 @@
    {
       super(check(unit).getClassLoader());
       this.unit = unit;
+      this.finder = DeploymentResourceOwnerFinderFactory.getFinder(unit);
    }
 
    /**
     * Cleanup.
     */
-   void cleanup()
+   protected void cleanup()
    {
       packages.clear();
       pckgCache.clear();
@@ -98,19 +102,9 @@
     * @param resource the resource context
     * @return the owner/jar url
     */
-   static URL getOwnerURL(ResourceContext resource)
+   URL getOwnerURL(ResourceContext resource)
    {
-      try
-      {
-         File file = new File(resource.getUrl().toURI());
-         while(file.exists() == false)
-            file = file.getParentFile();
-         return file.toURI().toURL(); // I guess this is the jar url?
-      }
-      catch (Exception e)
-      {
-         throw new RuntimeException(e);
-      }
+      return finder.findOwnerURL(resource);
    }
 
    Package loadPackage(String pckg)

Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/SingleAnnotationVisitor.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/SingleAnnotationVisitor.java	2010-03-24 22:07:57 UTC (rev 102926)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/hibernate/SingleAnnotationVisitor.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -66,7 +66,7 @@
       ClassInfo classInfo = getClassInfo(resource);
       if (classInfo.isAnnotationPresent(annotation))
       {
-         URL ownerURL = ScannerImpl.getOwnerURL(resource);
+         URL ownerURL = scanner.getOwnerURL(resource);
          scanner.addClass(ownerURL, annotation, classInfo.getName());         
       }
    }

Copied: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/CachingResourceOwnerFinder.java (from rev 102640, projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/WeakClassLoaderHolder.java)
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/CachingResourceOwnerFinder.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/CachingResourceOwnerFinder.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -0,0 +1,102 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.scanning.plugins.helpers;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jboss.classloading.spi.visitor.ResourceContext;
+
+/**
+ * Cache url owner lookup.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class CachingResourceOwnerFinder implements ResourceOwnerFinder
+{
+   /** The file to url cache */
+   private Map<File, URL> cache = new HashMap<File, URL>();
+
+   /**
+    * Cleanup.
+    */
+   public void cleanup()
+   {
+      cache.clear();
+   }
+
+   /**
+    * Get owner/jar url -- w/o actually loading class/resource.
+    *
+    * @param resource the resource context
+    * @return the owner/jar url
+    */
+   public URL findOwnerURL(ResourceContext resource)
+   {
+      try
+      {
+         URL url = resource.getUrl();
+         File file = new File(url.toURI());
+         if (file.exists()) // does the file exist
+            return url;
+         url = cache.get(file); // is the file cached
+         if (url != null)
+            return url;
+
+         List<File> files = new ArrayList<File>();
+         files.add(file); // add file, so we cache its mapping
+         // move to parent, so we don't duplicate previous lookups
+         file = file.getParentFile();
+
+         while(file.exists() == false)
+         {
+            url = cache.get(file);
+            if (url != null)
+               break;
+
+            files.add(file); // not found, add to cache
+            file = file.getParentFile();
+         }
+
+         // not found in cache yet
+         if (url == null)
+         {
+            url = file.toURI().toURL(); // I guess this is the jar url?
+            files.add(file); // so we map/cache the current file as well
+         }
+
+         for (File f : files)
+            cache.put(f, url);
+
+         return url;
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentResourceOwnerFinderFactory.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentResourceOwnerFinderFactory.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentResourceOwnerFinderFactory.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -0,0 +1,49 @@
+package org.jboss.scanning.plugins.helpers;
+
+import org.jboss.classloading.spi.dependency.Module;
+import org.jboss.deployers.spi.deployer.helpers.AttachmentLocator;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * Find the resource owner for deployment.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class DeploymentResourceOwnerFinderFactory
+{
+   /**
+    * Get finder.
+    *
+    * @param unit the depoyment unit
+    * @return the finder
+    */
+   public static ResourceOwnerFinder getFinder(DeploymentUnit unit)
+   {
+      ResourceOwnerFinder rof = AttachmentLocator.searchAncestors(unit, ResourceOwnerFinder.class);
+      if (rof == null)
+         rof = new CachingResourceOwnerFinder();
+
+      DeploymentUnit moduleUnit = unit;
+      while(moduleUnit != null && moduleUnit.isAttachmentPresent(Module.class) == false)
+         moduleUnit = moduleUnit.getParent();
+
+      if (moduleUnit == null)
+         throw new IllegalArgumentException("No module in unit: " + unit);
+
+      // group rof per module
+      moduleUnit.addAttachment(ResourceOwnerFinder.class, rof);
+
+      return rof;
+   }
+
+   /**
+    * Cleanup the finder.
+    *
+    * @param finder the finder to cleanup
+    */
+   public static void cleanup(ResourceOwnerFinder finder)
+   {
+      if (finder instanceof CachingResourceOwnerFinder)
+         CachingResourceOwnerFinder.class.cast(finder).cleanup();
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinder.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinder.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinder.java	2010-03-24 22:23:56 UTC (rev 102927)
@@ -0,0 +1,21 @@
+package org.jboss.scanning.plugins.helpers;
+
+import java.net.URL;
+
+import org.jboss.classloading.spi.visitor.ResourceContext;
+
+/**
+ * Find the resource owner.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface ResourceOwnerFinder
+{
+   /**
+    * Find the resource owner.
+    *
+    * @param resource the resource
+    * @return resource owner url
+    */
+   URL findOwnerURL(ResourceContext resource);
+}




More information about the jboss-cvs-commits mailing list