User: starksm 
  Date: 02/02/19 11:45:03

  Modified:    src/main/org/jboss/test/jbossmx/implementation/server
                        ContextCLTestCase.java
  Log:
  Update the testcase to property demonstrate the class loading problem
  seen with Bug#519699.
  
  Revision  Changes    Path
  1.2       +95 -19    
jbosstest/src/main/org/jboss/test/jbossmx/implementation/server/ContextCLTestCase.java
  
  Index: ContextCLTestCase.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jbosstest/src/main/org/jboss/test/jbossmx/implementation/server/ContextCLTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ContextCLTestCase.java    18 Feb 2002 23:54:32 -0000      1.1
  +++ ContextCLTestCase.java    19 Feb 2002 19:45:03 -0000      1.2
  @@ -7,10 +7,12 @@
   package org.jboss.test.jbossmx.implementation.server;
   
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.io.InputStream;
   import java.net.URL;
   import java.util.jar.JarOutputStream;
  +import java.util.jar.Manifest;
   import java.util.zip.ZipEntry;
   import javax.management.MBeanServer;
   import javax.management.MBeanServerFactory;
  @@ -25,20 +27,32 @@
   import org.apache.log4j.PatternLayout;
   import org.apache.log4j.WriterAppender;
   
  +import org.jboss.system.MBeanClassLoader;
  +import org.jboss.system.ServiceLibraries;
  +import org.jboss.system.UnifiedClassLoader;
   import org.jboss.test.JBossTestSetup;
   import org.jboss.test.jbossmx.implementation.TestCase;
   import org.jboss.test.jbossmx.implementation.server.support.ContextCL;
  -import org.jboss.test.jbossmx.implementation.server.support.TestClassLoader;
   
  -/** Test of the mbean operation invocation thread context class loader.
  +/** Test of the mbean operation invocation thread context class loader. This
  + *test case simulates the problem originally seen in Bug#519699. Reproducing the
  + *steps here requires a number of contrived steps including simulated reloading
  + *of the mbean from different jars and the loading of the TestData class by
  + *the main MBeanClassLoader using Class.forName(). These are the actions that
  + *occur when reloading a sar that contains an ejb-jar.
    *
    * @author  [EMAIL PROTECTED]
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class ContextCLTestCase extends TestCase 
   {
      private static URL dataClassURL;
      private static File dataJar;
  +   private static MBeanClassLoader mainLoader;
  +   private static ObjectName mainLoaderName;
  +   private static UnifiedClassLoader deployLoader;
  +   private static ObjectName deployLoaderName;
  +   static Object data0;
   
      public ContextCLTestCase(String name)
      {
  @@ -47,21 +61,20 @@
   
      public void testInvokeNeedingTCL() throws Exception
      {
  +      ClassLoader entryCL = Thread.currentThread().getContextClassLoader();
  +      /* Install the mainLoader to simulate how the MBeanServer would be
  +       *running under JBoss
  +      */
  +      Thread.currentThread().setContextClassLoader(mainLoader);
         MBeanServer server = MBeanServerFactory.createMBeanServer();
   
  -      // Register the mbean class loader
  -      URL[] urls = {dataJar.toURL()};
  -      TestClassLoader loader = new TestClassLoader(urls);
  -      ObjectName loaderName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:id=" 
+dataJar.getName());
  -      if( server.isRegistered(loaderName) == false )
  -         server.registerMBean(loader, loaderName);
  -      getLog().info("Registered TestClassLoader, urls="+urls[0]);
  -
         // Create the ContextCL MBean using the TestClassLoader
         try
         {
  +         if( server.isRegistered(deployLoaderName) == false )
  +            server.registerMBean(deployLoader, deployLoaderName);
            ObjectName beanName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:test=ContextCLTestCase");
  -         
server.createMBean("org.jboss.test.jbossmx.implementation.server.support.ContextCL", 
beanName, loaderName);
  +         
server.createMBean("org.jboss.test.jbossmx.implementation.server.support.ContextCL", 
beanName, deployLoaderName);
            getLog().info("Created ContextCL MBean");
   
            // Invoke the useTestData op to test the thread context class loader
  @@ -73,11 +86,17 @@
            getLog().error("NestedError", e.getTargetError());
            throw e;
         }
  +      finally
  +      {
  +         Thread.currentThread().setContextClassLoader(entryCL);
  +         
  +      }
   
         MBeanServerFactory.releaseMBeanServer(server);
      }
   
  -   /** Remove the local TestData.class from the classpath and create a tmp
  +   /** This creates two mbean deployment jars, testdata.jar and testdata1.jar.
  +    *A redeployment is simulated by loading 
       *
       * @exception Exception  Description of Exception
       */
  @@ -91,9 +110,11 @@
         if( dataClassURL != null )
         {
            getLog().info("Found TestData at: " + dataClassURL);
  -         // Build a jar file containing only the TestData.class
  +         // Build a jar file containing only the TestData.class and ContextCL mbean
            FileOutputStream fos = new FileOutputStream(dataJar);
  -         JarOutputStream jos = new JarOutputStream(fos);
  +         Manifest mf = new Manifest();
  +         JarOutputStream jos = new JarOutputStream(fos, mf);
  +
            ZipEntry entry = new 
ZipEntry("org/jboss/test/jbossmx/implementation/server/support/TestData.class");
            jos.putNextEntry(entry);
            InputStream dataIS = dataClassURL.openStream();
  @@ -103,17 +124,70 @@
               jos.write(bytes, 0, read);
            jos.closeEntry();
            dataIS.close();
  -         getLog().info("Created TestData.class Jar at: "+dataJar.getAbsolutePath());
   
  -         // Now remote the TestData.class file from this classpath
  +         URL mbeanURL = 
getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCL.class");
  +         entry = new 
ZipEntry("org/jboss/test/jbossmx/implementation/server/support/ContextCL.class");
  +         jos.putNextEntry(entry);
  +         dataIS = mbeanURL.openStream();
  +         while( (read = dataIS.read(bytes)) > 0 )
  +            jos.write(bytes, 0, read);
  +         jos.closeEntry();
  +         dataIS.close();
  +
  +         URL imbeanURL = 
getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCLMBean.class");
  +         entry = new 
ZipEntry("org/jboss/test/jbossmx/implementation/server/support/ContextCLMBean.class");
  +         jos.putNextEntry(entry);
  +         dataIS = imbeanURL.openStream();
  +         while( (read = dataIS.read(bytes)) > 0 )
  +            jos.write(bytes, 0, read);
  +         jos.closeEntry();
  +         dataIS.close();
  +
  +         getLog().info("Created mbean jar at: "+dataJar.getAbsolutePath());
  +         jos.close();
  +
  +         // Now remote the class files from this classpath
            File dataClassFile = new File(dataClassURL.getFile());
            getLog().info("Removed TestData.class File: " + dataClassFile.delete());
  +         File mbeanClassFile = new File(mbeanURL.getFile());
  +         getLog().info("Removed ContextCL.class File: " + mbeanClassFile.delete());
  +         File imbeanClassFile = new File(imbeanURL.getFile());
  +         getLog().info("Removed ContextCLMBean.class File: " + 
imbeanClassFile.delete());
         }
   
  +      // Create a copy of the jar
  +      FileInputStream fis = new FileInputStream("testdata.jar");
  +      FileOutputStream fos = new FileOutputStream("testdata1.jar");
  +      byte[] bytes = new byte[512];
  +      int read = 0;
  +      while( (read = fis.read(bytes)) > 0 )
  +         fos.write(bytes, 0, read);
  +      fis.close();
  +      fos.close();
  +
  +      // Initialize the ServiceLibraries
  +      ServiceLibraries libs = ServiceLibraries.getLibraries();
  +      libs.preRegister(null, null);
  +      // Simulator the initial deploy of the mbean service
  +      mainLoaderName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:id=main");
  +      mainLoader = new MBeanClassLoader(mainLoaderName);
  +      deployLoader = new UnifiedClassLoader(dataJar.toURL());
  +      Class c0 = 
deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData");
  +      getLog().info("TestData #0 ProtectionDomain: "+c0.getProtectionDomain());
  +      Class c1 = 
Class.forName("org.jboss.test.jbossmx.implementation.server.support.TestData",
  +         false, mainLoader);
  +      getLog().info("TestData #1 ProtectionDomain: "+c1.getProtectionDomain());
  +      libs.removeClassLoader(deployLoader);
  +      // Simulate a redeploy
  +      File data1Jar = new File("testdata1.jar");
  +      deployLoaderName = new 
ObjectName("org.jboss.test.jbossmx.implementation.server.support:key="+data1Jar.getName());
  +      deployLoader = new UnifiedClassLoader(data1Jar.toURL());
  +      c0 = 
deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData");
  +      getLog().info("Reloaded TestData #0 ProtectionDomain: 
"+c0.getProtectionDomain());
  +      data0 = c0.newInstance();
      }
   
  -   /**
  -    * Setup the test suite.
  +   /** Setup the test suite.
       */
      public static Test suite()
      {
  @@ -123,6 +197,8 @@
         return suite;
      }
   
  +   /** Allow the test case to run standalone
  +    */
      public static void main(java.lang.String[] args)
      {
         // Set up a simple configuration that logs on the console.
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to