Search in sources :

Example 6 with EntityAccessException

use of org.mx.dal.exception.EntityAccessException in project main by JohnPeng739.

the class TopologyManageServiceImpl method save.

/**
 * {@inheritDoc}
 *
 * @see TopologyManageService#save(String, String)
 */
@Transactional
@Override
public Topology save(String id, String topologyJsonStr) throws UserInterfaceErrorException {
    try {
        Topology topology;
        if (StringUtils.isBlank(id)) {
            topology = EntityFactory.createEntity(Topology.class);
        } else {
            topology = accessor.getById(id, Topology.class);
            if (topology == null) {
                throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_NOT_FOUND);
            }
        }
        JSONObject json = JSON.parseObject(topologyJsonStr);
        String name = json.getString("name");
        topology.setName(name);
        topology.setDescription(json.getString("description"));
        topology.setSubmitted(false);
        topology.setSubmitInfo("");
        topology.setTopologyId(null);
        topology.setTopologyContent(topologyJsonStr);
        topology = accessor.save(topology);
        operateLogService.writeLog(String.format("成功保存[%s]计算拓扑。", name));
        return topology;
    } catch (EntityAccessException | EntityInstantiationException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) EntityAccessException(org.mx.dal.exception.EntityAccessException) Topology(com.ds.retl.dal.entity.Topology) EntityInstantiationException(org.mx.dal.exception.EntityInstantiationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with EntityAccessException

use of org.mx.dal.exception.EntityAccessException in project main by JohnPeng739.

the class TopologyManageServiceImpl method submit.

/**
 * 提交一个拓扑实体对象到Storm集群中
 *
 * @param topology   拓扑实体对象
 * @param simulation 是否仿真
 * @return 成功返回拓扑实体对象
 * @throws UserInterfaceErrorException 提交过程中发生的异常
 */
private Topology submit(Topology topology, boolean simulation) throws UserInterfaceErrorException {
    String topologyName = topology.getName();
    if (!simulation) {
        // 检查拓扑名字是否已经被部署到Storm集群中
        JSONObject found = foundSubmitedTopology(topologyName);
        if (found != null) {
            try {
                topology.setSubmitted(false);
                topology.setSubmittedTime(new Date().getTime());
                topology.setSubmitInfo("同名的计算拓扑已经被部署到集群中,不能重复部署。");
                accessor.save(topology);
            } catch (EntityAccessException ex) {
                if (logger.isErrorEnabled()) {
                    logger.error("Save submit information fail.", ex);
                }
            }
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_ALREADY_SUBMITTED);
        }
    }
    String confStr = topology.getTopologyContent();
    if (StringUtils.isBlank(confStr)) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Topology's content is blank, name: %s.", topologyName));
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    JSONObject topologyJson = prepareTopologyConfigJsonObject(confStr);
    if (topology == null) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Parse JSON object fail, conf: %s.", confStr));
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    if (!simulation) {
        String stormHome = env.getProperty("storm.home"), stormBin = env.getProperty("storm.bin"), retlHome = env.getProperty("storm.retl"), retlPlatform = env.getProperty("storm.retl.platform"), retlDeps = env.getProperty("storm.retl.deps"), retlConf = env.getProperty("storm.retl.conf");
        String configName = String.format("%s/%s/%s.json", retlHome, retlConf, topologyName);
        File file = new File(configName);
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))) {
            bw.write(JSON.toJSONString(topologyJson, true));
        } catch (IOException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_FILE_OPERATE_FAIL);
        }
        try {
            int timeout = 60;
            String submitInfo = new RETLStormCli(stormHome, stormBin, retlHome, retlPlatform, retlDeps).deploy(configName, timeout);
            topology.setSubmitInfo(submitInfo);
            for (int index = 0; index < timeout; index++) {
                JSONObject submittedTopology = foundSubmitedTopology(topologyName);
                if (submittedTopology != null) {
                    topology.setSubmittedTime(new Date().getTime());
                    String topologyId = submittedTopology.getString("id");
                    topology.setTopologyId(topologyId);
                    topology.setSubmitted(true);
                    topology = accessor.save(topology);
                    operateLogService.writeLog(String.format("成功提交[%s]计算拓扑。", topologyName));
                    return topology;
                } else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        if (logger.isErrorEnabled()) {
                            logger.error(ex);
                        }
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("The topology can be found in cluster after wait 10s.");
            }
            topology.setSubmittedTime(new Date().getTime());
            topology.setSubmitted(false);
            accessor.save(topology);
            operateLogService.writeLog(String.format("提交[%s]计算拓扑未成功。", topologyName));
            throw new UserInterfaceErrorException(UserInterfaceErrors.TOPOLOGY_SUBMIT_FAIL);
        } catch (EntityAccessException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
        }
    } else {
        // 本地仿真运行
        new ETLTopologyBuilder().buildTopology(false, topologyJson);
        return topology;
    }
}
Also used : ETLTopologyBuilder(com.ds.retl.ETLTopologyBuilder) RETLStormCli(com.ds.retl.cli.RETLStormCli) JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) EntityAccessException(org.mx.dal.exception.EntityAccessException)

Example 8 with EntityAccessException

use of org.mx.dal.exception.EntityAccessException in project main by JohnPeng739.

the class UserManageServiceImpl method initUser.

/**
 * {@inheritDoc}
 *
 * @see UserManageService#initUser()
 */
@Transactional
@Override
public User initUser() throws UserInterfaceErrorException {
    String userCode = "ds110";
    try {
        User user = accessor.getByCode(userCode, User.class);
        if (user == null) {
            user = EntityFactory.createEntity(User.class);
            user.setCode("ds110");
        }
        try {
            user.setPassword(DigestUtils.md5("edmund110119"));
        } catch (NoSuchAlgorithmException ex) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.USER_PASSWORD_DISGEST_FAIL);
        }
        user.setName("RETL管理员");
        user.setRoles("manager");
        return saveUser(user);
    } catch (EntityInstantiationException | EntityAccessException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : User(com.ds.retl.dal.entity.User) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) EntityAccessException(org.mx.dal.exception.EntityAccessException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EntityInstantiationException(org.mx.dal.exception.EntityInstantiationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with EntityAccessException

use of org.mx.dal.exception.EntityAccessException in project main by JohnPeng739.

the class UserManageServiceImpl method logout.

/**
 * {@inheritDoc}
 *
 * @see UserManageService#logout(String)
 */
@Transactional
@Override
public void logout(String userCode) throws UserInterfaceErrorException {
    try {
        User user = accessor.getByCode(userCode, User.class);
        if (user == null) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.USER_NOT_FOUND);
        }
        user.setOnline(false);
        accessor.save(user);
        logService.writeLog(String.format("用户[%s]成功注销退出系统。", userCode));
    } catch (EntityAccessException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    }
}
Also used : User(com.ds.retl.dal.entity.User) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) EntityAccessException(org.mx.dal.exception.EntityAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with EntityAccessException

use of org.mx.dal.exception.EntityAccessException in project main by JohnPeng739.

the class ServerManageServiceImpl method saveServerInfo.

/**
 * {@inheritDoc}
 *
 * @see ServerManageService#saveServerInfo(String)
 */
@Override
public JSONObject saveServerInfo(String info) throws UserInterfaceErrorException {
    if (StringUtils.isBlank(info)) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    JSONObject json;
    try {
        json = JSON.parseObject(info);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Parse json fail, %s.", info), ex);
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    String machineName = json.getString("machineName"), machineIp = json.getString("machineIp");
    if (StringUtils.isBlank(machineName)) {
        if (logger.isErrorEnabled()) {
            logger.error("The machine's name is blank.");
        }
        throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_ILLEGAL_PARAM);
    }
    try {
        ConfigJson config = accessor.getByCode(createMachineConfigField(machineName), ConfigJson.class);
        if (config == null) {
            config = EntityFactory.createEntity(ConfigJson.class);
            config.setCode(createMachineConfigField(machineName));
        }
        config.setConfigContent(info);
        accessor.save(config);
        config = accessor.getByCode(createMachineConfigField(machineName), ConfigJson.class);
        if (config == null) {
            throw new UserInterfaceErrorException(UserInterfaceErrors.SYSTEM_CONFIG_NOT_FOUND);
        }
        operateLogService.writeLog(String.format("保存服务器[name=%s, ip=%s]配置信息成功。", machineName, machineIp));
        return JSON.parseObject(config.getConfigContent());
    } catch (EntityAccessException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_OPERATE_FAIL);
    } catch (EntityInstantiationException ex) {
        throw new UserInterfaceErrorException(UserInterfaceErrors.DB_ENTITY_INSTANCE_FAIL);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) ConfigJson(com.ds.retl.dal.entity.ConfigJson) EntityAccessException(org.mx.dal.exception.EntityAccessException) EntityInstantiationException(org.mx.dal.exception.EntityInstantiationException) EntityAccessException(org.mx.dal.exception.EntityAccessException) EntityInstantiationException(org.mx.dal.exception.EntityInstantiationException) UserInterfaceErrorException(com.ds.retl.exception.UserInterfaceErrorException) IOException(java.io.IOException) RestInvokeException(org.mx.rest.client.RestInvokeException)

Aggregations

UserInterfaceErrorException (com.ds.retl.exception.UserInterfaceErrorException)15 EntityAccessException (org.mx.dal.exception.EntityAccessException)15 Transactional (org.springframework.transaction.annotation.Transactional)6 JSONObject (com.alibaba.fastjson.JSONObject)5 User (com.ds.retl.dal.entity.User)5 Topology (com.ds.retl.dal.entity.Topology)4 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 EntityInstantiationException (org.mx.dal.exception.EntityInstantiationException)3 ConfigJson (com.ds.retl.dal.entity.ConfigJson)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintStream (java.io.PrintStream)2 Date (java.util.Date)2 DataVO (org.mx.rest.vo.DataVO)2 PaginationDataVO (org.mx.rest.vo.PaginationDataVO)2 ETLTopologyBuilder (com.ds.retl.ETLTopologyBuilder)1 RETLStormCli (com.ds.retl.cli.RETLStormCli)1 TopologyVO (com.ds.retl.rest.vo.topology.TopologyVO)1 UserVO (com.ds.retl.rest.vo.user.UserVO)1