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;
import javax.management.ObjectName;
import javax.management.RuntimeErrorException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.log4j.Category;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.WriterAppender;
import org.jboss.mx.loading.UnifiedLoaderRepository3;
import org.jboss.mx.loading.UnifiedClassLoader3;
import org.jboss.test.jbossmx.implementation.TestCase;
public class ContextCLTestCase extends TestCase
{
private static URL dataClassURL;
private static File dataJar;
private static UnifiedLoaderRepository3 repo;
private static UnifiedClassLoader3 deployLoader;
private static UnifiedClassLoader3 noLoader;
static Object data0;
private static URL jarUrl;
public ContextCLTestCase(String name)
{
super(name);
}
public void testInvokeNeedingTCL() throws Exception
{
ClassLoader entryCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(deployLoader);
MBeanServer server = MBeanServerFactory.createMBeanServer();
try
{
repo.newClassLoader(jarUrl, true);
ObjectName beanName = new ObjectName("org.jboss.test.jbossmx.implementation.server.support:test=ContextCLTestCase");
server.createMBean("org.jboss.test.jbossmx.implementation.server.support.ContextCL", beanName);
getLog().info("Created ContextCL MBean");
server.invoke(beanName, "useTestData", null, null);
getLog().info("Invoked ContextCL.useTestData");
}
catch(RuntimeErrorException e)
{
getLog().error("NestedError", e.getTargetError());
throw e;
}
finally
{
Thread.currentThread().setContextClassLoader(entryCL);
}
MBeanServerFactory.releaseMBeanServer(server);
}
public void createTestDataJar() throws Exception
{
repo = new UnifiedLoaderRepository3();
dataJar = new File("testdata.jar");
dataClassURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/TestData.class");
if( dataClassURL == null && dataJar.exists() == false )
fail("Failed to find /org/jboss/test/jbossmx/implementation/server/support/TestData.class");
if( dataClassURL != null )
{
getLog().info("Found TestData at: " + dataClassURL);
FileOutputStream fos = new FileOutputStream(dataJar);
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();
byte[] bytes = new byte[512];
int read = 0;
while( (read = dataIS.read(bytes)) > 0 )
jos.write(bytes, 0, read);
jos.closeEntry();
dataIS.close();
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();
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());
}
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();
noLoader = (UnifiedClassLoader3) repo.newClassLoader(null, true);
deployLoader = (UnifiedClassLoader3) repo.newClassLoader(dataJar.toURL(), true);
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, noLoader);
getLog().info("TestData #1 ProtectionDomain: "+c1.getProtectionDomain());
repo.removeClassLoader(deployLoader);
File data1Jar = new File("testdata1.jar");
jarUrl = data1Jar.toURL();
deployLoader = (UnifiedClassLoader3) repo.newClassLoader(jarUrl, true);
c0 = deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData");
getLog().info("Reloaded TestData #0 ProtectionDomain: "+c0.getProtectionDomain());
data0 = c0.newInstance();
}
public static Test suite()
{
TestSuite suite = new TestSuite();
try
{
Class annotationClass = Class.forName("java.lang.annotation.Annotation");
}
catch(ClassNotFoundException e)
{
suite.addTest(new ContextCLTestCase("createTestDataJar"));
suite.addTest(new ContextCLTestCase("testInvokeNeedingTCL"));
}
return suite;
}
public static void main(java.lang.String[] args)
{
Category root = Category.getRoot();
root.addAppender(new WriterAppender(new PatternLayout("%x%m%n"), System.out));
Test suite = ContextCLTestCase.suite();
junit.textui.TestRunner.run(suite);
}
}