1 22 package org.jboss.test.jbossmx.implementation.server; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.util.jar.JarOutputStream ; 30 import java.util.jar.Manifest ; 31 import java.util.zip.ZipEntry ; 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerFactory ; 34 import javax.management.ObjectName ; 35 import javax.management.RuntimeErrorException ; 36 37 import junit.framework.Test; 38 import junit.framework.TestSuite; 39 40 import org.jboss.mx.loading.UnifiedLoaderRepository3; 41 import org.jboss.mx.loading.UnifiedClassLoader3; 42 import org.jboss.test.jbossmx.implementation.TestCase; 43 44 54 public class ContextCLTestCase extends TestCase 55 { 56 private static URL dataClassURL; 57 private static File dataJar; 58 private static UnifiedLoaderRepository3 repo; 59 private static UnifiedClassLoader3 deployLoader; 60 private static UnifiedClassLoader3 noLoader; 61 static Object data0; 62 private static URL jarUrl; 63 64 public ContextCLTestCase(String name) 65 { 66 super(name); 67 } 68 69 public void testInvokeNeedingTCL() throws Exception 70 { 71 ClassLoader entryCL = Thread.currentThread().getContextClassLoader(); 72 75 Thread.currentThread().setContextClassLoader(deployLoader); 76 MBeanServer server = MBeanServerFactory.createMBeanServer(); 77 78 try 80 { 81 repo.newClassLoader(jarUrl, true); 82 ObjectName beanName = new ObjectName ("org.jboss.test.jbossmx.implementation.server.support:test=ContextCLTestCase"); 83 server.createMBean("org.jboss.test.jbossmx.implementation.server.support.ContextCL", beanName); 84 getLog().info("Created ContextCL MBean"); 85 86 server.invoke(beanName, "useTestData", null, null); 88 getLog().info("Invoked ContextCL.useTestData"); 89 } 90 catch(RuntimeErrorException e) 91 { 92 getLog().error("NestedError", e.getTargetError()); 93 throw e; 94 } 95 finally 96 { 97 Thread.currentThread().setContextClassLoader(entryCL); 98 99 } 100 101 MBeanServerFactory.releaseMBeanServer(server); 102 } 103 104 109 public void createTestDataJar() throws Exception 110 { 111 repo = new UnifiedLoaderRepository3(); 112 dataJar = new File ("testdata.jar"); 113 dataClassURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/TestData.class"); 115 if( dataClassURL == null && dataJar.exists() == false ) 116 fail("Failed to find /org/jboss/test/jbossmx/implementation/server/support/TestData.class"); 117 if( dataClassURL != null ) 118 { 119 getLog().info("Found TestData at: " + dataClassURL); 120 FileOutputStream fos = new FileOutputStream (dataJar); 122 Manifest mf = new Manifest (); 123 JarOutputStream jos = new JarOutputStream (fos, mf); 124 125 ZipEntry entry = new ZipEntry ("org/jboss/test/jbossmx/implementation/server/support/TestData.class"); 126 jos.putNextEntry(entry); 127 InputStream dataIS = dataClassURL.openStream(); 128 byte[] bytes = new byte[512]; 129 int read = 0; 130 while( (read = dataIS.read(bytes)) > 0 ) 131 jos.write(bytes, 0, read); 132 jos.closeEntry(); 133 dataIS.close(); 134 135 URL mbeanURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCL.class"); 136 entry = new ZipEntry ("org/jboss/test/jbossmx/implementation/server/support/ContextCL.class"); 137 jos.putNextEntry(entry); 138 dataIS = mbeanURL.openStream(); 139 while( (read = dataIS.read(bytes)) > 0 ) 140 jos.write(bytes, 0, read); 141 jos.closeEntry(); 142 dataIS.close(); 143 144 URL imbeanURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCLMBean.class"); 145 entry = new ZipEntry ("org/jboss/test/jbossmx/implementation/server/support/ContextCLMBean.class"); 146 jos.putNextEntry(entry); 147 dataIS = imbeanURL.openStream(); 148 while( (read = dataIS.read(bytes)) > 0 ) 149 jos.write(bytes, 0, read); 150 jos.closeEntry(); 151 dataIS.close(); 152 153 getLog().info("Created mbean jar at: "+dataJar.getAbsolutePath()); 154 jos.close(); 155 156 File dataClassFile = new File (dataClassURL.getFile()); 158 getLog().info("Removed TestData.class File: " + dataClassFile.delete()); 159 File mbeanClassFile = new File (mbeanURL.getFile()); 160 getLog().info("Removed ContextCL.class File: " + mbeanClassFile.delete()); 161 File imbeanClassFile = new File (imbeanURL.getFile()); 162 getLog().info("Removed ContextCLMBean.class File: " + imbeanClassFile.delete()); 163 } 164 165 FileInputStream fis = new FileInputStream ("testdata.jar"); 167 FileOutputStream fos = new FileOutputStream ("testdata1.jar"); 168 byte[] bytes = new byte[512]; 169 int read = 0; 170 while( (read = fis.read(bytes)) > 0 ) 171 fos.write(bytes, 0, read); 172 fis.close(); 173 fos.close(); 174 175 noLoader = (UnifiedClassLoader3) repo.newClassLoader(null, true); 176 deployLoader = (UnifiedClassLoader3) repo.newClassLoader(dataJar.toURL(), true); 177 Class c0 = deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData"); 178 getLog().info("TestData #0 ProtectionDomain: "+c0.getProtectionDomain()); 179 Class c1 = Class.forName("org.jboss.test.jbossmx.implementation.server.support.TestData", 180 false, noLoader); 181 getLog().info("TestData #1 ProtectionDomain: "+c1.getProtectionDomain()); 182 repo.removeClassLoader(deployLoader); 183 File data1Jar = new File ("testdata1.jar"); 185 jarUrl = data1Jar.toURL(); 186 deployLoader = (UnifiedClassLoader3) repo.newClassLoader(jarUrl, true); 187 c0 = deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData"); 188 getLog().info("Reloaded TestData #0 ProtectionDomain: "+c0.getProtectionDomain()); 189 data0 = c0.newInstance(); 190 } 191 192 194 public static Test suite() 195 { 196 TestSuite suite = new TestSuite(); 197 200 try 201 { 202 Class annotationClass = Class.forName("java.lang.annotation.Annotation"); 203 } 205 catch(ClassNotFoundException e) 206 { 207 suite.addTest(new ContextCLTestCase("createTestDataJar")); 209 suite.addTest(new ContextCLTestCase("testInvokeNeedingTCL")); 210 } 211 return suite; 212 } 213 214 } 215 | Popular Tags |