|
@@ -12,21 +12,22 @@ import java.lang.reflect.Field;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @author PuHong Weng
|
|
|
|
|
- * @date 2021/3/10
|
|
|
|
|
- * @description: 记录流水线配置json中 name到具体Handler的映射
|
|
|
|
|
|
|
+ * Handler 配置表
|
|
|
|
|
+ * 根据流水线配置 JSON 中的 name 映射到具体 Handler 类
|
|
|
|
|
+ * 并通过反射将 JSON 配置注入 Handler 实例
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class HandlerConfigTable {
|
|
public class HandlerConfigTable {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据名称映射对应的流水线执行类
|
|
|
|
|
|
|
+ * name -> Handler 类映射表
|
|
|
*/
|
|
*/
|
|
|
private static final Map<String, List<Class<?>>> handlerConfigMap;
|
|
private static final Map<String, List<Class<?>>> handlerConfigMap;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
static {
|
|
static {
|
|
|
handlerConfigMap = new HashMap<>();
|
|
handlerConfigMap = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 构建单个 Handler 映射
|
|
|
handlerConfigMap.put("node-image-build", Collections.singletonList(NodeImageBuildHandler.class));
|
|
handlerConfigMap.put("node-image-build", Collections.singletonList(NodeImageBuildHandler.class));
|
|
|
handlerConfigMap.put("java-image-build", Collections.singletonList(JavaImageBuildHandler.class));
|
|
handlerConfigMap.put("java-image-build", Collections.singletonList(JavaImageBuildHandler.class));
|
|
|
handlerConfigMap.put("k8s-deployment", Collections.singletonList(K8sDeploymentHandler.class));
|
|
handlerConfigMap.put("k8s-deployment", Collections.singletonList(K8sDeploymentHandler.class));
|
|
@@ -40,39 +41,61 @@ public class HandlerConfigTable {
|
|
|
handlerConfigMap.put("sonar-java-image-build", Collections.singletonList(SonarJavaImageBuildHandler.class));
|
|
handlerConfigMap.put("sonar-java-image-build", Collections.singletonList(SonarJavaImageBuildHandler.class));
|
|
|
handlerConfigMap.put("k8s-job", Collections.singletonList(K8sJobHandler.class));
|
|
handlerConfigMap.put("k8s-job", Collections.singletonList(K8sJobHandler.class));
|
|
|
|
|
|
|
|
- handlerConfigMap.put("sonar-java", Arrays.asList(SonarJavaImageBuildHandler.class, K8sJobHandler.class, SonarPostProcessHandler.class));
|
|
|
|
|
- handlerConfigMap.put("sonar-java-deploy", Arrays.asList(SonarJavaImageBuildHandler.class, K8sJobHandler.class, SonarPostProcessHandler.class));
|
|
|
|
|
|
|
+ // 构建组合 Handler 映射
|
|
|
|
|
+ handlerConfigMap.put("sonar-java", Arrays.asList(
|
|
|
|
|
+ SonarJavaImageBuildHandler.class,
|
|
|
|
|
+ K8sJobHandler.class,
|
|
|
|
|
+ SonarPostProcessHandler.class
|
|
|
|
|
+ ));
|
|
|
|
|
+ handlerConfigMap.put("sonar-java-deploy", Arrays.asList(
|
|
|
|
|
+ SonarJavaImageBuildHandler.class,
|
|
|
|
|
+ K8sJobHandler.class,
|
|
|
|
|
+ SonarPostProcessHandler.class
|
|
|
|
|
+ ));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将 name 和配置 JSON 转换为 Handler 链
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param name Handler 名称
|
|
|
|
|
+ * @param config 配置 JSON
|
|
|
|
|
+ * @return Handler 链头节点
|
|
|
|
|
+ * @throws Exception 转换异常
|
|
|
|
|
+ */
|
|
|
public static Handler transToHandler(String name, JsonNode config) throws Exception {
|
|
public static Handler transToHandler(String name, JsonNode config) throws Exception {
|
|
|
Handler firstHandler = null;
|
|
Handler firstHandler = null;
|
|
|
Handler lastHandler = null;
|
|
Handler lastHandler = null;
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- List<Class<?>> handlers = handlerConfigMap.get(name);
|
|
|
|
|
- // 对所有Handler注入依赖
|
|
|
|
|
- for (Class<?> handlerClass : handlers) {
|
|
|
|
|
|
|
+ List<Class<?>> handlerClasses = handlerConfigMap.get(name);
|
|
|
|
|
+ if (handlerClasses == null || handlerClasses.isEmpty()) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+
|
|
|
|
|
+ for (Class<?> handlerClass : handlerClasses) {
|
|
|
|
|
+ // 创建 Handler 实例
|
|
|
Handler handler = (Handler) handlerClass.newInstance();
|
|
Handler handler = (Handler) handlerClass.newInstance();
|
|
|
- //通过反射注入配置对应Handler类
|
|
|
|
|
- Iterator<Map.Entry<String, JsonNode>> jsonNodeIterator = config.fields();
|
|
|
|
|
- while (jsonNodeIterator.hasNext()) {
|
|
|
|
|
- Map.Entry<String, JsonNode> entry = jsonNodeIterator.next();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 反射注入配置属性
|
|
|
|
|
+ Iterator<Map.Entry<String, JsonNode>> fields = config.fields();
|
|
|
|
|
+ while (fields.hasNext()) {
|
|
|
|
|
+ Map.Entry<String, JsonNode> entry = fields.next();
|
|
|
String propertyName = entry.getKey();
|
|
String propertyName = entry.getKey();
|
|
|
- JsonNode propertyValues = entry.getValue();
|
|
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
- // 遍历json,去handler中获取对应的成员变量,因此要处理找不到变量的情况
|
|
|
|
|
- Field field;
|
|
|
|
|
|
|
+ JsonNode propertyValue = entry.getValue();
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- field = handler.getClass().getDeclaredField(propertyName);
|
|
|
|
|
|
|
+ Field field = handler.getClass().getDeclaredField(propertyName);
|
|
|
field.setAccessible(true);
|
|
field.setAccessible(true);
|
|
|
- //注意,这种写法无法自动注入List<xx>等的字段,因为泛型信息由于java的机制被删除了
|
|
|
|
|
- // 所以最好还是传入一些基本类型或者String,在对应的handler自己解析
|
|
|
|
|
- field.set(handler, mapper.readValue(propertyValues.traverse(), field.getType()));
|
|
|
|
|
- } catch (NoSuchFieldException ignored){
|
|
|
|
|
- // 如果Handler中没有对应json中的config属性,跳过,直接取下一个属性。
|
|
|
|
|
- // 注意java中异常当场处理之后会从try后面继续运行
|
|
|
|
|
|
|
+ // 将 JSON 值注入字段
|
|
|
|
|
+ field.set(handler, mapper.readValue(propertyValue.traverse(), field.getType()));
|
|
|
|
|
+ } catch (NoSuchFieldException ignored) {
|
|
|
|
|
+ // 如果 Handler 中没有对应字段,直接跳过
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // 设置 Handler
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 链接 Handler
|
|
|
if (firstHandler == null) {
|
|
if (firstHandler == null) {
|
|
|
firstHandler = handler;
|
|
firstHandler = handler;
|
|
|
}
|
|
}
|
|
@@ -81,9 +104,12 @@ public class HandlerConfigTable {
|
|
|
}
|
|
}
|
|
|
lastHandler = handler;
|
|
lastHandler = handler;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
- throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE, PipelineException.CONFIG_TRANSFER_ERROR, e);
|
|
|
|
|
|
|
+ throw new PipelineException(HttpStatus.SC_SERVICE_UNAVAILABLE,
|
|
|
|
|
+ PipelineException.CONFIG_TRANSFER_ERROR, e);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return firstHandler;
|
|
return firstHandler;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|