All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.codingdebugallday.extension.ExtensionInitializer Maven / Gradle / Ivy

The newest version!
package com.github.codingdebugallday.extension;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;

import com.github.codingdebugallday.exceptions.PluginException;
import com.github.codingdebugallday.factory.process.pipe.PluginPipeProcessorExtend;
import com.github.codingdebugallday.factory.process.pipe.classs.PluginClassGroupExtend;
import com.github.codingdebugallday.factory.process.post.PluginPostProcessorExtend;
import com.github.codingdebugallday.loader.PluginResourceLoader;
import com.github.codingdebugallday.utils.CommonUtils;
import com.github.codingdebugallday.utils.OrderPriority;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

/**
 * 

* 静态的扩展初始化器 *

* * @author isaac 2020/6/16 11:51 * @since 1.0 */ public class ExtensionInitializer { private static final Logger LOG = LoggerFactory.getLogger(ExtensionInitializer.class); private static final AtomicBoolean IS_INIT = new AtomicBoolean(false); private static final List RESOURCE_LOADERS_EXTENDS = new ArrayList<>(); private static final List PIPE_PROCESSOR_EXTENDS = new ArrayList<>(); private static final List CLASS_GROUP_EXTENDS = new ArrayList<>(); private static final List POST_PROCESSOR_EXTENDS = new ArrayList<>(); private ExtensionInitializer() { } public static synchronized void initialize(ApplicationContext applicationContext) { if (applicationContext == null) { LOG.error("ApplicationContext is null, cannot initialize"); return; } if (IS_INIT.get()) { throw new PluginException("The extension has been initialized"); } Map pluginExtension = ExtensionFactory.getPluginExtension(); for (Map.Entry entry : pluginExtension.entrySet()) { AbstractExtension abstractExtension = entry.getValue(); if (abstractExtension == null) { continue; } try { abstractExtension.initialize(applicationContext); initialize(abstractExtension, applicationContext); } catch (Exception e) { LOG.error("Plugin extension '{}' initialize exception. {}", abstractExtension.key(), e.getMessage(), e); } } IS_INIT.set(true); } private static void initialize(AbstractExtension abstractExtension, ApplicationContext applicationContext) { StringBuilder debug = new StringBuilder(); String key = abstractExtension.key(); debug.append("Plugin extension '").append(key).append("'") .append(" are ["); iteration(abstractExtension.getPluginResourceLoader(), pluginResourceLoader -> { RESOURCE_LOADERS_EXTENDS.add(pluginResourceLoader); debug.append(pluginResourceLoader.key()).append("、"); }, PluginResourceLoader::order); iteration(abstractExtension.getPluginPipeProcessor(applicationContext), pluginPipeProcessorExtend -> { PIPE_PROCESSOR_EXTENDS.add(pluginPipeProcessorExtend); debug.append(pluginPipeProcessorExtend.key()).append("、"); }, PluginPipeProcessorExtend::order); iteration(abstractExtension.getPluginClassGroup(applicationContext), pluginClassGroupExtend -> { CLASS_GROUP_EXTENDS.add(pluginClassGroupExtend); debug.append(pluginClassGroupExtend.key()).append("、"); }, null); iteration(abstractExtension.getPluginPostProcessor(applicationContext), pluginResourceLoader -> { POST_PROCESSOR_EXTENDS.add(pluginResourceLoader); debug.append(pluginResourceLoader.key()); }, PluginPostProcessorExtend::order); debug.append("] is registered"); LOG.info("Plugin extension '{}' is registered", key); LOG.debug("{}", debug); } public static List getResourceLoadersExtends() { return RESOURCE_LOADERS_EXTENDS; } public static List getPipeProcessorExtends() { return PIPE_PROCESSOR_EXTENDS; } public static List getClassGroupExtends() { return CLASS_GROUP_EXTENDS; } public static List getPostProcessorExtends() { return POST_PROCESSOR_EXTENDS; } /** * 迭代器 * * @param list 当前处理的集合 * @param consumer 消费集合中的数据项 * @param order 排序集合。传入 null 表示不需要排序 */ private static void iteration(List list, Consumer consumer, final Function order) { if (list == null || list.isEmpty()) { return; } if (order != null) { list.stream() .filter(Objects::nonNull) .sorted(CommonUtils.orderPriority(order)) .forEach(consumer); } else { for (T t : list) { if (t != null) { consumer.accept(t); } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy