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;
}
Aggregations