use of com.alipay.sofa.ark.spi.service.extension.ExtensionClass in project sofa-ark by alipay.
the class ExtensionClassTest method testExtensionClass.
@Test
public void testExtensionClass() {
Extensible extensible = SingletonService.class.getAnnotation(Extensible.class);
Extension extension = SingletonServiceImpl.class.getAnnotation(Extension.class);
ExtensionClass extensionClass = new ExtensionClass();
extensionClass.setExtensible(extensible);
extensionClass.setExtension(extension);
extensionClass.setInterfaceClass(SingletonService.class);
extensionClass.setImplementClass(SingletonServiceImpl.class);
Object object1 = extensionClass.getObject();
Object object2 = extensionClass.getObject();
Object singleton = extensionClass.getSingleton();
Assert.assertTrue(object1.equals(object2));
Assert.assertTrue(object1.equals(singleton));
Assert.assertEquals(200, extensionClass.getPriority());
}
use of com.alipay.sofa.ark.spi.service.extension.ExtensionClass in project sofa-ark by alipay.
the class ExtensionLoaderServiceImpl method loadExtension.
private <I, L> Set<ExtensionClass<I, L>> loadExtension(Class<I> interfaceType, String extensionName, L location, ClassLoader resourceLoader) throws Throwable {
BufferedReader reader = null;
try {
Set<ExtensionClass<I, L>> extensionClassSet = new HashSet<>();
Extensible extensible = interfaceType.getAnnotation(Extensible.class);
if (extensible == null) {
throw new ArkRuntimeException(String.format("Extensible class %s is not annotated by %s.", interfaceType, Extensible.class));
}
String fileName = interfaceType.getCanonicalName();
if (!StringUtils.isEmpty(extensible.file())) {
fileName = extensible.file();
}
Enumeration<URL> enumeration = resourceLoader.getResources(EXTENSION_FILE_DIR + fileName);
while (enumeration.hasMoreElements()) {
URL url = enumeration.nextElement();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Loading extension of extensible: {} from location: {} and file: {}", interfaceType, location, url);
}
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
ExtensionClass<I, L> extensionClass = new ExtensionClass<>();
extensionClass.setDefinedLocation(location);
extensionClass.setExtensible(extensible);
extensionClass.setInterfaceClass(interfaceType);
Class<?> implementClass = null;
String clazzName = line.trim();
try {
implementClass = resourceLoader.loadClass(clazzName);
} catch (Exception e) {
if (ArkConfigs.isEmbedEnable() && resourceLoader != ArkClient.getMasterBiz().getBizClassLoader()) {
implementClass = ArkClient.getMasterBiz().getBizClassLoader().loadClass(clazzName);
} else {
throw e;
}
}
if (!interfaceType.isAssignableFrom(implementClass)) {
throw new ArkRuntimeException(String.format("Extension implementation class %s is not type of %s.", implementClass.getCanonicalName(), interfaceType.getCanonicalName()));
}
Extension extension = implementClass.getAnnotation(Extension.class);
if (extension == null) {
throw new ArkRuntimeException(String.format("Extension implementation class %s is not annotated by %s.", implementClass, Extension.class));
}
if (!extensionName.equals(extension.value())) {
continue;
}
extensionClass.setExtension(extension);
extensionClass.setImplementClass((Class<I>) implementClass);
extensionClassSet.add(extensionClass);
}
}
return extensionClassSet;
} catch (Throwable throwable) {
LOGGER.error("Loading extension files from {} occurs an error {}.", location, throwable);
throw throwable;
} finally {
if (reader != null) {
reader.close();
}
}
}
Aggregations