use of com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader in project hazelcast by hazelcast.
the class HazelcastProxyFactoryTest method testReturnedProxyImplements_sameInterfaceByNameOnTargetClassLoader.
@Test
public void testReturnedProxyImplements_sameInterfaceByNameOnTargetClassLoader() throws Exception {
ProxiedInterface delegate = new ProxiedInterface() {
@Override
public void get() {
}
};
// HazelcastAPIDelegatingClassloader will reload the bytes of ProxiedInterface as a new class
// as happens with every com.hazelcast class that contains "test"
HazelcastAPIDelegatingClassloader targetClassLoader = new HazelcastAPIDelegatingClassloader(new URL[] {}, HazelcastProxyFactoryTest.class.getClassLoader());
Object proxy = HazelcastProxyFactory.proxyObjectForStarter(targetClassLoader, delegate);
assertNotNull(proxy);
Class<?>[] ifaces = proxy.getClass().getInterfaces();
assertEquals(1, ifaces.length);
Class<?> proxyInterface = ifaces[0];
// it is not the same class but has the same name on a different classloader
assertNotEquals(ProxiedInterface.class, proxyInterface);
assertEquals(ProxiedInterface.class.getName(), proxyInterface.getName());
assertEquals(targetClassLoader, proxyInterface.getClassLoader());
}
use of com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader in project hazelcast by hazelcast.
the class ProcessorClassLoaderTest method createHazelcastMember.
private HazelcastInstance createHazelcastMember() throws MalformedURLException {
Config config = smallInstanceConfig();
config.getJetConfig().setResourceUploadEnabled(true);
// Create a member in a separate classloader without the test classes loaded
URL classesUrl = new File("target/classes/").toURI().toURL();
HazelcastAPIDelegatingClassloader classloader = new HazelcastAPIDelegatingClassloader(new URL[] { classesUrl }, // Need to delegate to system classloader, which has maven dependencies like Jackson
ClassLoader.getSystemClassLoader());
return HazelcastStarter.newHazelcastInstance(config, classloader);
}
use of com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader in project hazelcast by hazelcast.
the class HazelcastClientStarter method newHazelcastClient.
@SuppressWarnings("unchecked")
public static HazelcastInstance newHazelcastClient(String version, ClientConfig clientConfig, boolean enterprise, List<URL> additionalJars) {
ClassLoader classLoader = clientConfig == null ? null : clientConfig.getClassLoader();
HazelcastAPIDelegatingClassloader classloader = getTargetVersionClassloader(version, enterprise, classLoader, additionalJars);
ClassLoader contextClassLoader = currentThread().getContextClassLoader();
currentThread().setContextClassLoader(null);
try {
Class<HazelcastClient> hazelcastClass = (Class<HazelcastClient>) classloader.loadClass("com.hazelcast.client.HazelcastClient");
System.out.println(hazelcastClass + " loaded by " + hazelcastClass.getClassLoader());
Class<?> configClass = classloader.loadClass("com.hazelcast.client.config.ClientConfig");
Object config = getConfig(classloader, configClass, clientConfig);
Method newHazelcastInstanceMethod = hazelcastClass.getMethod("newHazelcastClient", configClass);
Object delegate = newHazelcastInstanceMethod.invoke(null, config);
return (HazelcastInstance) proxyObjectForStarter(HazelcastStarter.class.getClassLoader(), delegate);
} catch (ClassNotFoundException e) {
throw rethrowGuardianException(e);
} catch (NoSuchMethodException e) {
throw rethrowGuardianException(e);
} catch (IllegalAccessException e) {
throw rethrowGuardianException(e);
} catch (InvocationTargetException e) {
throw rethrowGuardianException(e);
} catch (InstantiationException e) {
throw rethrowGuardianException(e);
} finally {
if (contextClassLoader != null) {
currentThread().setContextClassLoader(contextClassLoader);
}
}
}
Aggregations