/** * 构建者模式 * * @return */ public PolicyContext builderPolicy(EventIf eventif, PolicyObject policyObject) { /** * 在炉子里锻造PolicyContext,基本上从eventIf可以推断出必要的PolicyCondition和PolicyAction * * <p>需要根据taskid去查找对应的关联关系表 */ if (eventif != null) { List<PolicyCondition> policyConditions = policyObject.getPolicyConditionList(); List<PolicyAction> policyActions = policyObject.getPolicyActions(); if (eventif != null && policyConditions != null && policyActions != null) { PolicyContext policyContext = new PolicyContext(); policyContext.setPolicyConditions(policyConditions); policyContext.setPolicyActions(policyActions); policyContext.setEvent(eventif); for (PolicyCondition policyCondition : policyConditions) { policyCondition.setParameters(eventif.getParameters()); } for (PolicyAction policyAction : policyActions) { policyAction.setParameters(eventif.getParameters()); } return policyContext; } } return null; }
/** * 在这个处理方法中,一个Policy包含了 组合的Conditions和组合的Actions, 所以返回是执行的结果集列表 * * @param policyContext * @return List<PolicyResult> */ private List<PolicyResult> processPolicyAction(PolicyContext policyContext) { List<PolicyAction> policyActions = policyContext.getPolicyActions(); List<PolicyResult> policyResults = new ArrayList<>(); for (PolicyAction policyAction : policyActions) { String actionId = policyAction.getId(); ActionEntry actionEntry = policyXmlManager.queryActionById(actionId); if (actionEntry != null) { String classPath = actionEntry.getClassPath(); try { Policy policy = getPolicy(actionId, classPath); PolicyResult policyResult = policy.execute(policyContext); // 一个Policy执行结果放入结果集里 policyResults.add(policyResult); } catch (Exception en) { LOG.error("锻造对象失败{}", en.getMessage()); } } } return policyResults; }