test/com/sun/org/apache/xml/internal/security/transforms/ClassLoaderTest.java

Print this page

        

*** 37,64 **** import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; import com.sun.org.apache.xml.internal.security.transforms.Transform; public class ClassLoaderTest { ! private final static String BASE = System.getProperty("test.src", "./"); public static void main(String[] args) throws Exception { Transform.init(); File file = new File(BASE); URL[] urls = new URL[1]; urls[0] = file.toURI().toURL(); URLClassLoader ucl = new URLClassLoader(urls); Class c = ucl.loadClass("MyTransform"); Constructor cons = c.getConstructor(); Object o = cons.newInstance(); // Apache code swallows the ClassNotFoundExc, so we need to // check if the Transform has already been registered by registering // it again and catching an AlgorithmAlreadyRegisteredExc - try { Transform.register(MyTransform.URI, "MyTransform"); throw new Exception("ClassLoaderTest failed"); } catch (AlgorithmAlreadyRegisteredException e) { // test passed } } } --- 37,75 ---- import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; import com.sun.org.apache.xml.internal.security.transforms.Transform; public class ClassLoaderTest { ! private final static String BASE = System.getProperty("test.classes", "./"); public static void main(String[] args) throws Exception { Transform.init(); File file = new File(BASE); URL[] urls = new URL[1]; urls[0] = file.toURI().toURL(); URLClassLoader ucl = new URLClassLoader(urls); Class c = ucl.loadClass("MyTransform"); Constructor cons = c.getConstructor(); + + Thread curThread = Thread.currentThread(); + ClassLoader ctxLoader = curThread.getContextClassLoader(); + ClassLoader loader = MyTransform.class.getClassLoader(); + try { + // In agentvm mode, the class MyTransform is loaded by the child of + // context ClassLoader of this thread. Replace context ClassLoader + // with its child to avoid ClassNotFoundException thrown from + // Transform.register(String, String) method. + curThread.setContextClassLoader(loader); Object o = cons.newInstance(); // Apache code swallows the ClassNotFoundExc, so we need to // check if the Transform has already been registered by registering // it again and catching an AlgorithmAlreadyRegisteredExc Transform.register(MyTransform.URI, "MyTransform"); throw new Exception("ClassLoaderTest failed"); } catch (AlgorithmAlreadyRegisteredException e) { // test passed + } finally { + curThread.setContextClassLoader(ctxLoader); } } }