Java Code Examples for cn.hutool.core.util.StrUtil#split()

The following examples show how to use cn.hutool.core.util.StrUtil#split() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 获取jdk 版本
 *
 * @param path jdk 路径
 * @return 获取成功返回版本号
 */
public static String getJdkVersion(String path) {
    String newPath = getJdkJavaPath(path, false);
    if (path.contains(StrUtil.SPACE)) {
        newPath = String.format("\"%s\"", newPath);
    }
    String command = CommandUtil.execSystemCommand(newPath + "  -version");
    String[] split = StrUtil.split(command, StrUtil.LF);
    if (split == null || split.length <= 0) {
        return null;
    }
    String[] strings = StrUtil.split(split[0], "\"");
    if (strings == null || strings.length <= 1) {
        return null;
    }
    return strings[1];
}
 
Example 2
Source File: BaseServerController.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 处理分页的时间字段
 *
 * @param page    分页
 * @param entity  条件
 * @param colName 字段名称
 */
protected void doPage(Page page, Entity entity, String colName) {
    String time = getParameter("time");
    colName = colName.toUpperCase();
    page.addOrder(new Order(colName, Direction.DESC));
    // 时间
    if (StrUtil.isNotEmpty(time)) {
        String[] val = StrUtil.split(time, "~");
        if (val.length == 2) {
            DateTime startDateTime = DateUtil.parse(val[0], DatePattern.NORM_DATETIME_FORMAT);
            entity.set(colName, ">= " + startDateTime.getTime());

            DateTime endDateTime = DateUtil.parse(val[1], DatePattern.NORM_DATETIME_FORMAT);
            if (startDateTime.equals(endDateTime)) {
                endDateTime = DateUtil.endOfDay(endDateTime);
            }
            // 防止字段重复
            entity.set(colName + " ", "<= " + endDateTime.getTime());
        }
    }
}
 
Example 3
Source File: JvmUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 判断命令行是否为jpom 标识
 *
 * @param commandLine 命令行
 * @param tag         标识
 * @return true
 */
public static boolean checkCommandLineIsJpom(String commandLine, String tag) {
    if (StrUtil.isEmpty(commandLine)) {
        return false;
    }
    String[] split = StrUtil.split(commandLine, StrUtil.SPACE);
    String appTag = String.format("-%s=%s", JvmUtil.POM_PID_TAG, tag);
    String appTag2 = String.format("-%s=%s", JvmUtil.OLD_JPOM_PID_TAG, tag);
    String appTag3 = String.format("-%s=%s", JvmUtil.OLD2_JPOM_PID_TAG, tag);
    for (String item : split) {
        if (StrUtil.equalsAnyIgnoreCase(item, appTag, appTag2, appTag3)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: HutoolController.java    From mall-learning with Apache License 2.0 6 votes vote down vote up
@ApiOperation("CollUtil使用:集合工具类")
@GetMapping("/collUtil")
public CommonResult collUtil() {
    //数组转换为列表
    String[] array = new String[]{"a", "b", "c", "d", "e"};
    List<String> list = CollUtil.newArrayList(array);
    //join:数组转字符串时添加连接符号
    String joinStr = CollUtil.join(list, ",");
    LOGGER.info("collUtil join:{}", joinStr);
    //将以连接符号分隔的字符串再转换为列表
    List<String> splitList = StrUtil.split(joinStr, ',');
    LOGGER.info("collUtil split:{}", splitList);
    //创建新的Map、Set、List
    HashMap<Object, Object> newMap = CollUtil.newHashMap();
    HashSet<Object> newHashSet = CollUtil.newHashSet();
    ArrayList<Object> newList = CollUtil.newArrayList();
    //判断列表是否为空
    CollUtil.isEmpty(list);
    CollUtil.isNotEmpty(list);
    return CommonResult.success(null, "操作成功");
}
 
Example 5
Source File: MutiStrFactory.java    From WebStack-Guns with MIT License 6 votes vote down vote up
/**
 * 解析一个组合字符串(例如:  "1:启用;2:禁用;3:冻结"  这样的字符串)
 *
 * @author fengshuonan
 * @Date 2017/4/27 16:44
 */
public static List<Map<String, String>> parseKeyValue(String mutiString) {
    if (ToolUtil.isEmpty(mutiString)) {
        return new ArrayList<>();
    } else {
        ArrayList<Map<String, String>> results = new ArrayList<>();
        String[] items = StrUtil.split(StrUtil.removeSuffix(mutiString, ITEM_SPLIT), ITEM_SPLIT);
        for (String item : items) {
            String[] attrs = item.split(ATTR_SPLIT);
            HashMap<String, String> itemMap = new HashMap<>();
            itemMap.put(MUTI_STR_CODE, attrs[0]);
            itemMap.put(MUTI_STR_NAME, attrs[1]);
            itemMap.put(MUTI_STR_NUM, attrs[2]);
            results.add(itemMap);
        }
        return results;
    }
}
 
Example 6
Source File: ProjectInfoModel.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 拼接java 执行的jar路径
 *
 * @param projectInfoModel 项目
 * @return classpath 或者 jar
 */
public static String getClassPathLib(ProjectInfoModel projectInfoModel) {
    List<File> files = listJars(projectInfoModel);
    if (files.size() <= 0) {
        return "";
    }
    // 获取lib下面的所有jar包
    StringBuilder classPath = new StringBuilder();
    RunMode runMode = projectInfoModel.getRunMode();
    int len = files.size();
    if (runMode == RunMode.ClassPath) {
        classPath.append("-classpath ");
    } else if (runMode == RunMode.Jar || runMode == RunMode.JarWar) {
        classPath.append("-jar ");
        // 只取一个jar文件
        len = 1;
    } else if (runMode == RunMode.JavaExtDirsCp) {
        classPath.append("-Djava.ext.dirs=");
        String javaExtDirsCp = projectInfoModel.getJavaExtDirsCp();
        String[] split = StrUtil.split(javaExtDirsCp, StrUtil.COLON);
        if (ArrayUtil.isEmpty(split)) {
            classPath.append(". -cp ");
        } else {
            classPath.append(split[0]).append(" -cp ");
            if (split.length > 1) {
                classPath.append(split[1]).append(FileUtils.getJarSeparator());
            }
        }
    } else {
        return StrUtil.EMPTY;
    }
    for (int i = 0; i < len; i++) {
        File file = files.get(i);
        classPath.append(file.getAbsolutePath());
        if (i != len - 1) {
            classPath.append(FileUtils.getJarSeparator());
        }
    }
    return classPath.toString();
}
 
Example 7
Source File: AbstractProjectCommander.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 尝试jps 中查看进程id
 *
 * @param tag 进程标识
 * @return 运行标识
 */
private String getJpsStatus(String tag) {
    String execSystemCommand = CommandUtil.execSystemCommand("jps -mv");
    List<String> list = StrSpliter.splitTrim(execSystemCommand, StrUtil.LF, true);
    for (String item : list) {
        if (JvmUtil.checkCommandLineIsJpom(item, tag)) {
            String[] split = StrUtil.split(item, StrUtil.SPACE);
            return StrUtil.format("{}:{}", AbstractProjectCommander.RUNNING_TAG, split[0]);
        }
    }
    return AbstractProjectCommander.STOP_TAG;
}
 
Example 8
Source File: ReleaseManage.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 发布项目
 *
 * @param afterOpt 后续操作
 */
private void doProject(AfterOpt afterOpt, boolean clearOld) {
    String releaseMethodDataId = this.baseBuildModule.getReleaseMethodDataId();
    String[] strings = StrUtil.split(releaseMethodDataId, ":");
    if (strings == null || strings.length != 2) {
        throw new JpomRuntimeException(releaseMethodDataId + " error");
    }
    NodeService nodeService = SpringUtil.getBean(NodeService.class);
    NodeModel nodeModel = nodeService.getItem(strings[0]);
    Objects.requireNonNull(nodeModel, "节点不存在");

    File zipFile = BuildUtil.isDirPackage(this.resultFile);
    boolean unZip = true;
    if (zipFile == null) {
        zipFile = this.resultFile;
        unZip = false;
    }
    JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(zipFile,
            strings[1],
            unZip,
            afterOpt,
            nodeModel, this.userModel, clearOld);
    if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
        this.log("发布项目包成功:" + jsonMessage.toString());
    } else {
        throw new JpomRuntimeException("发布项目包失败:" + jsonMessage.toString());
    }
}
 
Example 9
Source File: ManageEditProjectController.java    From Jpom with MIT License 4 votes vote down vote up
/**
 * 基础检查
 *
 * @param projectInfo        项目实体
 * @param whitelistDirectory 白名单
 * @param previewData        预检查数据
 * @return null 检查正常
 */
private String checkParameter(ProjectInfoModel projectInfo, String whitelistDirectory, boolean previewData) {
    String id = projectInfo.getId();
    if (StrUtil.isEmptyOrUndefined(id)) {
        return JsonMessage.getString(400, "项目id不能为空");
    }
    if (!StringUtil.isGeneral(id, 2, 20)) {
        return JsonMessage.getString(401, "项目id 长度范围2-20(英文字母 、数字和下划线)");
    }
    if (JpomApplication.SYSTEM_ID.equals(id)) {
        return JsonMessage.getString(401, "项目id " + JpomApplication.SYSTEM_ID + " 关键词被系统占用");
    }
    // 防止和Jpom冲突
    if (StrUtil.isNotEmpty(ConfigBean.getInstance().applicationTag) && ConfigBean.getInstance().applicationTag.equalsIgnoreCase(id)) {
        return JsonMessage.getString(401, "当前项目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) {
        if (StrUtil.isEmpty(projectInfo.getMainClass())) {
            return JsonMessage.getString(401, "ClassPath、JavaExtDirsCp 模式 MainClass必填");
        }
    } else if (runMode1 == RunMode.Jar || runMode1 == RunMode.JarWar) {
        projectInfo.setMainClass("");
    }
    if (runMode1 == RunMode.JavaExtDirsCp) {
        if (StrUtil.isEmpty(projectInfo.getJavaExtDirsCp())) {
            return JsonMessage.getString(401, "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 {
                return JsonMessage.getString(401, "请选择正确的项目路径,或者还没有配置白名单");
            }
        }
    }

    String lib = projectInfo.getLib();
    if (StrUtil.isEmpty(lib) || StrUtil.SLASH.equals(lib) || Validator.isChinese(lib)) {
        return JsonMessage.getString(401, "项目Jar路径不能为空,不能为顶级目录,不能包含中文");
    }
    if (!checkPathSafe(lib)) {
        return JsonMessage.getString(401, "项目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.split(javaCopyIds, StrUtil.COMMA);
            List<ProjectInfoModel.JavaCopyItem> javaCopyItemList = new ArrayList<>(split.length);
            for (String copyId : split) {
                String jvm = getParameter("jvm_" + copyId);
                String args = getParameter("args_" + copyId);
                //
                ProjectInfoModel.JavaCopyItem javaCopyItem = new ProjectInfoModel.JavaCopyItem();
                javaCopyItem.setId(copyId);
                javaCopyItem.setParendId(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);
    }
    return null;
}
 
Example 10
Source File: OutGivingProjectEditController.java    From Jpom with MIT License 4 votes vote down vote up
/**
 * 处理页面数据
 *
 * @param outGivingModel 分发实体
 * @param edit           是否为编辑模式
 * @return json
 */
private String doData(OutGivingModel outGivingModel, boolean edit) {
    outGivingModel.setName(getParameter("name"));
    if (StrUtil.isEmpty(outGivingModel.getName())) {
        return JsonMessage.getString(405, "分发名称不能为空");
    }
    String reqId = getParameter("reqId");
    List<NodeModel> nodeModelList = nodeService.getNodeModel(reqId);
    if (nodeModelList == null) {
        return JsonMessage.getString(401, "当前页面请求超时");
    }
    //
    String afterOpt = getParameter("afterOpt");
    AfterOpt afterOpt1 = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(afterOpt, 0));
    if (afterOpt1 == null) {
        return JsonMessage.getString(400, "请选择分发后的操作");
    }
    outGivingModel.setAfterOpt(afterOpt1.getCode());
    Object object = getDefData(outGivingModel, edit);
    if (object instanceof String) {
        return object.toString();
    }
    JSONObject defData = (JSONObject) object;
    UserModel userModel = getUser();
    //
    List<OutGivingModel> outGivingModels = outGivingServer.list();
    List<OutGivingNodeProject> outGivingNodeProjects = new ArrayList<>();
    OutGivingNodeProject outGivingNodeProject;
    //
    Iterator<NodeModel> iterator = nodeModelList.iterator();
    Map<NodeModel, JSONObject> cache = new HashMap<>(nodeModelList.size());
    while (iterator.hasNext()) {
        NodeModel nodeModel = iterator.next();
        String add = getParameter("add_" + nodeModel.getId());
        if (!nodeModel.getId().equals(add)) {
            iterator.remove();
            continue;
        }
        // 判断项目是否已经被使用过啦
        if (outGivingModels != null) {
            for (OutGivingModel outGivingModel1 : outGivingModels) {
                if (outGivingModel1.getId().equalsIgnoreCase(outGivingModel.getId())) {
                    continue;
                }
                if (outGivingModel1.checkContains(nodeModel.getId(), outGivingModel.getId())) {
                    return JsonMessage.getString(405, "已经存在相同的分发项目:" + outGivingModel.getId());
                }
            }
        }
        outGivingNodeProject = outGivingModel.getNodeProject(nodeModel.getId(), outGivingModel.getId());
        if (outGivingNodeProject == null) {
            outGivingNodeProject = new OutGivingNodeProject();
        }
        outGivingNodeProject.setNodeId(nodeModel.getId());
        outGivingNodeProject.setProjectId(outGivingModel.getId());
        outGivingNodeProjects.add(outGivingNodeProject);
        // 检查数据
        JSONObject allData = (JSONObject) defData.clone();
        String token = getParameter(StrUtil.format("{}_token", nodeModel.getId()));
        allData.put("token", token);
        String jvm = getParameter(StrUtil.format("{}_jvm", nodeModel.getId()));
        allData.put("jvm", jvm);
        String args = getParameter(StrUtil.format("{}_args", nodeModel.getId()));
        allData.put("args", args);
        // 项目副本
        String javaCopyIds = getParameter(StrUtil.format("{}_javaCopyIds", nodeModel.getId()));
        allData.put("javaCopyIds", javaCopyIds);
        if (StrUtil.isNotEmpty(javaCopyIds)) {
            String[] split = StrUtil.split(javaCopyIds, StrUtil.COMMA);
            for (String copyId : split) {
                String copyJvm = getParameter(StrUtil.format("{}_jvm_{}", nodeModel.getId(), copyId));
                String copyArgs = getParameter(StrUtil.format("{}_args_{}", nodeModel.getId(), copyId));
                allData.put("jvm_" + copyId, copyJvm);
                allData.put("args_" + copyId, copyArgs);
            }
        }
        JsonMessage<String> jsonMessage = sendData(nodeModel, userModel, allData, false);
        if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
            return JsonMessage.getString(406, nodeModel.getName() + "节点失败:" + jsonMessage.getMsg());
        }
        cache.put(nodeModel, allData);
    }
    // 删除已经删除的项目
    String error = deleteProject(outGivingModel, outGivingNodeProjects, userModel);
    if (error != null) {
        return error;
    }
    outGivingModel.setOutGivingNodeProjectList(outGivingNodeProjects);
    outGivingModel.setTempCacheMap(cache);
    return null;
}
 
Example 11
Source File: LocalAutoConfigure.java    From zuihou-admin-boot with Apache License 2.0 2 votes vote down vote up
/**
 * 为上传的文件生成随机名称
 *
 * @param originalName 文件的原始名称,主要用来获取文件的后缀名
 * @return
 */
private String randomFileName(String originalName) {
    String[] ext = StrUtil.split(originalName, ".");
    return UUID.randomUUID().toString() + StrPool.DOT + ext[ext.length - 1];
}
 
Example 12
Source File: LocalAutoConfigure.java    From zuihou-admin-cloud with Apache License 2.0 2 votes vote down vote up
/**
 * 为上传的文件生成随机名称
 *
 * @param originalName 文件的原始名称,主要用来获取文件的后缀名
 * @return
 */
private String randomFileName(String originalName) {
    String[] ext = StrUtil.split(originalName, ".");
    return UUID.randomUUID().toString() + StrPool.DOT + ext[ext.length - 1];
}