Ark extension mechanism
The Ark container and Ark Plugin are loaded by different classloaders in the run time, and the regular ServiceLoader cannot be used to provide SPI extensions. Therefore, SOFAArk customizes the extension point SPI mechanism, and Ark Plugin implements the SPI mechanism.
Due to Biz uninstallation issues, Ark Biz does not support this SPI mechanism for the time being, and only applies to Ark Plugins.
Declare an extended interface
The extension interface is declared @Extensible using annotations. The annotations are defined as follows:
@Target({ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Extensible{
/**
* return specify extensible file name, default value is the
* full name of interface.
*/
String file() default"";
/**
* return whether this a singleton, with a single, shared instance
* returned on all calls, default value is true.
*/
boolean singleton() default true;
}file: used to declare the name of the SPI extension. The default value is the full class name of the interface.singleton: used to declare whether the loading extension class is in singleton mode.
Declare an extension implementation
Use the annotation @Extension to declare the extension implementation. The annotation is defined as follows:
@Target({ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Extension{
/**
* extension name
*/
String value();
/**
* extension order, Higher values are interpreted as lower priority.
* As a consequence, the object with the lowest value has the highest
* priority.
*/
int order() default 100;
}value: the name of the extension implementation. For example, different plug-ins may use different names to extend the same interface.order: Used to determine the order in which a specific extension implementation takes effect.run time, the following rules apply to extension implementations of the same interface:
Rule 1: For extension implementations with the same name, only the extension implementation class with the highest priority is returned. The smaller the order number, the higher the priority.
Rule 2: If extension implementations have different names, a corresponding List list is returned, and the extension implementation with the highest priority is returned for each name.
Load the SPI implementation class
Under normal circumstances, use ServiceLoader to load the SPI interface implementation. SOFAArk provides a tool class ArkServiceLoader for loading extension implementations. The tool class defines two simple methods:
Method 1: Used to load the extension implementation of the specified interface and name and return a single result. Refer to Rule 1 above.
Method 2: Used to load the extension implementation of the specified interface and return the list result. Refer to Rule 2 above.
The sample code is as follows:
public class ArkServiceLoader{
private static ExtensionLoaderService extensionLoaderService;
// Method 1
public static<T> T loadExtension(Class<T> interfaceType,String extensionName){
return extensionLoaderService.getExtensionContributor(interfaceType, extensionName);
}
// Method 2
public static<T>List<T> loadExtension(Class<T> interfaceType){
return extensionLoaderService.getExtensionContributor(interfaceType);
}
}The plug-in that defines the SPI interface needs to export the interface, and the plug-in that implements the SPI interface needs to import the interface. The SOFAArk container itself also defines some SPI interfaces that are used by plug-in extensions, such as
ClassLoaderHook.Because Biz will dynamically install and unload, if the extension supporting Biz is loaded, the life cycle is easy to cause confusion, so the SPI extension supporting Biz is not considered for the time being. If an Ark Plugin needs to trigger the logic call of Ark Biz, you can use the SOFAArk internal event mechanism.
SOFAArk default extension point
The SOFAArk container currently provides the only extension point ClassLoaderHook for other plug-ins to provide extension implementations, custom classes, or resource loading logic.
The ClassLoaderHooker interface is used to extend BizClassLoader and PluginClassLoader class (resource) loading logic and is defined as follows:
@Extensible
public interface ClassLoaderHook<T>{
Class<?> preFindClass(String name,ClassLoaderService classLoaderService, T t)
throws ClassNotFoundException;
Class<?> postFindClass(String name,ClassLoaderService classLoaderService, T t)
throws ClassNotFoundException;
URL preFindResource(String name,ClassLoaderService classLoaderService, T t);
URL postFindResource(String name,ClassLoaderService classLoaderService, T t);
Enumeration<URL> preFindResources(String name,ClassLoaderService classLoaderService, T t)
throws IOException;
Enumeration<URL> postFindResources(String name,ClassLoaderService classLoaderService, T t)
throws IOException;
}By extending the SPI interface implementation in the plug-in, you can customize the loading logic of PluginClassLoader and BizClassLoader classes or resources.
Extensions implement PluginClassLoader load logic
To define an extension implementation for a PluginClassLoader, you must specify the extension name plugin-classloader-hook. This is because the SOFAArk policy only allows ClassLoaderHook extension implementation to take effect for one plug-in. If multiple extension classes are defined at the same time, the one with the highest priority takes effect.
@Extension("plugin-classloader-hook")
public class TestPluginClassLoaderHook implements ClassLoaderHook<Plugin>{
}Extensions implement BizClassLoader load logic
Defining an extension implementation to BizClassLoader requires specifying the extension name biz-classloader-hook for the same reason as defining an extension implementation to PluginClassLoader. The SOFAArk policy allows only one Biz ClassLoaderHook extension implementation to take effect. If multiple extension classes are defined at the same time, the one with the highest priority takes effect.
@Extension("biz-classloader-hook")
public class TestBizClassLoaderHook implements ClassLoaderHook<Biz>{
}