[jboss-cvs] JBossAS SVN: r69332 - in projects/microcontainer/trunk/deployers-impl/src: tests/org/jboss/test/deployers/deployer and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 24 19:28:59 EST 2008


Author: adrian at jboss.org
Date: 2008-01-24 19:28:59 -0500 (Thu, 24 Jan 2008)
New Revision: 69332

Added:
   projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/SecurityActions.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoader.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoaderDeployer.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/test/DeployerContextClassLoaderUnitTestCase.java
Modified:
   projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/DeployerWrapper.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/DeployersDeployerTestSuite.java
Log:
[JBMICROCONT-231] - Make the deployers run with the same context classloader as whoever registered them

Modified: projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/DeployerWrapper.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/DeployerWrapper.java	2008-01-25 00:28:43 UTC (rev 69331)
+++ projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/DeployerWrapper.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -51,6 +51,9 @@
 
    /** The managed object creator */
    private ManagedObjectCreator managedObjectCreator;
+ 
+   /** The context classloader of the person registering the deployer */
+   private ClassLoader classLoader;
    
    /**
     * Create a new DeployerWrapper.
@@ -65,6 +68,7 @@
       if (deployer instanceof ManagedObjectCreator)
          managedObjectCreator = (ManagedObjectCreator) deployer;
       this.log = Logger.getLogger(deployer.getClass());
+      this.classLoader = SecurityActions.getContextClassLoader();
    }
 
    /**
@@ -163,6 +167,7 @@
       if (unit == null)
          throw new IllegalArgumentException("Null unit");
 
+      ClassLoader previous = SecurityActions.setContextClassLoader(classLoader);
       try
       {
          log.trace("Deploying: " + unit.getName());
@@ -174,6 +179,10 @@
          log.debug("Error during deploy: " + unit.getName(), t);
          throw DeploymentException.rethrowAsDeploymentException("Error during deploy: " + unit.getName(), t);
       }
+      finally
+      {
+         SecurityActions.resetContextClassLoader(previous);
+      }
    }
 
    public void undeploy(DeploymentUnit unit)
@@ -181,6 +190,7 @@
       if (unit == null)
          throw new IllegalArgumentException("Null unit");
 
+      ClassLoader previous = SecurityActions.setContextClassLoader(classLoader);
       try
       {
          log.trace("Undeploying: " + unit.getName());
@@ -191,10 +201,15 @@
       {
          log.error("Error during undeploy: " + unit.getName(), t);
       }
+      finally
+      {
+         SecurityActions.resetContextClassLoader(previous);
+      }
    }
 
    public void build(DeploymentUnit unit, Map<String, ManagedObject> managedObjects) throws DeploymentException
    {
+      ClassLoader previous = SecurityActions.setContextClassLoader(classLoader);
       try
       {
          ManagedObjectCreator creator = getManagedObjectCreator();
@@ -205,6 +220,10 @@
       {
          throw DeploymentException.rethrowAsDeploymentException("Error building managed objects for " + unit.getName(), t);
       }
+      finally
+      {
+         SecurityActions.resetContextClassLoader(previous);
+      }
    }
 
    @Override

Added: projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/SecurityActions.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/SecurityActions.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/main/org/jboss/deployers/plugins/deployers/SecurityActions.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -0,0 +1,97 @@
+/*
+* 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.deployers.plugins.deployers;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * SecurityActions.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+class SecurityActions
+{
+   static ClassLoader getContextClassLoader()
+   {
+      if (System.getSecurityManager() == null)
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+      else
+      {
+         return AccessController.doPrivileged(GetContextClassLoader.INSTANCE);
+      }
+   }
+
+   static class GetContextClassLoader implements PrivilegedAction<ClassLoader>
+   {
+      static GetContextClassLoader INSTANCE = new GetContextClassLoader();
+      
+      public ClassLoader run()
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+   }
+   
+   static ClassLoader setContextClassLoader(final ClassLoader classLoader)
+   {
+      if (System.getSecurityManager() == null)
+      {
+         ClassLoader previous = Thread.currentThread().getContextClassLoader();
+         Thread.currentThread().setContextClassLoader(classLoader);
+         return previous;
+      }
+      else
+      {
+         return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+         {
+            public ClassLoader run()
+            {
+               ClassLoader previous = Thread.currentThread().getContextClassLoader();
+               Thread.currentThread().setContextClassLoader(classLoader);
+               return previous;
+            }
+         });
+      }
+   }
+
+   static void resetContextClassLoader(final ClassLoader classLoader)
+   {
+      if (System.getSecurityManager() == null)
+      {
+         Thread.currentThread().setContextClassLoader(classLoader);
+      }
+      else
+      {
+         AccessController.doPrivileged(new PrivilegedAction<Object>()
+         {
+            public Object run()
+            {
+               Thread.currentThread().setContextClassLoader(classLoader);
+               return null;
+            }
+         });
+      }
+   }
+}

Modified: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/DeployersDeployerTestSuite.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/DeployersDeployerTestSuite.java	2008-01-25 00:28:43 UTC (rev 69331)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/DeployersDeployerTestSuite.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -27,6 +27,7 @@
 
 import org.jboss.test.deployers.deployer.test.ComponentUnitTestCase;
 import org.jboss.test.deployers.deployer.test.DeployerClassLoaderUnitTestCase;
+import org.jboss.test.deployers.deployer.test.DeployerContextClassLoaderUnitTestCase;
 import org.jboss.test.deployers.deployer.test.DeployerFlowUnitTestCase;
 import org.jboss.test.deployers.deployer.test.DeployerOrderingUnitTestCase;
 import org.jboss.test.deployers.deployer.test.DeployerProtocolUnitTestCase;
@@ -63,6 +64,7 @@
       suite.addTest(MultipleComponentTypeUnitTestCase.suite());
       suite.addTest(HeuristicAllOrNothingUnitTestCase.suite());
       suite.addTest(HeuristicRussionDollUnitTestCase.suite());
+      suite.addTest(DeployerContextClassLoaderUnitTestCase.suite());
 
       return suite;
    }

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoader.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoader.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoader.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -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.test.deployers.deployer.support;
+
+/**
+ * TestDummyClassLoader.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestDummyClassLoader extends ClassLoader
+{
+   public TestDummyClassLoader()
+   {
+      super(TestDummyClassLoader.class.getClassLoader());
+   }
+}

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoaderDeployer.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoaderDeployer.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestDummyClassLoaderDeployer.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -0,0 +1,59 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.test.deployers.deployer.support;
+
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * TestDummyClassLoaderDeployer.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestDummyClassLoaderDeployer extends AbstractRealDeployer
+{
+   private static ClassLoader classLoader;
+
+   public static ClassLoader getAndResetClassLoader()
+   {
+      ClassLoader result = classLoader;
+      classLoader = null;
+      return result;
+   }
+
+   protected static void checkClassLoader()
+   {
+      classLoader = Thread.currentThread().getContextClassLoader();
+   }
+   
+   protected void internalDeploy(DeploymentUnit unit) throws DeploymentException
+   {
+      checkClassLoader();
+   }
+
+   protected void internalUndeploy(DeploymentUnit unit)
+   {
+      checkClassLoader();
+   }
+}

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/test/DeployerContextClassLoaderUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/test/DeployerContextClassLoaderUnitTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/test/DeployerContextClassLoaderUnitTestCase.java	2008-01-25 00:28:59 UTC (rev 69332)
@@ -0,0 +1,76 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.test.deployers.deployer.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.jboss.deployers.client.spi.DeployerClient;
+import org.jboss.deployers.client.spi.Deployment;
+import org.jboss.test.deployers.AbstractDeployerTest;
+import org.jboss.test.deployers.deployer.support.TestDummyClassLoader;
+import org.jboss.test.deployers.deployer.support.TestDummyClassLoaderDeployer;
+
+/**
+ * DeployerClassLoaderUnitTestCase.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class DeployerContextClassLoaderUnitTestCase extends AbstractDeployerTest
+{
+   public static Test suite()
+   {
+      return new TestSuite(DeployerContextClassLoaderUnitTestCase.class);
+   }
+   
+   public DeployerContextClassLoaderUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void testContextClassLoader() throws Exception
+   {
+      TestDummyClassLoader dummy = new TestDummyClassLoader();
+      ClassLoader previous = Thread.currentThread().getContextClassLoader();
+      Thread.currentThread().setContextClassLoader(dummy);
+      DeployerClient main = null;
+      try
+      {
+         TestDummyClassLoaderDeployer deployer = new TestDummyClassLoaderDeployer();
+         main = createMainDeployer(deployer);
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(previous);
+      }
+      
+      Deployment deployment = createSimpleDeployment("x");
+      main.addDeployment(deployment);
+      main.process();
+      assertEquals(dummy, TestDummyClassLoaderDeployer.getAndResetClassLoader());
+      
+      main.removeDeployment(deployment);
+      main.process();
+      assertEquals(dummy, TestDummyClassLoaderDeployer.getAndResetClassLoader());
+   }
+}




More information about the jboss-cvs-commits mailing list