Search in sources :

Example 1 with HazelcastAPIDelegatingClassloader

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());
}
Also used : HazelcastAPIDelegatingClassloader(com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 2 with HazelcastAPIDelegatingClassloader

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);
}
Also used : Config(com.hazelcast.config.Config) JobConfig(com.hazelcast.jet.config.JobConfig) HazelcastAPIDelegatingClassloader(com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader) File(java.io.File) URL(java.net.URL)

Example 3 with HazelcastAPIDelegatingClassloader

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);
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastAPIDelegatingClassloader(com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader) HazelcastClient(com.hazelcast.client.HazelcastClient)

Aggregations

HazelcastAPIDelegatingClassloader (com.hazelcast.test.starter.HazelcastAPIDelegatingClassloader)3 HazelcastClient (com.hazelcast.client.HazelcastClient)1 Config (com.hazelcast.config.Config)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 JobConfig (com.hazelcast.jet.config.JobConfig)1 SlowTest (com.hazelcast.test.annotation.SlowTest)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 Test (org.junit.Test)1