[jboss-cvs] JBossAS SVN: r112466 - projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Nov 22 10:14:24 EST 2011


Author: alesj
Date: 2011-11-22 10:14:24 -0500 (Tue, 22 Nov 2011)
New Revision: 112466

Modified:
   projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java
   projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
   projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/ClassLoaderInformation.java
Log:
[JBCL-181]; cache getResources lookup.


Modified: projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java
===================================================================
--- projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java	2011-11-21 20:46:52 UTC (rev 112465)
+++ projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java	2011-11-22 15:14:24 UTC (rev 112466)
@@ -34,7 +34,9 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -42,7 +44,6 @@
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
-
 import javax.management.ObjectName;
 
 import org.jboss.classloader.plugins.ClassLoaderUtils;
@@ -62,6 +63,7 @@
  * BaseClassLoader.
  *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class BaseClassLoader extends SecureClassLoader implements BaseClassLoaderMBean, RealClassLoader
@@ -84,6 +86,9 @@
    /** Our resource cache */
    private Map<String, URL> resourceCache;
 
+   /** Our resources cache */
+   private Map<String, Set<URL>> resourcesCache;
+
    /** Our black list */
    private Set<String> blackList;
 
@@ -107,7 +112,10 @@
       loader = new DelegateLoader(policy);
 
       if (basePolicy.isCacheable())
+      {
          resourceCache = new ConcurrentHashMap<String, URL>();
+         resourcesCache = new ConcurrentHashMap<String, Set<URL>>();
+      }
 
       if (basePolicy.isBlackListable())
          blackList = new ConcurrentSet<String>();
@@ -211,18 +219,35 @@
 
    public Set<String> listLoadedResourceNames()
    {
-      if (resourceCache == null)
-         return Collections.emptySet();
-      return new HashSet<String>(resourceCache.keySet());
+      Set<String> names = new HashSet<String>();
+
+      Map<String, URL> resourceCache = this.resourceCache;
+      if (resourceCache != null)
+         names.addAll(resourceCache.keySet());
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+         names.addAll(resourcesCache.keySet());
+
+      return names;
    }
 
    // FindBugs: The Set doesn't use equals/hashCode
    public Set<URL> listLoadedResources()
    {
-      if (resourceCache == null)
-         return Collections.emptySet();
       Set<URL> result = new TreeSet<URL>(ClassLoaderUtils.URLComparator.INSTANCE);
-      result.addAll(resourceCache.values());
+
+      Map<String, URL> resourceCache = this.resourceCache;
+      if (resourceCache != null)
+         result.addAll(resourceCache.values());
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+      {
+         for (Set<URL> urls : resourcesCache.values())
+            result.addAll(urls);
+      }
+
       return result;
    }
 
@@ -680,6 +705,18 @@
          }
       }
 
+      // Do we already know the answer?
+      if (resourcesCache != null)
+      {
+         Set<URL> urls = resourcesCache.get(name);
+         if (urls != null)
+         {
+            if (trace)
+               log.trace(this + " got resource from resources cache " + name);
+            return (urls.isEmpty()) ? null : urls.iterator().next();
+         }
+      }
+
       // Is this resource blacklisted?
       if (blackList != null && blackList.contains(name))
       {
@@ -745,6 +782,26 @@
       if (trace)
          log.trace(this + " get resources locally " + name);
 
+      if (resourcesCache != null)
+      {
+         Set<URL> cached = resourcesCache.get(name);
+         if (cached != null)
+         {
+            if (trace)
+               log.trace(this + " got resources from cache " + name);
+            urls.addAll(cached);
+            return;
+         }
+      }
+
+      // Is this resource blacklisted?
+      if (blackList != null && blackList.contains(name))
+      {
+         if (trace)
+            log.trace(this + " resource is blacklisted " + name);
+         return;
+      }
+
       // Look for the resources
       try
       {
@@ -752,7 +809,23 @@
          {
             public Object run() throws Exception
             {
-               policy.getResources(name, urls);
+               final Set<URL> tmp = new LinkedHashSet<URL>();
+               policy.getResources(name, tmp);
+
+               if (tmp.isEmpty())
+               {
+                  if (resourcesCache != null)
+                     resourcesCache.put(name, Collections.<URL>emptySet());
+                  if (blackList != null)
+                     blackList.add(name);
+               }
+               else
+               {
+                  urls.addAll(tmp);
+                  if (resourcesCache != null)
+                     resourcesCache.put(name, tmp);
+               }
+
                return null;
             }
          }, policy.getAccessControlContext());
@@ -913,9 +986,14 @@
    public int getResourceCacheSize()
    {
       Map<String, URL> resourceCache = this.resourceCache;
-      if (resourceCache == null)
-         return 0;
-      return resourceCache.size();
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+
+      int size = 0;
+      if (resourceCache != null)
+         size += resourceCache.size();
+      if (resourcesCache != null)
+         size += resourcesCache.size();
+      return size;
    }
 
    public Set<String> listResourceBlackList()
@@ -928,10 +1006,25 @@
 
    public Map<String, URL> listResourceCache()
    {
+      Map<String, URL> map = new HashMap<String, URL>();
+
       Map<String, URL> resourceCache = this.resourceCache;
-      if (resourceCache == null)
-         return Collections.emptyMap();
-      return Collections.unmodifiableMap(resourceCache);
+      if (resourceCache != null)
+         map.putAll(resourceCache);
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+      {
+         for (Map.Entry<String, Set<URL>> entry : resourcesCache.entrySet())
+         {
+            if (map.containsKey(entry.getKey()) == false && entry.getValue().isEmpty() == false)
+            {
+               map.put(entry.getKey(), entry.getValue().iterator().next());
+            }
+         }
+      }
+
+      return map;
    }
 
    public void clearBlackList()
@@ -980,6 +1073,8 @@
       loadedClasses.clear();
       if (resourceCache != null)
          resourceCache.clear();
+      if (resourcesCache != null)
+         resourcesCache.clear();
       if (blackList != null)
          blackList.clear();
    }

Modified: projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
===================================================================
--- projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java	2011-11-21 20:46:52 UTC (rev 112465)
+++ projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java	2011-11-22 15:14:24 UTC (rev 112466)
@@ -28,10 +28,11 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
-import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
@@ -79,6 +80,9 @@
    /** The global resource cache */
    private Map<String, URL> globalResourceCache = new ConcurrentHashMap<String, URL>();
    
+   /** The global resource cache */
+   private Map<String, Set<URL>> globalResourcesCache = new ConcurrentHashMap<String, Set<URL>>();
+
    /** The global resource black list */
    private Set<String> globalResourceBlackList = new ConcurrentSet<String>();
    
@@ -104,6 +108,7 @@
       globalClassCache.clear();
       globalClassBlackList.clear();
       globalResourceCache.clear();
+      globalResourcesCache.clear();
       globalResourceBlackList.clear();
 
       for (ClassLoaderInformation info : classLoaders)
@@ -129,7 +134,12 @@
    {
       return globalResourceCache.size();
    }
-   
+
+   public int getResourcesCacheSize()
+   {
+      return globalResourcesCache.size();
+   }
+
    public Set<String> listClassBlackList()
    {
       return Collections.unmodifiableSet(globalClassBlackList);
@@ -150,7 +160,14 @@
 
    public Map<String, URL> listResourceCache()
    {
-      return Collections.unmodifiableMap(globalResourceCache);
+      Map<String, URL> map = new HashMap<String, URL>();
+      map.putAll(globalResourceCache);
+      for (Map.Entry<String, Set<URL>> e : globalResourcesCache.entrySet())
+      {
+         if (map.containsKey(e.getKey()) == false && e.getValue().isEmpty() == false)
+            map.put(e.getKey(), e.getValue().iterator().next());
+      }
+      return map;
    }
 
    /**
@@ -749,14 +766,24 @@
       {
          if (trace)
             log.trace(this + " got resource from cache " + name);
+         return result;
       }
       
+      Set<URL> results = globalResourcesCache.get(name);
+      if (results != null)
+      {
+         if (trace)
+            log.trace(this + " got resource from resources cache " + name);
+         return (results.isEmpty()) ? null : results.iterator().next();
+      }
+
       if (globalResourceBlackList.contains(name))
       {
          if (trace)
             log.trace(this + " resource is black listed, not looking at exports " + name);
          return null;
       }
+
       boolean canCache = true;
       boolean canBlackList = true;
       
@@ -804,18 +831,57 @@
    // FindBugs: The Set doesn't use equals/hashCode
    void getResourcesFromExports(BaseClassLoader classLoader, String name, Set<URL> urls, boolean trace) throws IOException
    {
+      Set<URL> results = globalResourcesCache.get(name);
+      if (results != null)
+      {
+         if (trace)
+            log.trace(this + " got resources from cache " + name);
+         urls.addAll(results);
+         return;
+      }
+
+      if (globalResourceBlackList.contains(name))
+         return;
+
       String packageName = ClassLoaderUtils.getResourcePackageName(name);
       List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
       if (trace)
          log.trace(this + " trying to get resources " + name + " from all exports " + list);
+
+      boolean canCache = true;
+      boolean canBlackList = true;
+
+      Set<URL> tmp = new LinkedHashSet<URL>();
       if (list != null && list.isEmpty() == false)
       {
          for (ClassLoaderInformation info : list)
          {
             BaseDelegateLoader loader = info.getExported();
-            loader.getResources(name, urls);
+
+            // See whether the policies allow caching/blacklisting
+            BaseClassLoaderPolicy loaderPolicy = loader.getPolicy();
+            if (loaderPolicy == null || loaderPolicy.isCacheable() == false)
+               canCache = false;
+            if (loaderPolicy == null || loaderPolicy.isBlackListable() == false)
+               canBlackList = false;
+
+            loader.getResources(name, tmp);
          }
       }
+
+      if (tmp.isEmpty())
+      {
+         if (canCache)
+            globalResourcesCache.put(name, Collections.<URL>emptySet());
+         if (canBlackList)
+            globalResourceBlackList.add(name);
+      }
+      else
+      {
+         urls.addAll(tmp);
+         if (canCache)
+            globalResourcesCache.put(name, tmp);
+      }
    }
    
    /**
@@ -979,13 +1045,45 @@
       if (delegates == null || delegates.isEmpty())
       {
          if (trace)
-            log.trace(this + " not getting resource " + name + " from imports it has no delegates");
+            log.trace(this + " not getting resources " + name + " from imports it has no delegates");
          return;
       }
+
+      Set<URL> cached = info.getCachedResources(name);
+      if (cached != null)
+      {
+         if (trace)
+            log.trace(this + " found resources in import cache " + name);
+
+         urls.addAll(cached);
+         return;
+      }
+
+      if (info.isBlackListedResource(name))
+      {
+         if (trace)
+            log.trace(this + " resource is black listed in imports " + name);
+         return;
+      }
+
       if (trace)
          log.trace(this + " trying to get resources " + name + " from imports " + delegates + " for " + info.getClassLoader());
+
+      cached = new LinkedHashSet<URL>();
       for (DelegateLoader delegate : delegates)
-         delegate.getResources(name, urls);
+      {
+         delegate.getResources(name, cached);
+      }
+      if (cached.isEmpty())
+      {
+         info.blackListResource(name);
+         info.cacheResources(name, Collections.<URL>emptySet());
+      }
+      else
+      {
+         urls.addAll(cached);
+         info.cacheResources(name, cached);
+      }
    }
    
    /**

Modified: projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/ClassLoaderInformation.java
===================================================================
--- projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/ClassLoaderInformation.java	2011-11-21 20:46:52 UTC (rev 112465)
+++ projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/ClassLoaderInformation.java	2011-11-22 15:14:24 UTC (rev 112466)
@@ -24,6 +24,7 @@
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.jboss.classloader.spi.DelegateLoader;
@@ -33,6 +34,7 @@
  * ClassLoaderInformation.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class ClassLoaderInformation
@@ -61,6 +63,9 @@
    /** The resource cache */
    private Map<String, URL> resourceCache;
    
+   /** The resources cache */
+   private Map<String, Set<URL>> resourcesCache;
+
    /** The resource black list */
    private Map<String, String> resourceBlackList;
 
@@ -105,6 +110,7 @@
       {
          classCache = new ConcurrentHashMap<String, Loader>();
          resourceCache = new ConcurrentHashMap<String, URL>();
+         resourcesCache = new ConcurrentHashMap<String, Set<URL>>();
       }
       
       if (canBlackList)
@@ -125,6 +131,8 @@
          classBlackList.clear();
       if (resourceCache != null)
          resourceCache.clear();
+      if (resourcesCache != null)
+         resourcesCache.clear();
       if (resourceBlackList != null)
          resourceBlackList.clear();
    }
@@ -243,6 +251,15 @@
       Map<String, URL> resourceCache = this.resourceCache;
       if (resourceCache != null)
          return resourceCache.get(name);
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+      {
+         Set<URL> urls = resourcesCache.get(name);
+         if (urls != null && urls.isEmpty() == false)
+            return urls.iterator().next();
+      }
+
       return null;
    }
    
@@ -260,6 +277,34 @@
    }
    
    /**
+    * Get the cached urls for a resource
+    *
+    * @param name the resource name
+    * @return any cached urls
+    */
+   public Set<URL> getCachedResources(String name)
+   {
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+         return resourcesCache.get(name);
+
+      return null;
+   }
+
+   /**
+    * Cache urls for a resource
+    *
+    * @param name the resource name
+    * @param urls the cached urls
+    */
+   public void cacheResources(String name, Set<URL> urls)
+   {
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+         resourcesCache.put(name, urls);
+   }
+
+   /**
     * Check whether this is a black listed resource
     * 
     * @param name the resource name



More information about the jboss-cvs-commits mailing list