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