Search in sources :

Example 1 with Extensible

use of com.alipay.sofa.ark.spi.service.extension.Extensible 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());
}
Also used : Extension(com.alipay.sofa.ark.spi.service.extension.Extension) Extensible(com.alipay.sofa.ark.spi.service.extension.Extensible) ExtensionClass(com.alipay.sofa.ark.spi.service.extension.ExtensionClass) Test(org.junit.Test)

Example 2 with Extensible

use of com.alipay.sofa.ark.spi.service.extension.Extensible 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();
        }
    }
}
Also used : Extensible(com.alipay.sofa.ark.spi.service.extension.Extensible) InputStreamReader(java.io.InputStreamReader) ExtensionClass(com.alipay.sofa.ark.spi.service.extension.ExtensionClass) URL(java.net.URL) URL(java.net.URL) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) ArkRuntimeException(com.alipay.sofa.ark.exception.ArkRuntimeException) Extension(com.alipay.sofa.ark.spi.service.extension.Extension) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Aggregations

Extensible (com.alipay.sofa.ark.spi.service.extension.Extensible)2 Extension (com.alipay.sofa.ark.spi.service.extension.Extension)2 ExtensionClass (com.alipay.sofa.ark.spi.service.extension.ExtensionClass)2 ArkRuntimeException (com.alipay.sofa.ark.exception.ArkRuntimeException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1