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

The following examples show how to use cn.hutool.core.util.StrUtil#startWith() . 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: LinuxSystemCommander.java    From Jpom with MIT License 6 votes vote down vote up
@Override
public String stopService(String serviceName) {
    if (StrUtil.startWith(serviceName, StrUtil.SLASH)) {
        String ps = getPs(serviceName);
        List<String> list = StrUtil.splitTrim(ps, StrUtil.LF);
        if (list == null || list.isEmpty()) {
            return "stop";
        }
        String s = list.get(0);
        list = StrUtil.splitTrim(s, StrUtil.SPACE);
        if (list == null || list.size() < 2) {
            return "stop";
        }
        File file = new File(SystemUtil.getUserInfo().getHomeDir());
        int pid = Convert.toInt(list.get(1), 0);
        if (pid <= 0) {
            return "error stop";
        }
        return kill(file, pid);
    }
    String format = StrUtil.format("service {} stop", serviceName);
    return CommandUtil.execSystemCommand(format);
}
 
Example 2
Source File: Subscribe.java    From netty-learning-example with Apache License 2.0 6 votes vote down vote up
private boolean validTopicFilter(List<MqttTopicSubscription> topicSubscriptions) {
    for (MqttTopicSubscription topicSubscription : topicSubscriptions) {
        String topicFilter = topicSubscription.topicName();
        // 以#或+符号开头的、以/符号结尾的订阅按非法订阅处理, 这里没有参考标准协议
        if (StrUtil.startWith(topicFilter, '+') || StrUtil.endWith(topicFilter, '/')) {
            return false;
        }
        if (StrUtil.contains(topicFilter, '#')) {
            // 如果出现多个#符号的订阅按非法订阅处理
            if (StrUtil.count(topicFilter, '#') > 1) {
                return false;
            }
        }
        if (StrUtil.contains(topicFilter, '+')) {
            //如果+符号和/+字符串出现的次数不等的情况按非法订阅处理
            if (StrUtil.count(topicFilter, '+') != StrUtil.count(topicFilter, "/+")) {
                return false;
            }

        }

    }
    return true;
}
 
Example 3
Source File: ServletUtils.java    From datax-web with MIT License 5 votes vote down vote up
/**
 * 从请求对象中扩展参数数据,格式:JSON 或  param_ 开头的参数
 *
 * @param request 请求对象
 * @return 返回Map对象
 */
public static Map<String, Object> getExtParams(ServletRequest request) {
    Map<String, Object> paramMap = null;
    String params = StrUtil.trim(request.getParameter(DEFAULT_PARAMS_PARAM));
    if (StrUtil.isNotBlank(params) && StrUtil.startWith(params, "{")) {
        paramMap = (Map) JSONUtil.parseObj(params);
    } else {
        paramMap = getParametersStartingWith(ServletUtils.getRequest(), DEFAULT_PARAM_PREFIX_PARAM);
    }
    return paramMap;
}
 
Example 4
Source File: LinuxSystemCommander.java    From Jpom with MIT License 5 votes vote down vote up
@Override
public boolean getServiceStatus(String serviceName) {
    if (StrUtil.startWith(serviceName, StrUtil.SLASH)) {
        String ps = getPs(serviceName);
        return StrUtil.isNotEmpty(ps);
    }
    String format = StrUtil.format("service {} status", serviceName);
    String result = CommandUtil.execSystemCommand(format);
    return StrUtil.containsIgnoreCase(result, "RUNNING");
}
 
Example 5
Source File: LinuxSystemCommander.java    From Jpom with MIT License 5 votes vote down vote up
@Override
public String startService(String serviceName) {
    if (StrUtil.startWith(serviceName, StrUtil.SLASH)) {
        try {
            CommandUtil.asyncExeLocalCommand(new File(SystemUtil.getUserInfo().getHomeDir()), serviceName);
            return "ok";
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("执行异常", e);
            return "执行异常:" + e.getMessage();
        }
    }
    String format = StrUtil.format("service {} start", serviceName);
    return CommandUtil.execSystemCommand(format);
}
 
Example 6
Source File: BuildService.java    From Jpom with MIT License 5 votes vote down vote up
public boolean checkNode(String nodeId) {
    List<BuildModel> list = list();
    if (list == null || list.isEmpty()) {
        return false;
    }
    for (BuildModel buildModel : list) {
        if (buildModel.getReleaseMethod() == BuildModel.ReleaseMethod.Project.getCode()) {
            String releaseMethodDataId = buildModel.getReleaseMethodDataId();
            if (StrUtil.startWith(releaseMethodDataId, nodeId + ":")) {
                return true;
            }
        }
    }
    return false;
}
 
Example 7
Source File: SshInstallAgentController.java    From Jpom with MIT License 4 votes vote down vote up
@RequestMapping(value = "installAgentSubmit.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
@Feature(method = MethodFeature.INSTALL)
@OptLog(UserOperateLogV1.OptType.SshInstallAgent)
public String installAgentSubmit(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id,
                                 @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "节点数据") String nodeData,
                                 @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "安装路径") String path) throws Exception {
    // 判断输入的节点信息
    Object object = getNodeModel(nodeData);
    if (object instanceof JsonMessage) {
        return object.toString();
    }
    NodeModel nodeModel = (NodeModel) object;
    //
    SshModel sshModel = sshService.getItem(id);
    Objects.requireNonNull(sshModel, "没有找到对应ssh");
    //
    String tempFilePath = ServerConfigBean.getInstance().getUserTempPath().getAbsolutePath();
    MultipartFileBuilder cert = createMultipart().addFieldName("file").setSavePath(tempFilePath);
    String filePath = cert.save();
    //
    File outFle = FileUtil.file(tempFilePath, Type.Agent.name() + "_" + IdUtil.fastSimpleUUID());
    try {
        try (ZipFile zipFile = new ZipFile(filePath)) {
            // 判断文件是否正确
            ZipEntry sh = zipFile.getEntry(Type.Agent.name() + ".sh");
            ZipEntry lib = zipFile.getEntry("lib" + StrUtil.SLASH);
            if (sh == null || null == lib || !lib.isDirectory()) {
                return JsonMessage.getString(405, "不能jpom 插件包");
            }
            ZipUtil.unzip(zipFile, outFle);
        }
        // 获取上传的tag
        File shFile = FileUtil.file(outFle, Type.Agent.name() + ".sh");
        List<String> lines = FileUtil.readLines(shFile, CharsetUtil.CHARSET_UTF_8);
        String tag = null;
        for (String line : lines) {
            line = line.trim();
            if (StrUtil.startWith(line, "Tag=\"") && StrUtil.endWith(line, "\"")) {
                tag = line.substring(5, line.length() - 1);
                break;
            }
        }
        if (StrUtil.isEmpty(tag)) {
            return JsonMessage.getString(405, "管理命令中不存在tag");
        }
        //  读取授权信息
        File configFile = FileUtil.file(outFle, "extConfig.yml");
        if (configFile.exists()) {
            List<Map<String, Object>> load = YmlUtil.load(configFile);
            Map<String, Object> map = load.get(0);
            Object user = map.get(ConfigBean.AUTHORIZE_USER_KEY);
            nodeModel.setLoginName(Convert.toStr(user, ""));
            //
            Object pwd = map.get(ConfigBean.AUTHORIZE_PWD_KEY);
            nodeModel.setLoginPwd(Convert.toStr(pwd, ""));
        }
        // 查询远程是否运行
        if (sshService.checkSshRun(sshModel, tag)) {
            return JsonMessage.getString(300, "对应服务器中已经存在Jpom 插件端,不需要再次安装啦");
        }
        // 上传文件到服务器
        sshService.uploadDir(sshModel, path, outFle);
        //
        String shPtah = FileUtil.normalize(path + "/" + Type.Agent.name() + ".sh");
        String command = StrUtil.format("sh {} start upgrade", shPtah);
        String result = sshService.exec(sshModel, command);
        // 休眠10秒
        Thread.sleep(10 * 1000);
        if (StrUtil.isEmpty(nodeModel.getLoginName()) || StrUtil.isEmpty(nodeModel.getLoginPwd())) {
            String error = this.getAuthorize(sshModel, nodeModel, path);
            if (error != null) {
                return error;
            }
        }
        nodeModel.setOpenStatus(true);
        // 绑定关系
        nodeModel.setSshId(sshModel.getId());
        nodeService.addItem(nodeModel);
        //
        return JsonMessage.getString(200, "操作成功:" + result);
    } finally {
        // 清理资源
        FileUtil.del(filePath);
        FileUtil.del(outFle);
    }
}