Search in sources :

Example 6 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ManageEditProjectController method saveProject.

/**
 * 检查项目lib 情况
 *
 * @param id     项目id
 * @param newLib 新路径
 * @return 状态码,400是一定不能操作的,401 是提醒
 */
@RequestMapping(value = "judge_lib.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String saveProject(String id, String newLib) {
    File file = FileUtil.file(newLib);
    // new File(newLib);
    // 填写的jar路径是一个存在的文件
    Assert.state(!FileUtil.isFile(file), "填写jar目录当前是一个已经存在的文件,请修改");
    NodeProjectInfoModel exits = projectInfoService.getItem(id);
    if (exits == null) {
        // 创建项目 填写的jar路径是已经存在的文件夹
        Assert.state(!FileUtil.exist(file), "填写jar目录当前已经在,创建成功后会自动同步文件");
    } else {
        // 已经存在的项目
        File oldLib = new File(exits.allLib());
        Path newPath = file.toPath();
        Path oldPath = oldLib.toPath();
        if (newPath.equals(oldPath)) {
            // 新 旧没有变更
            return JsonMessage.getString(200, "");
        }
        if (file.exists()) {
            String msg;
            if (oldLib.exists()) {
                // 新旧jar路径都存在,会自动覆盖新的jar路径中的文件
                msg = "原jar目录已经存在并且新的jar目录已经存在,保存将覆盖新文件夹并会自动同步原jar目录";
            } else {
                msg = "填写jar目录当前已经在,创建成功后会自动同步文件";
            }
            return JsonMessage.getString(401, msg);
        }
    }
    Assert.state(!Validator.isChinese(newLib), "不建议使用中文目录");
    return JsonMessage.getString(200, "");
}
Also used : Path(java.nio.file.Path) NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ManageEditProjectController method checkPath.

/**
 * 路径存在包含关系
 *
 * @param nodeProjectInfoModel 比较的项目
 */
private void checkPath(NodeProjectInfoModel nodeProjectInfoModel) {
    List<NodeProjectInfoModel> nodeProjectInfoModelList = projectInfoService.list();
    if (nodeProjectInfoModelList == null) {
        return;
    }
    NodeProjectInfoModel nodeProjectInfoModel1 = null;
    for (NodeProjectInfoModel model : nodeProjectInfoModelList) {
        if (!model.getId().equals(nodeProjectInfoModel.getId())) {
            File file1 = new File(model.allLib());
            File file2 = new File(nodeProjectInfoModel.allLib());
            if (FileUtil.pathEquals(file1, file2)) {
                nodeProjectInfoModel1 = model;
                break;
            }
            // 包含关系
            if (FileUtil.isSub(file1, file2) || FileUtil.isSub(file2, file1)) {
                nodeProjectInfoModel1 = model;
                break;
            }
        }
    }
    if (nodeProjectInfoModel1 != null) {
        throw new IllegalArgumentException("项目Jar路径和【" + nodeProjectInfoModel1.getName() + "】项目冲突:" + nodeProjectInfoModel1.allLib());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) File(java.io.File)

Example 8 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ManageEditProjectController method deleteProject.

/**
 * 删除项目
 *
 * @return json
 */
@RequestMapping(value = "deleteProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String deleteProject(String copyId) {
    NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel();
    if (nodeProjectInfoModel == null) {
        return JsonMessage.getString(200, "项目不存在");
    }
    try {
        NodeProjectInfoModel.JavaCopyItem copyItem = nodeProjectInfoModel.findCopyItem(copyId);
        if (copyItem == null) {
            // 运行判断
            boolean run = AbstractProjectCommander.getInstance().isRun(nodeProjectInfoModel, null);
            Assert.state(!run, "不能删除正在运行的项目");
            projectInfoService.deleteItem(nodeProjectInfoModel.getId());
        } else {
            boolean run = AbstractProjectCommander.getInstance().isRun(nodeProjectInfoModel, copyItem);
            Assert.state(!run, "不能删除正在运行的项目副本");
            boolean removeCopyItem = nodeProjectInfoModel.removeCopyItem(copyId);
            Assert.state(removeCopyItem, "删除对应副本集不存在");
            projectInfoService.updateItem(nodeProjectInfoModel);
        }
        return JsonMessage.getString(200, "删除成功!");
    } catch (Exception e) {
        DefaultSystemLog.getLog().error(e.getMessage(), e);
        return JsonMessage.getString(500, "删除异常:" + e.getMessage());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ManageEditProjectController method checkParameter.

/**
 * 基础检查
 *
 * @param projectInfo        项目实体
 * @param whitelistDirectory 白名单
 * @param previewData        预检查数据
 */
private void checkParameter(NodeProjectInfoModel projectInfo, String whitelistDirectory, boolean previewData) {
    String id = projectInfo.getId();
    String checkId = StrUtil.replace(id, StrUtil.DASHED, StrUtil.UNDERLINE);
    Validator.validateGeneral(checkId, 2, Const.ID_MAX_LEN, "项目id 长度范围2-20(英文字母 、数字和下划线)");
    Assert.state(!JpomApplication.SYSTEM_ID.equals(id), "项目id " + JpomApplication.SYSTEM_ID + " 关键词被系统占用");
    // 防止和Jpom冲突
    ConfigBean instance = ConfigBean.getInstance();
    Assert.state(!instance.applicationTag.equalsIgnoreCase(id), "当前项目id已经被Jpom占用");
    // 运行模式
    String runMode = getParameter("runMode");
    RunMode runMode1 = RunMode.ClassPath;
    try {
        runMode1 = RunMode.valueOf(runMode);
    } catch (Exception ignored) {
    }
    projectInfo.setRunMode(runMode1);
    // 监测
    if (runMode1 == RunMode.ClassPath || runMode1 == RunMode.JavaExtDirsCp) {
        Assert.hasText(projectInfo.getMainClass(), "ClassPath、JavaExtDirsCp 模式 MainClass必填");
    } else if (runMode1 == RunMode.Jar || runMode1 == RunMode.JarWar) {
        projectInfo.setMainClass(StrUtil.EMPTY);
    }
    if (runMode1 == RunMode.JavaExtDirsCp) {
        Assert.hasText(projectInfo.getJavaExtDirsCp(), "JavaExtDirsCp 模式 javaExtDirsCp必填");
    }
    // 判断是否为分发添加
    String strOutGivingProject = getParameter("outGivingProject");
    boolean outGivingProject = Boolean.parseBoolean(strOutGivingProject);
    projectInfo.setOutGivingProject(outGivingProject);
    if (!previewData) {
        // 不是预检查数据才效验白名单
        if (!whitelistDirectoryService.checkProjectDirectory(whitelistDirectory)) {
            if (outGivingProject) {
                whitelistDirectoryService.addProjectWhiteList(whitelistDirectory);
            } else {
                throw new IllegalArgumentException("请选择正确的项目路径,或者还没有配置白名单");
            }
        }
        String logPath = projectInfo.getLogPath();
        if (StrUtil.isNotEmpty(logPath)) {
            if (!whitelistDirectoryService.checkProjectDirectory(logPath)) {
                if (outGivingProject) {
                    whitelistDirectoryService.addProjectWhiteList(logPath);
                } else {
                    throw new IllegalArgumentException("请填写的项目日志存储路径,或者还没有配置白名单");
                }
            }
        }
    }
    // 
    String lib = projectInfo.getLib();
    Assert.state(StrUtil.isNotEmpty(lib) && !StrUtil.SLASH.equals(lib) && !Validator.isChinese(lib), "项目Jar路径不能为空,不能为顶级目录,不能包含中文");
    Assert.state(checkPathSafe(lib), "项目Jar路径存在提升目录问题");
    // java 程序副本
    if (runMode1 == RunMode.ClassPath || runMode1 == RunMode.Jar || runMode1 == RunMode.JarWar || runMode1 == RunMode.JavaExtDirsCp) {
        String javaCopyIds = getParameter("javaCopyIds");
        if (StrUtil.isEmpty(javaCopyIds)) {
            projectInfo.setJavaCopyItemList(null);
        } else {
            String[] split = StrUtil.splitToArray(javaCopyIds, StrUtil.COMMA);
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = new ArrayList<>(split.length);
            for (String copyId : split) {
                String jvm = getParameter("jvm_" + copyId);
                String args = getParameter("args_" + copyId);
                // 
                NodeProjectInfoModel.JavaCopyItem javaCopyItem = new NodeProjectInfoModel.JavaCopyItem();
                javaCopyItem.setId(copyId);
                javaCopyItem.setParentId(id);
                javaCopyItem.setModifyTime(DateUtil.now());
                javaCopyItem.setJvm(StrUtil.emptyToDefault(jvm, StrUtil.EMPTY));
                javaCopyItem.setArgs(StrUtil.emptyToDefault(args, StrUtil.EMPTY));
                javaCopyItemList.add(javaCopyItem);
            }
            projectInfo.setJavaCopyItemList(javaCopyItemList);
        }
    } else {
        projectInfo.setJavaCopyItemList(null);
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RunMode(io.jpom.model.RunMode) ArrayList(java.util.ArrayList) ConfigBean(io.jpom.system.ConfigBean)

Example 10 with NodeProjectInfoModel

use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.

the class ManageEditProjectController method save.

/**
 * 保存项目
 *
 * @param projectInfo 项目
 * @param previewData 是否是预检查
 * @return 错误信息
 */
private String save(NodeProjectInfoModel projectInfo, boolean previewData) {
    projectInfo.setWorkspaceId(getWorkspaceId());
    NodeProjectInfoModel exits = projectInfoService.getItem(projectInfo.getId());
    try {
        this.checkPath(projectInfo);
        RunMode runMode = projectInfo.getRunMode();
        if (exits == null) {
            // 检查运行中的tag 是否被占用
            if (runMode != RunMode.File && runMode != RunMode.Dsl) {
                Assert.state(!AbstractProjectCommander.getInstance().isRun(projectInfo, null), "当前项目id已经被正在运行的程序占用");
            }
            if (previewData) {
                // 预检查数据
                return JsonMessage.getString(200, "检查通过");
            } else {
                projectInfoService.addItem(projectInfo);
                return JsonMessage.getString(200, "新增成功!");
            }
        }
        if (previewData) {
            // 预检查数据
            return JsonMessage.getString(200, "检查通过");
        } else {
            exits.setLog(projectInfo.getLog());
            exits.setLogPath(projectInfo.getLogPath());
            exits.setName(projectInfo.getName());
            exits.setGroup(projectInfo.getGroup());
            exits.setAutoStart(projectInfo.getAutoStart());
            exits.setMainClass(projectInfo.getMainClass());
            exits.setLib(projectInfo.getLib());
            exits.setJvm(projectInfo.getJvm());
            exits.setArgs(projectInfo.getArgs());
            exits.setWorkspaceId(this.getWorkspaceId());
            exits.setOutGivingProject(projectInfo.isOutGivingProject());
            exits.setRunMode(runMode);
            exits.setWhitelistDirectory(projectInfo.getWhitelistDirectory());
            exits.setToken(projectInfo.getToken());
            exits.setDslContent(projectInfo.getDslContent());
            exits.setJdkId(projectInfo.getJdkId());
            // 检查是否非法删除副本集
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = exits.getJavaCopyItemList();
            List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList1 = projectInfo.getJavaCopyItemList();
            if (CollUtil.isNotEmpty(javaCopyItemList) && !CollUtil.containsAll(javaCopyItemList1, javaCopyItemList)) {
                // 重写了 equals
                return JsonMessage.getString(405, "修改中不能删除副本集、请到副本集中删除");
            }
            exits.setJavaCopyItemList(javaCopyItemList1);
            exits.setJavaExtDirsCp(projectInfo.getJavaExtDirsCp());
            // 
            moveTo(exits, projectInfo);
            projectInfoService.updateItem(exits);
            return JsonMessage.getString(200, "修改成功");
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error(e.getMessage(), e);
        return JsonMessage.getString(500, "保存数据异常:" + e.getMessage());
    }
}
Also used : NodeProjectInfoModel(io.jpom.model.data.NodeProjectInfoModel) RunMode(io.jpom.model.RunMode)

Aggregations

NodeProjectInfoModel (io.jpom.model.data.NodeProjectInfoModel)40 File (java.io.File)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 JSONObject (com.alibaba.fastjson.JSONObject)11 JSONArray (com.alibaba.fastjson.JSONArray)7 AgentWhitelist (io.jpom.model.data.AgentWhitelist)6 AbstractProjectCommander (io.jpom.common.commander.AbstractProjectCommander)5 Charset (java.nio.charset.Charset)5 DiffFileVo (io.jpom.controller.manage.vo.DiffFileVo)4 ConsoleService (io.jpom.service.manage.ConsoleService)4 CollStreamUtil (cn.hutool.core.collection.CollStreamUtil)3 CollUtil (cn.hutool.core.collection.CollUtil)3 Convert (cn.hutool.core.convert.Convert)3 FileUtil (cn.hutool.core.io.FileUtil)3 ThreadUtil (cn.hutool.core.thread.ThreadUtil)3 ArrayUtil (cn.hutool.core.util.ArrayUtil)3 BooleanUtil (cn.hutool.core.util.BooleanUtil)3 StrUtil (cn.hutool.core.util.StrUtil)3 SecureUtil (cn.hutool.crypto.SecureUtil)3 ServletUtil (cn.hutool.extra.servlet.ServletUtil)3