[jboss-cvs] JBossAS SVN: r111894 - in projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src: test/java/org/jboss/test/classloader/resources/tests and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Aug 1 11:47:11 EDT 2011


Author: bmaxwell
Date: 2011-08-01 11:47:11 -0400 (Mon, 01 Aug 2011)
New Revision: 111894

Added:
   projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/test/java/org/jboss/test/classloader/resources/tests/JBPAPP6824TestCase.java
Modified:
   projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
Log:
[JBPAPP-6927] allow parent-first=true to return resource from parent instead of child

Modified: projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
===================================================================
--- projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java	2011-08-01 14:48:27 UTC (rev 111893)
+++ projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java	2011-08-01 15:47:11 UTC (rev 111894)
@@ -84,7 +84,18 @@
    
    /** Keep track of the added order */
    private int order = 0;
-   
+
+   // JBPAPP-6824 - getResource when parent-first=true returns resource from child
+   private static final Boolean honorResourceDelegation = java.security.AccessController.doPrivileged (
+      new java.security.PrivilegedAction<Boolean>()
+		  {
+   		   public Boolean run()
+   		   {
+      	   return Boolean.valueOf ( System.getProperty("org.jboss.classloader.honor.resource.delegation", "false") );
+   		   }
+		   }
+   );
+ 
    /**
     * Flush the internal caches
     */
@@ -411,12 +422,22 @@
    {
       boolean trace = log.isTraceEnabled();
 
+      URL result = null;
+           
+      if( honorResourceDelegation == true )
+      {
+         // Try the before attempt
+         result = beforeGetResource(name);
+         if (result != null)
+            return result;
+      }
+
       // Try the classloader first
       if (classLoader != null)
       {
          if (trace)
             log.trace(this + " trying to get resource " + name + " from requesting " + classLoader);
-         URL result = classLoader.getResourceLocally(name);
+         result = classLoader.getResourceLocally(name);
          if (result != null)
          {
             if (trace)
@@ -428,21 +449,25 @@
       if (getClassLoaderSystem() == null)
          throw new IllegalStateException("Domain is not registered with a classloader system: " + toLongString());
 
-      // Try the before attempt
-      URL result = beforeGetResource(name);
-      if (result != null)
-         return result;
+      if( ! honorResourceDelegation )
+      {
+         // Try the before attempt
+         result = beforeGetResource(name);
+         if (result != null)
+            return result;
+      }
 
       // Work out the rules
       ClassLoaderInformation info = null;
-      BaseClassLoaderPolicy policy;
+      BaseClassLoaderPolicy policy;            
+      
       if (classLoader != null)
       {
          policy = classLoader.getPolicy();
          info = infos.get(classLoader);
          if (policy.isImportAll())
             allExports = true;
-      }
+      }            
 
       // Next we try the old "big ball of mud" model      
       if (allExports)

Added: projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/test/java/org/jboss/test/classloader/resources/tests/JBPAPP6824TestCase.java
===================================================================
--- projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/test/java/org/jboss/test/classloader/resources/tests/JBPAPP6824TestCase.java	                        (rev 0)
+++ projects/jboss-cl/branches/2.0.9.GA_JBPAPP-6927/classloader/src/test/java/org/jboss/test/classloader/resources/tests/JBPAPP6824TestCase.java	2011-08-01 15:47:11 UTC (rev 111894)
@@ -0,0 +1,204 @@
+/*
+ * 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.test.classloader.resources.tests;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.domain.support.MockLoader;
+import org.jboss.test.classloader.domain.test.ParentPolicyUnitTestCase;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class JBPAPP6824TestCase extends org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity
+{
+   private String SYSTEM_PROPERTY = "org.jboss.classloader.honor.resource.delegation";
+   private ClassLoader previousClassLoader;
+
+   public JBPAPP6824TestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(JBPAPP6824TestCase.class);
+   }
+
+   private void setSystemProperty(final String key, final String value)
+   {      
+      SecurityManager sm = suspendSecurity();
+      try
+      {
+         java.security.AccessController.doPrivileged
+          (
+              new java.security.PrivilegedAction<String>()
+              {
+                public String run()
+                {
+                  return System.setProperty(key, value);               
+                }
+              }
+          );
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+   }
+   
+   private Boolean getSystemProperty()
+   {
+      SecurityManager sm = suspendSecurity();
+
+      try
+      {        
+         Boolean useCorrectParentFirstResourceLoading =
+            java.security.AccessController.doPrivileged
+          (
+              new java.security.PrivilegedAction<Boolean>()
+              {
+                public Boolean run()
+                {
+                  Boolean value = Boolean.valueOf ( System.getProperty(SYSTEM_PROPERTY, "false") );
+                  return value;
+                }
+              }
+          );
+         return useCorrectParentFirstResourceLoading;
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+   }
+   
+   // from org.jboss.test.classloader.resources.tests.ResourceUnitTestCase extends org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity
+   public void testGetResourceFromParentBefore() throws Exception
+   {
+       System.out.println("property: " + getSystemProperty());
+       setSystemProperty(SYSTEM_PROPERTY, "false");
+       System.out.println("property after set: " + getSystemProperty());
+
+      try
+      {
+         // prefix is only on MockClassLoaderPolicy, it looks like it is just used to verify where the resource was found. So there will be two 
+         // testResources in the same package but will be prefixed with a (parent) and b (child)
+         // The testResources are packaged in the resources directory, the policy is overriding getResource and prepending the prefix
+         ClassLoaderSystem system = createClassLoaderSystem();
+         ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+         MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+         policy.setPrefix("a/");
+         policy.setPath("com/acme/p1");
+         policy.setPackageNames(new String[] { "com.acme.p1" } );
+         system.registerClassLoaderPolicy(parent, policy);
+
+         ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+         MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+         policy2.setPrefix("b/");
+         policy2.setPath("com/acme/p1");
+         ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+      
+         assertGetResource("b/", "com/acme/p1/testResource", classLoader);
+      }
+      finally
+      {
+      }
+   }
+
+   public void testGetResourceFromParentBeforeJBPAPP682() throws Exception
+   {
+      System.out.println("property: " + getSystemProperty());
+      setSystemProperty(SYSTEM_PROPERTY, "true");
+      System.out.println("property after: " + getSystemProperty());
+      System.out.println("Classloader type: " + Thread.currentThread().getContextClassLoader().getClass());        
+
+      try
+      {
+         // prefix is only on MockClassLoaderPolicy, it looks like it is just used to verify where the resource was found. So there will be two 
+         // testResources in the same package but will be prefixed with a (parent) and b (child)
+         // The testResources are packaged in the resources directory, the policy is overriding getResource and prepending the prefix
+         ClassLoaderSystem system = createClassLoaderSystem();
+         ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+         MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+         policy.setPrefix("a/");
+         policy.setPath("com/acme/p1");
+         policy.setPackageNames(new String[] { "com.acme.p1" } );
+         system.registerClassLoaderPolicy(parent, policy);
+   
+         ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+         MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+         policy2.setPrefix("b/");
+         policy2.setPath("com/acme/p1");
+         ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+         
+         assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+      }
+      finally
+      {
+         setSystemProperty(SYSTEM_PROPERTY, "false");
+      }
+   }
+   
+   protected URL assertGetResource(String prefix, String resourcePath, ClassLoader classLoader) throws Exception
+   {
+      URL url = classLoader.getResource(resourcePath);     
+      assertNotNull("Should have got resource prefix=" + prefix + " resourcePath " + resourcePath + " from " + classLoader, url);
+      SecurityManager sm = suspendSecurity();
+      try
+      {
+         InputStream is = url.openStream();
+         try
+         {
+            InputStreamReader reader = new InputStreamReader(is);
+            char[] chars = new char[1000];
+            int count = 0;
+            int read = reader.read(chars);
+            while (read != -1)
+            {
+               count += read;
+               read = reader.read(chars, read, 1000 - read);
+            }
+            String string = new String(chars, 0, count);
+            assertEquals("Should have read the correct resource", prefix + resourcePath, string);
+         }
+         finally
+         {
+            is.close();
+         }
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+      return url;
+   }
+}



More information about the jboss-cvs-commits mailing list