Search in sources :

Example 1 with IdAwarePlugin

use of com.tencent.polaris.api.plugin.IdAwarePlugin in project polaris-java by polarismesh.

the class PluginManager method initPlugins.

@Override
public void initPlugins(InitContext context) throws PolarisException {
    // 先实例化所有插件
    int baseId = 0;
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = new HashMap<>();
        typedPlugins.put(pluginType, plugins);
        ServiceLoader<? extends Plugin> loader = ServiceLoader.load(pluginType.getClazz());
        for (Plugin plugin : loader) {
            baseId++;
            String name = plugin.getName();
            if (StringUtils.isBlank(name) || plugins.containsKey(name)) {
                throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("duplicated name for plugin(name=%s, type=%s)", name, pluginType));
            }
            if (plugin instanceof IdAwarePlugin) {
                ((IdAwarePlugin) plugin).setId(baseId);
            }
            plugins.put(name, plugin);
        }
    }
    // 再进行初始化
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
            pluginEntry.getValue().init(context);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) HashMap(java.util.HashMap) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) PluginType(com.tencent.polaris.api.plugin.PluginType) HashMap(java.util.HashMap) Map(java.util.Map) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) Plugin(com.tencent.polaris.api.plugin.Plugin)

Aggregations

PolarisException (com.tencent.polaris.api.exception.PolarisException)1 IdAwarePlugin (com.tencent.polaris.api.plugin.IdAwarePlugin)1 Plugin (com.tencent.polaris.api.plugin.Plugin)1 PluginType (com.tencent.polaris.api.plugin.PluginType)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1