Search in sources :

Example 1 with PluginType

use of com.tencent.polaris.api.plugin.PluginType 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)

Example 2 with PluginType

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

the class PluginManager method postContextInitPlugins.

/**
 * 在应用上下文初始化完毕后进行调用
 *
 * @param extensions 插件实例
 * @throws PolarisException 运行异常
 */
public void postContextInitPlugins(Extensions extensions) throws PolarisException {
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        if (MapUtils.isEmpty(plugins)) {
            continue;
        }
        for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
            Plugin plugin = pluginEntry.getValue();
            plugin.postContextInit(extensions);
        }
    }
}
Also used : 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)

Example 3 with PluginType

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

the class PluginManager method destroyPlugins.

/**
 * 销毁已初始化的插件列表
 */
@Override
public void destroyPlugins() {
    // 倒序遍历
    for (int i = pluginTypes.size() - 1; i >= 0; i--) {
        PluginType pluginType = pluginTypes.get(i);
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        if (MapUtils.isEmpty(plugins)) {
            continue;
        }
        for (Map.Entry<String, Plugin> plugin : plugins.entrySet()) {
            plugin.getValue().destroy();
        }
    }
}
Also used : 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)

Example 4 with PluginType

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

the class SDKContext method initContextByConfig.

/**
 * 通过配置对象初始化SDK上下文
 *
 * @param config 配置对象
 * @return SDK上下文
 * @throws PolarisException 初始化过程的异常
 */
public static SDKContext initContextByConfig(Configuration config) throws PolarisException {
    try {
        ((ConfigurationImpl) config).setDefault();
        config.verify();
    } catch (IllegalArgumentException e) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, "fail to verify configuration", e);
    }
    ServiceLoader<TypeProvider> providers = ServiceLoader.load(TypeProvider.class);
    List<PluginType> types = new ArrayList<>();
    for (TypeProvider provider : providers) {
        types.addAll(provider.getTypes());
    }
    PluginManager manager = new PluginManager(types);
    ValueContext valueContext = new ValueContext();
    valueContext.setHost(parseHost(config));
    valueContext.setServerConnectorProtocol(parseServerConnectorProtocol(config));
    SDKContext initContext = new SDKContext(config, manager, valueContext);
    try {
        manager.initPlugins(initContext);
    } catch (Throwable e) {
        manager.destroyPlugins();
        if (e instanceof PolarisException) {
            throw e;
        }
        throw new PolarisException(ErrorCode.PLUGIN_ERROR, "plugin error", e);
    }
    return initContext;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) PluginManager(com.tencent.polaris.api.plugin.impl.PluginManager) ValueContext(com.tencent.polaris.api.plugin.common.ValueContext) ArrayList(java.util.ArrayList) TypeProvider(com.tencent.polaris.api.plugin.TypeProvider) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) PluginType(com.tencent.polaris.api.plugin.PluginType)

Aggregations

PluginType (com.tencent.polaris.api.plugin.PluginType)4 IdAwarePlugin (com.tencent.polaris.api.plugin.IdAwarePlugin)3 Plugin (com.tencent.polaris.api.plugin.Plugin)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 PolarisException (com.tencent.polaris.api.exception.PolarisException)2 TypeProvider (com.tencent.polaris.api.plugin.TypeProvider)1 ValueContext (com.tencent.polaris.api.plugin.common.ValueContext)1 PluginManager (com.tencent.polaris.api.plugin.impl.PluginManager)1 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)1 ArrayList (java.util.ArrayList)1