コード例 #1
0
 @Override
 public int create(Config config) {
   Config configFound = configDao.findConfigByKey(config.getKey());
   if (configFound != null) {
     Project project = projectDao.getProject(configFound.getProjectId());
     throw new RuntimeBusinessException(
         "该配置项已存在(project: "
             + (project != null ? project.getName() : "***")
             + ", desc: "
             + configFound.getDesc()
             + ")!");
   }
   Integer currentUserId = SecurityUtils.getCurrentUserId();
   int projectId = config.getProjectId();
   if (config.getCreateUserId() == null) {
     config.setCreateUserId(currentUserId);
   }
   if (config.getModifyUserId() == null) {
     config.setModifyUserId(currentUserId);
   }
   projectDao.lockProject(projectId);
   if (!config.isPrivatee()) {
     config.setPrivatee(isSharedProject(projectId));
   }
   config.setSeq(configDao.getMaxSeq(projectId) + 1);
   return configDao.create(config);
 }
コード例 #2
0
 @Override
 public Paginater<Config> paginateConfigs(ConfigCriteria criteria, Paginater<Config> paginater) {
   long configCount = configDao.getConfigCount(criteria);
   List<Config> logList = configDao.getConfigList(criteria, paginater);
   paginater.setTotalCount(configCount);
   paginater.setResults(logList);
   return paginater;
 }
コード例 #3
0
 public void deleteInstance(Config config, int envId) {
   if (hasConfigReferencedTo(config.getKey(), envId)) {
     throw new ReferencedConfigForbidDeleteException(config.getKey());
   }
   configDao.deleteInstance(config.getId(), envId);
   configDao.deleteStatus(config.getId(), envId);
   getRegisterService(envId).unregister(config.getKey());
 }
コード例 #4
0
 private void updateConfigModifyStatus(int configId, int envId) {
   int updated = configDao.updateModifyStatus(configId, envId);
   if (updated == 0) {
     ConfigStatus status = new ConfigStatus(configId, envId);
     Integer currentUserId = SecurityUtils.getCurrentUserId();
     status.setCreateUserId(currentUserId);
     status.setModifyUserId(currentUserId);
     configDao.createStatus(status);
   }
 }
コード例 #5
0
 @Override
 public String getConfigContextValue(int configId, int envId) {
   try {
     List<ConfigInstance> topInsts =
         configDao.findInstanceByConfig(configId, envId, ServiceConstants.MAX_AVAIL_CONFIG_INST);
     if (!topInsts.isEmpty()) {
       Iterator<ConfigInstance> iterator = topInsts.iterator();
       while (iterator.hasNext()) {
         ConfigInstance inst = iterator.next();
         if (inst.isDefault()) {
           iterator.remove();
           break;
         }
       }
     }
     JSONArray valueArray = new JSONArray();
     for (ConfigInstance topInst : topInsts) {
       JSONObject valueObj = new JSONObject();
       valueObj.put("value", resolveConfigFinalValue(topInst.getValue(), envId));
       valueObj.put("context", topInst.getContext());
       valueArray.put(valueObj);
     }
     return valueArray.toString();
   } catch (JSONException e) {
     Config config = getConfig(configId);
     throw new RuntimeBusinessException(
         "Cannot create config[key="
             + config.getKey()
             + "]'s register value, "
             + "plz check its instances' value.",
         e);
   }
 }
コード例 #6
0
 @Override
 public List<ConfigInstance> findInstancesByConfig(int configId, Integer maxPerEnv) {
   Config config = getConfig(configId);
   if (config == null) {
     return Collections.emptyList();
   }
   return configDao.findInstanceByConfig(config.getId(), maxPerEnv);
 }
コード例 #7
0
  public int createInstance(ConfigInstance instance, ConfigSetType setType) {
    Integer currentUserId = SecurityUtils.getCurrentUserId();
    if (instance.getCreateUserId() == null) {
      instance.setCreateUserId(currentUserId);
    }
    if (instance.getModifyUserId() == null) {
      instance.setModifyUserId(currentUserId);
    }
    processInstanceIfReferenceType(instance);
    int retryTimes = 0;
    while (true) {
      try {
        instance.setSeq(configDao.getMaxInstSeq(instance.getConfigId(), instance.getEnvId()) + 1);
        int instId = configDao.createInstance(instance);
        updateConfigModifyStatus(instance.getConfigId(), instance.getEnvId());

        Config config = getConfig(instance.getConfigId());
        List<ConfigInstance> refInstances =
            getInstanceReferencedTo(config.getKey(), instance.getEnvId());

        // 确保注册操作是在最后一步
        if (setType == ConfigSetType.RegisterAndPush) {
          registerAndPush(instance.getConfigId(), instance.getEnvId());
        } else if (setType == ConfigSetType.Register) {
          register(instance.getConfigId(), instance.getEnvId());
        }

        updateReferencedInstances(config, refInstances, setType);

        return instId;
      } catch (RuntimeException e) {
        if (DBUtils.isDuplicateKeyError(e)) {
          if (retryTimes++ >= 1) {
            String errorMsg =
                StringUtils.isNotBlank(instance.getContext())
                    ? "该上下文[context]下配置值已存在!"
                    : "默认配置值已存在!";
            throw new RuntimeBusinessException(errorMsg);
          }
        } else {
          throw e;
        }
      }
    }
  }
コード例 #8
0
 public int updateConfig(Config config) {
   try {
     config.setModifyUserId(SecurityUtils.getCurrentUserId());
     return configDao.update(config);
   } finally {
     cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getId());
     cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
   }
 }
コード例 #9
0
 @Override
 public Map<Integer, Date> findModifyTime(int projectId, int envId) {
   Map<Integer, Date> modifyTimes = new HashMap<Integer, Date>();
   List<ConfigStatus> statusList = configDao.findModifyTime(projectId, envId);
   for (ConfigStatus status : statusList) {
     modifyTimes.put(status.getConfigId(), status.getModifyTime());
   }
   return modifyTimes;
 }
コード例 #10
0
 public void setConfigValue(
     int configId, int envId, String context, String value, ConfigSetType setType) {
   ConfigInstance found = configDao.findInstance(configId, envId, context);
   if (found == null) {
     createInstance(new ConfigInstance(configId, envId, context, value), setType);
   } else {
     found.setValue(value);
     updateInstance(found, setType);
   }
 }
コード例 #11
0
 @Override
 public Config getConfig(int configId) {
   Config config = cacheClient.get(ServiceConstants.CACHE_CONFIG_PREFIX + configId);
   if (config == null) {
     config = configDao.getConfig(configId);
     if (config != null) {
       cacheClient.set(ServiceConstants.CACHE_CONFIG_PREFIX + configId, config);
     }
   }
   return config;
 }
コード例 #12
0
 @Override
 public Config findConfigByKey(String key) {
   Config config = cacheClient.get(ServiceConstants.CACHE_CONFIG_PREFIX + key);
   if (config == null) {
     config = configDao.findConfigByKey(key);
     if (config != null) {
       cacheClient.set(ServiceConstants.CACHE_CONFIG_PREFIX + key, config);
     }
   }
   return config;
 }
コード例 #13
0
 @Override
 public List<ConfigVo> findConfigVos(ConfigCriteria criteria) {
   int projectId = criteria.getProjectId();
   int envId = criteria.getEnvId();
   HasValueEnum hasValue =
       EnumUtils.fromEnumProperty(HasValueEnum.class, "value", criteria.getHasValue());
   List<Config> configs = configDao.findConfigByProject(projectId);
   List<ConfigVo> configVos = new ArrayList<ConfigVo>(configs.size());
   if (!configs.isEmpty()) {
     List<Integer> hasInstanceConfigs = configDao.findHasInstanceConfigs(projectId, envId);
     List<Integer> hasContextInstConfigs = configDao.findHasContextInstConfigs(projectId, envId);
     Map<Integer, ConfigInstance> defaultInsts = configDao.findDefaultInstance(projectId, envId);
     List<Integer> hasReferencedConfigs =
         isSharedProject(projectId)
             ? configDao.getProjectHasReferencedConfigs(projectId)
             : Collections.<Integer>emptyList();
     for (Config config : configs) {
       // 配置不会太多,使用内存过滤,无分页,如果配置太多影响到性能则考虑数据库过滤和分页
       String key = StringUtils.trim(criteria.getKey());
       String value = StringUtils.trim(criteria.getValue());
       ConfigInstance defaultInst = defaultInsts.get(config.getId());
       if ((StringUtils.isEmpty(key) || config.getKey().contains(key))
           && (hasValue == HasValueEnum.All
               || (hasValue == HasValueEnum.Yes && defaultInst != null)
               || (hasValue == HasValueEnum.No && defaultInst == null))
           && (StringUtils.isEmpty(value)
               || (defaultInst != null && defaultInst.getValue().contains(value)))) {
         configVos.add(
             new ConfigVo(
                 config,
                 hasInstanceConfigs.contains(config.getId()),
                 hasContextInstConfigs.contains(config.getId()),
                 hasReferencedConfigs.contains(config.getId()),
                 defaultInst));
       }
     }
   }
   return configVos;
 }
コード例 #14
0
 @Override
 public void moveDown(int projectId, int configId) {
   Config config = getConfig(configId);
   if (config == null) {
     throw new EntityNotFoundException("Config[id=" + configId + "] not found.");
   }
   projectDao.lockProject(projectId); // 锁定指定projectId,避免同一项目下的config出现相同的seq值
   Config nextConfig = configDao.getNextConfig(configId);
   if (nextConfig != null) {
     try {
       int seq = config.getSeq();
       config.setSeq(nextConfig.getSeq());
       nextConfig.setSeq(seq);
       configDao.update(config);
       configDao.update(nextConfig);
     } finally {
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getId());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + nextConfig.getId());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + nextConfig.getKey());
     }
   }
 }
コード例 #15
0
 @Override
 public void moveUp(int projectId, Integer configId) {
   Config config = getConfig(configId);
   if (config == null) {
     throw new EntityNotFoundException("Config[id=" + configId + "] not found.");
   }
   projectDao.lockProject(projectId);
   Config prevConfig = configDao.getPrevConfig(configId);
   if (prevConfig != null) {
     try {
       int seq = config.getSeq();
       config.setSeq(prevConfig.getSeq());
       prevConfig.setSeq(seq);
       configDao.update(config);
       configDao.update(prevConfig);
     } finally {
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getId());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + prevConfig.getId());
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + prevConfig.getKey());
     }
   }
 }
コード例 #16
0
  private int updateInstance(ConfigInstance instance, ConfigSetType setType) {
    instance.setModifyUserId(SecurityUtils.getCurrentUserId());
    processInstanceIfReferenceType(instance);
    int instId = configDao.updateInstance(instance);
    updateConfigModifyStatus(instance.getConfigId(), instance.getEnvId());

    Config config = getConfig(instance.getConfigId());
    List<ConfigInstance> refInstances =
        getInstanceReferencedTo(config.getKey(), instance.getEnvId());

    // 确保注册操作是在最后一步(后续的都需要try-catch掉所有error)
    if (setType == ConfigSetType.RegisterAndPush) {
      registerAndPush(instance.getConfigId(), instance.getEnvId());
    } else if (setType == ConfigSetType.Register) {
      register(instance.getConfigId(), instance.getEnvId());
    }

    updateReferencedInstances(config, refInstances, setType);
    return instId;
  }
コード例 #17
0
 @Override
 public ConfigDeleteResult delete(int configId) {
   final ConfigDeleteResult result = new ConfigDeleteResult();
   final Config config = getConfig(configId);
   if (config != null) {
     result.setConfig(config);
     List<Environment> environments = environmentService.findAll();
     for (final Environment environment : environments) {
       try {
         this.transactionTemplate.execute(
             new TransactionCallbackWithoutResult() {
               @Override
               protected void doInTransactionWithoutResult(TransactionStatus status) {
                 deleteInstance(config, environment.getId());
               }
             });
       } catch (RuntimeException e) {
         logger.error(
             "Delete config["
                 + config.getKey()
                 + "] in environment["
                 + environment.getLabel()
                 + "] failed.",
             e);
         result.addFailedEnv(environment.getLabel());
         if (e instanceof ReferencedConfigForbidDeleteException) {
           result.setHasReference(true);
         }
       }
     }
     if (result.isSucceed()) {
       configDao.delete(configId);
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + configId);
       cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
     }
   }
   return result;
 }
コード例 #18
0
 private boolean hasConfigReferencedTo(String configKey, int envId) {
   return configDao.hasConfigReferencedTo(configKey, envId);
 }
コード例 #19
0
 public List<ConfigInstance> getInstanceReferencedTo(String configKey, int envId) {
   return configDao.getInstanceReferencedTo(configKey, envId);
 }
コード例 #20
0
 @Override
 public List<ConfigInstance> findInstances(int projectId, int envId) {
   return configDao.findInstanceByProjectAndEnv(projectId, envId);
 }
コード例 #21
0
 @Override
 public List<Config> findConfigs(int projectId) {
   return configDao.findConfigByProject(projectId);
 }
コード例 #22
0
 @Override
 public ConfigInstance findInstance(int configId, int envId, String context) {
   return configDao.findInstance(configId, envId, context);
 }
コード例 #23
0
 @Override
 public List<Config> findConfigByKeys(List<String> keys) {
   return configDao.findConfigByKeys(keys);
 }