KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > implementation > server > ContextCLTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmx.implementation.server;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.jar.JarOutputStream JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerFactory JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.RuntimeErrorException JavaDoc;
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 /** Test of the mbean operation invocation thread context class loader. This
45  *test case simulates the problem originally seen in Bug#516649. Reproducing the
46  *steps here requires a number of contrived steps including simulated reloading
47  *of the mbean from different jars and the loading of the TestData class by
48  *the main MBeanClassLoader using Class.forName(). These are the actions that
49  *occur when reloading a sar that contains an ejb-jar.
50  *
51  * @author Scott.Stark@jboss.org
52  * @version $Revision: 58115 $
53  */

54 public class ContextCLTestCase extends TestCase
55 {
56    private static URL JavaDoc dataClassURL;
57    private static File JavaDoc dataJar;
58    private static UnifiedLoaderRepository3 repo;
59    private static UnifiedClassLoader3 deployLoader;
60    private static UnifiedClassLoader3 noLoader;
61    static Object JavaDoc data0;
62    private static URL JavaDoc jarUrl;
63
64    public ContextCLTestCase(String JavaDoc name)
65    {
66       super(name);
67    }
68
69    public void testInvokeNeedingTCL() throws Exception JavaDoc
70    {
71       ClassLoader JavaDoc entryCL = Thread.currentThread().getContextClassLoader();
72       /* Install the mainLoader to simulate how the MBeanServer would be
73        *running under JBoss
74       */

75       Thread.currentThread().setContextClassLoader(deployLoader);
76       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
77
78       // Create the ContextCL MBean using the TestClassLoader
79
try
80       {
81          repo.newClassLoader(jarUrl, true);
82          ObjectName JavaDoc beanName = new ObjectName JavaDoc("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          // Invoke the useTestData op to test the thread context class loader
87
server.invoke(beanName, "useTestData", null, null);
88          getLog().info("Invoked ContextCL.useTestData");
89       }
90       catch(RuntimeErrorException JavaDoc 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    /** This creates two mbean deployment jars, testdata.jar and testdata1.jar.
105     *A redeployment is simulated by loading
106     *
107     * @exception Exception Description of Exception
108     */

109    public void createTestDataJar() throws Exception JavaDoc
110    {
111       repo = new UnifiedLoaderRepository3();
112       dataJar = new File JavaDoc("testdata.jar");
113       // Find the TestData class
114
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          // Build a jar file containing only the TestData.class and ContextCL mbean
121
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(dataJar);
122          Manifest JavaDoc mf = new Manifest JavaDoc();
123          JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(fos, mf);
124
125          ZipEntry JavaDoc entry = new ZipEntry JavaDoc("org/jboss/test/jbossmx/implementation/server/support/TestData.class");
126          jos.putNextEntry(entry);
127          InputStream JavaDoc 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 JavaDoc mbeanURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCL.class");
136          entry = new ZipEntry JavaDoc("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 JavaDoc imbeanURL = getClass().getResource("/org/jboss/test/jbossmx/implementation/server/support/ContextCLMBean.class");
145          entry = new ZipEntry JavaDoc("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          // Now remote the class files from this classpath
157
File JavaDoc dataClassFile = new File JavaDoc(dataClassURL.getFile());
158          getLog().info("Removed TestData.class File: " + dataClassFile.delete());
159          File JavaDoc mbeanClassFile = new File JavaDoc(mbeanURL.getFile());
160          getLog().info("Removed ContextCL.class File: " + mbeanClassFile.delete());
161          File JavaDoc imbeanClassFile = new File JavaDoc(imbeanURL.getFile());
162          getLog().info("Removed ContextCLMBean.class File: " + imbeanClassFile.delete());
163       }
164
165       // Create a copy of the jar
166
FileInputStream JavaDoc fis = new FileInputStream JavaDoc("testdata.jar");
167       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("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 JavaDoc c0 = deployLoader.loadClass("org.jboss.test.jbossmx.implementation.server.support.TestData");
178       getLog().info("TestData #0 ProtectionDomain: "+c0.getProtectionDomain());
179       Class JavaDoc 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       // Simulate a redeploy
184
File JavaDoc data1Jar = new File JavaDoc("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    /** Setup the test suite.
193     */

194    public static Test suite()
195    {
196       TestSuite suite = new TestSuite();
197       /* If this is running under java5 just return as there is no guarentee
198       of what the class loader of the mbean is.
199       */

200       try
201       {
202          Class JavaDoc annotationClass = Class.forName("java.lang.annotation.Annotation");
203          // Running under java5+, use an empty suite
204
}
205       catch(ClassNotFoundException JavaDoc e)
206       {
207          // Running under jdk1.4.x, continue
208
suite.addTest(new ContextCLTestCase("createTestDataJar"));
209          suite.addTest(new ContextCLTestCase("testInvokeNeedingTCL"));
210       }
211       return suite;
212    }
213
214 }
215
Popular Tags