use of org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI in project santuario-java by apache.
the class ClassLoaderTest method testProviderMultipleLoaders.
@SuppressWarnings("resource")
@org.junit.Test
public void testProviderMultipleLoaders() throws Exception {
String baseDir = System.getProperty("basedir");
String fs = System.getProperty("file.separator");
File file0 = new File(baseDir + fs + "build" + fs + "classes" + fs);
File file1 = new File(baseDir + fs + "build" + fs + "test" + fs);
URL[] urls = new URL[2];
urls[0] = file0.toURI().toURL();
urls[1] = file1.toURI().toURL();
URLClassLoader uc1 = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
// load security provider using current class loader
final Provider provider = new XMLDSigRI();
AccessController.doPrivileged((java.security.PrivilegedAction<Object>) () -> Security.addProvider(provider));
// get the provider from java.security.Security using URLClassLoader.
// Need to use introspection to invoke methods to avoid using the
// current class loader
String factoryName = "javax.xml.crypto.dsig.XMLSignatureFactory";
Class<?> factoryClass = uc1.loadClass(factoryName);
Method factoryMethod = factoryClass.getDeclaredMethod("getInstance", new Class[] { String.class });
Class<?> methodParameterClass = uc1.loadClass("javax.xml.crypto.dsig.spec.C14NMethodParameterSpec");
Method canonicalizationMethod = factoryClass.getDeclaredMethod("newCanonicalizationMethod", new Class[] { String.class, methodParameterClass });
Object factory = factoryMethod.invoke(null, "DOM");
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
canonicalizationMethod.invoke(factory, new Object[] { CanonicalizationMethod.EXCLUSIVE, null });
}
long end = System.currentTimeMillis();
long elapsed = end - start;
LOG.debug("Elapsed: {}", elapsed);
}
Aggregations