Search in sources :

Example 1 with ClassLoaderAwareInvocationHandler

use of com.hortonworks.registries.common.util.ClassLoaderAwareInvocationHandler in project registry by hortonworks.

the class SchemaRegistryClient method createInstance.

private <T> T createInstance(SerDesInfo serDesInfo, boolean isSerializer) {
    Set<Class<?>> interfaceClasses = isSerializer ? SERIALIZER_INTERFACE_CLASSES : DESERIALIZER_INTERFACE_CLASSES;
    if (interfaceClasses == null || interfaceClasses.isEmpty()) {
        throw new IllegalArgumentException("interfaceClasses array must be neither null nor empty.");
    }
    // loading serializer, create a class loader and and keep them in cache.
    final SerDesPair serDesPair = serDesInfo.getSerDesPair();
    String fileId = serDesPair.getFileId();
    // get class loader for this file ID
    ClassLoader classLoader = classLoaderCache.getClassLoader(fileId);
    T t;
    try {
        String className = isSerializer ? serDesPair.getSerializerClassName() : serDesPair.getDeserializerClassName();
        Class<T> clazz = (Class<T>) Class.forName(className, true, classLoader);
        t = clazz.newInstance();
        List<Class<?>> classes = new ArrayList<>();
        for (Class<?> interfaceClass : interfaceClasses) {
            if (interfaceClass.isAssignableFrom(clazz)) {
                classes.add(interfaceClass);
            }
        }
        if (classes.isEmpty()) {
            throw new RuntimeException("Given Serialize/Deserializer " + className + " class does not implement any " + "one of the registered interfaces: " + interfaceClasses);
        }
        Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), classes.toArray(new Class[classes.size()]), new ClassLoaderAwareInvocationHandler(classLoader, t));
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new SerDesException(e);
    }
    return t;
}
Also used : ArrayList(java.util.ArrayList) DEFAULT_CONNECTION_TIMEOUT(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_CONNECTION_TIMEOUT) DEFAULT_READ_TIMEOUT(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_READ_TIMEOUT) ClassLoaderAwareInvocationHandler(com.hortonworks.registries.common.util.ClassLoaderAwareInvocationHandler) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) SerDesPair(com.hortonworks.registries.schemaregistry.SerDesPair)

Aggregations

ClassLoaderAwareInvocationHandler (com.hortonworks.registries.common.util.ClassLoaderAwareInvocationHandler)1 SerDesPair (com.hortonworks.registries.schemaregistry.SerDesPair)1 DEFAULT_CONNECTION_TIMEOUT (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_CONNECTION_TIMEOUT)1 DEFAULT_READ_TIMEOUT (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.DEFAULT_READ_TIMEOUT)1 SerDesException (com.hortonworks.registries.schemaregistry.serde.SerDesException)1 ArrayList (java.util.ArrayList)1