Search in sources :

Example 1 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class JpomManifest method getScriptFile.

/**
 * 获取当前的管理名文件
 *
 * @return file
 */
public static File getScriptFile() {
    File runPath = getRunPath().getParentFile().getParentFile();
    String type = JpomApplication.getAppType().name();
    File scriptFile = FileUtil.file(runPath, StrUtil.format("{}.{}", type, CommandUtil.SUFFIX));
    if (!scriptFile.exists() || scriptFile.isDirectory()) {
        throw new JpomRuntimeException("当前服务中没有命令脚本:" + StrUtil.format("{}.{}", type, CommandUtil.SUFFIX));
    }
    return scriptFile;
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class JpomManifest method releaseJar.

/**
 * 发布包到对应运行路径
 *
 * @param path    文件路径
 * @param version 新版本号
 */
public static void releaseJar(String path, String version) {
    File runFile = getRunPath();
    File runPath = runFile.getParentFile();
    if (!runPath.isDirectory()) {
        throw new JpomRuntimeException(runPath.getAbsolutePath() + " error");
    }
    String upgrade = FileUtil.file(runPath, ConfigBean.UPGRADE).getAbsolutePath();
    JSONObject jsonObject = null;
    try {
        jsonObject = (JSONObject) JsonFileUtil.readJson(upgrade);
    } catch (FileNotFoundException ignored) {
    }
    if (jsonObject == null) {
        jsonObject = new JSONObject();
    }
    jsonObject.put("beforeJar", runFile.getName());
    // 如果升级的版本号一致
    if (StrUtil.equals(version, JpomManifest.getInstance().getVersion())) {
        version = StrUtil.format("{}_{}", version, System.currentTimeMillis());
    }
    String newFile;
    File to;
    while (true) {
        newFile = JpomApplication.getAppType().name() + "-" + version + FileUtil.JAR_FILE_EXT;
        to = FileUtil.file(runPath, newFile);
        if (FileUtil.equals(to, runFile)) {
            version = StrUtil.format("{}_{}", version, RandomUtil.randomInt(1, 100));
            continue;
        }
        break;
    }
    // 
    FileUtil.move(new File(path), to, true);
    jsonObject.put("newJar", newFile);
    jsonObject.put("updateTime", new DateTime().toString());
    // 更新管理命令
    List<String> newData = new LinkedList<>();
    // 
    String typeName = JpomApplication.getAppType().name().toLowerCase();
    final String[] oldName = new String[] { typeName + ".log" };
    final boolean[] logBack = { true };
    File scriptFile = getScriptFile();
    Charset charset = ExtConfigBean.getInstance().getConsoleLogCharset();
    String finalNewFile = newFile;
    FileUtil.readLines(scriptFile, charset, (LineHandler) line -> {
        if (!line.startsWith(String.valueOf(StrUtil.C_TAB)) && !line.startsWith(String.valueOf(StrUtil.C_SPACE))) {
            if (StrUtil.containsAny(line, "RUNJAR=")) {
                if ("sh".equals(CommandUtil.SUFFIX)) {
                    newData.add(StrUtil.format("RUNJAR=\"{}\"", finalNewFile));
                } else if ("bat".equals(CommandUtil.SUFFIX)) {
                    newData.add(StrUtil.format("set RUNJAR={}", finalNewFile));
                } else {
                    newData.add(line);
                }
            } else if (SystemUtil.getOsInfo().isWindows()) {
                if (StrUtil.containsAny(line, "set LogName=")) {
                    oldName[0] = CharSequenceUtil.splitToArray(line, "=")[0];
                    newData.add(StrUtil.format("set LogName={}_{}.log", typeName, System.currentTimeMillis()));
                } else if (StrUtil.containsAny(line, "set LogBack=")) {
                    logBack[0] = Convert.toBool(CharSequenceUtil.splitToArray(line, "=")[1], true);
                    newData.add(line);
                } else {
                    newData.add(line);
                }
            } else {
                newData.add(line);
            }
        } else {
            newData.add(line);
        }
    });
    // 新增升级次数 @author jzy 2021-08-04
    jsonObject.put("upgradeCount", jsonObject.getIntValue("upgradeCount"));
    jsonObject.put("oldLogName", oldName[0]);
    jsonObject.put("logBack", logBack[0]);
    // 
    JsonFileUtil.saveJson(upgrade, jsonObject);
    FileUtil.writeLines(newData, scriptFile, charset);
}
Also used : GlobalHeaders(cn.hutool.http.GlobalHeaders) Manifest(java.util.jar.Manifest) DateUtil(cn.hutool.core.date.DateUtil) SecureUtil(cn.hutool.crypto.SecureUtil) JarClassLoader(cn.hutool.core.lang.JarClassLoader) CommandUtil(io.jpom.util.CommandUtil) URL(java.net.URL) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) JarFile(java.util.jar.JarFile) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JsonMessage(cn.jiangzeyin.common.JsonMessage) Charset(java.nio.charset.Charset) cn.hutool.core.util(cn.hutool.core.util) Header(cn.hutool.http.Header) DateTime(cn.hutool.core.date.DateTime) ExtConfigBean(io.jpom.system.ExtConfigBean) ZipFile(java.util.zip.ZipFile) ManifestUtil(cn.hutool.core.io.ManifestUtil) LinkedList(java.util.LinkedList) ZipEntry(java.util.zip.ZipEntry) BetweenFormatter(cn.hutool.core.date.BetweenFormatter) Predicate(java.util.function.Predicate) LineHandler(cn.hutool.core.io.LineHandler) IOException(java.io.IOException) SystemUtil(cn.hutool.system.SystemUtil) Attributes(java.util.jar.Attributes) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Tuple(cn.hutool.core.lang.Tuple) List(java.util.List) JSON(com.alibaba.fastjson.JSON) JpomApplication(io.jpom.JpomApplication) Convert(cn.hutool.core.convert.Convert) ConfigBean(io.jpom.system.ConfigBean) Optional(java.util.Optional) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) CharSequenceUtil(cn.hutool.core.text.CharSequenceUtil) JsonFileUtil(io.jpom.util.JsonFileUtil) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) JSONObject(com.alibaba.fastjson.JSONObject) JpomRuntimeException(io.jpom.system.JpomRuntimeException) FileNotFoundException(java.io.FileNotFoundException) Charset(java.nio.charset.Charset) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) DateTime(cn.hutool.core.date.DateTime) LinkedList(java.util.LinkedList)

Example 3 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class TomcatManageService method tomcatCmd.

/**
 * 访问tomcat Url
 *
 * @param tomcatInfoModel tomcat信息
 * @param cmd             命令
 * @return 访问结果
 */
private String tomcatCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
    String url = String.format("http://127.0.0.1:%d/jpomAgent/%s", tomcatInfoModel.getPort(), cmd);
    HttpRequest httpRequest = new HttpRequest(url);
    // 设置超时时间为3秒
    httpRequest.setConnectionTimeout(3000);
    String body = "";
    try {
        HttpResponse httpResponse = httpRequest.execute();
        if (httpResponse.isOk()) {
            body = httpResponse.body();
        }
        if (httpResponse.getStatus() == HttpStatus.HTTP_NOT_FOUND) {
            // 没有插件
            tomcatInfoModel.initTomcat();
            throw new JpomRuntimeException("tomcat 未初始化,已经重新初始化请稍后再试");
        }
    } catch (JpomRuntimeException jpom) {
        throw jpom;
    } catch (Exception ignored) {
    }
    return body;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JpomRuntimeException(io.jpom.system.JpomRuntimeException) HttpResponse(cn.hutool.http.HttpResponse) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 4 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class BaseDbCommonService method update.

/**
 * 修改数据
 *
 * @param entity 要修改的数据
 * @param where  条件
 * @return 影响行数
 */
public int update(Entity entity, Entity where) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return 0;
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    if (where.isEmpty()) {
        throw new JpomRuntimeException("没有更新条件");
    }
    entity.setTableName(tableName);
    where.setTableName(tableName);
    try {
        return db.update(entity, where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 5 with JpomRuntimeException

use of io.jpom.system.JpomRuntimeException in project Jpom by dromara.

the class BaseDbCommonService method del.

/**
 * 根据条件删除
 *
 * @param where 条件
 * @return 影响行数
 */
public int del(Entity where) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return 0;
    }
    where.setTableName(tableName);
    if (where.isEmpty()) {
        throw new JpomRuntimeException("没有删除条件");
    }
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        return db.del(where);
    } catch (Exception e) {
        throw warpException(e);
    }
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Aggregations

JpomRuntimeException (io.jpom.system.JpomRuntimeException)16 JSONObject (com.alibaba.fastjson.JSONObject)7 File (java.io.File)6 InputStream (java.io.InputStream)4 BetweenFormatter (cn.hutool.core.date.BetweenFormatter)2 DateUtil (cn.hutool.core.date.DateUtil)2 FileUtil (cn.hutool.core.io.FileUtil)2 LineHandler (cn.hutool.core.io.LineHandler)2 Tuple (cn.hutool.core.lang.Tuple)2 SecureUtil (cn.hutool.crypto.SecureUtil)2 Db (cn.hutool.db.Db)2 JsonMessage (cn.jiangzeyin.common.JsonMessage)2 JSON (com.alibaba.fastjson.JSON)2 AfterOpt (io.jpom.model.AfterOpt)2 NodeModel (io.jpom.model.data.NodeModel)2 JarFile (java.util.jar.JarFile)2 ZipFile (java.util.zip.ZipFile)2 JdbcSQLNonTransientException (org.h2.jdbc.JdbcSQLNonTransientException)2 CollUtil (cn.hutool.core.collection.CollUtil)1 Convert (cn.hutool.core.convert.Convert)1