|
|
@@ -0,0 +1,76 @@
|
|
|
+package seecoder.devcloud.core.pipeline;
|
|
|
+
|
|
|
+import seecoder.devcloud.core.pipeline.config.HandlerConfig;
|
|
|
+import seecoder.devcloud.core.pipeline.config.HandlerConfigTable;
|
|
|
+import seecoder.devcloud.core.pipeline.config.PipelineConfig;
|
|
|
+import seecoder.devcloud.core.pipeline.pipeline.PipelineImpl;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author PuHong Weng
|
|
|
+ * @date 2021/3/10
|
|
|
+ * @description:
|
|
|
+ */
|
|
|
+public class PipelineFactory {
|
|
|
+ /**
|
|
|
+ * 流水线初始化
|
|
|
+ * @param configJson configJson 流水线配置的json字符串
|
|
|
+ * @param namespace 流水线所属的namespace,需要用户自己指定,例如devcloud后端使用用户所属的groupName中作为namespace
|
|
|
+ * @param projectName 流水线所属的project,需要用户自己指定
|
|
|
+ * @param pipelineId 流水线标识符,需要用户自己指定
|
|
|
+ * @param pipelineName 流水线名称,需要用户自己指定
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Pipeline init(String configJson, String namespace, String projectName, Integer pipelineId, String pipelineName) {
|
|
|
+
|
|
|
+ PipelineConfig pipelineConfig = PipelineConfig.init(configJson);
|
|
|
+
|
|
|
+ //初始化context
|
|
|
+ Context context = Context.builder()
|
|
|
+ .projectName(projectName)
|
|
|
+ .namespace(namespace)
|
|
|
+ .pipelineId(pipelineId)
|
|
|
+ .pipelineName(pipelineName)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Handler firstHandler = initHandlerChainAndContext(pipelineConfig,context.getConfigs());
|
|
|
+
|
|
|
+ return new PipelineImpl(firstHandler, context);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 handlerConfig 构筑 Handler 的流水线,以及将配置属性统一放入 Context
|
|
|
+ * @param pipelineConfig 流水线配置
|
|
|
+ * @param configs 将此handler配置的所有config加入context中的config中
|
|
|
+ * (所以这一步要避免配置的覆盖,即不要用相同名字的key)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Handler initHandlerChainAndContext(PipelineConfig pipelineConfig, Map<String,String> configs){
|
|
|
+ Handler firstHandler = null;
|
|
|
+ Handler currentHandler = null;
|
|
|
+
|
|
|
+ //转换成对应的Handler实例
|
|
|
+ List<HandlerConfig> handlerConfigs = pipelineConfig.getHandlerConfigs();
|
|
|
+
|
|
|
+
|
|
|
+ HandlerConfig firstHandlerConfig = handlerConfigs.get(0);
|
|
|
+ //todo 相同名字的key可能导致覆盖 这里应该抛出warning
|
|
|
+ configs.putAll(firstHandlerConfig.getConfigs());
|
|
|
+ firstHandler = HandlerConfigTable.transToHandler(firstHandlerConfig);
|
|
|
+ currentHandler = firstHandler;
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 1; i < handlerConfigs.size(); i++){
|
|
|
+ HandlerConfig nextHandlerConfig = handlerConfigs.get(i);
|
|
|
+ //todo 相同名字的key可能导致覆盖 这里应该抛出warning
|
|
|
+ configs.putAll(nextHandlerConfig.getConfigs());
|
|
|
+ Handler nextHandler = HandlerConfigTable.transToHandler(nextHandlerConfig);
|
|
|
+ currentHandler.setNextHandler(nextHandler);
|
|
|
+ currentHandler = nextHandler;
|
|
|
+ }
|
|
|
+
|
|
|
+ return firstHandler;
|
|
|
+ }
|
|
|
+}
|