Search in sources :

Example 1 with JarClassLoader

use of cn.hutool.core.lang.JarClassLoader in project Jpom by dromara.

the class JpomManifest method checkJpomJar.

/**
 * 检查是否为jpom包
 *
 * @param path        路径
 * @param type        类型
 * @param checkRepeat 是否检查版本重复
 * @return 结果消息
 * @see Type#getApplicationClass()
 */
public static JsonMessage<Tuple> checkJpomJar(String path, Type type, boolean checkRepeat) {
    File jarFile = new File(path);
    Tuple jarVersion = getJarVersion(jarFile);
    if (jarVersion == null) {
        return new JsonMessage<>(405, "jar 包文件不合法");
    }
    try (JarFile jarFile1 = new JarFile(jarFile)) {
        // Manifest manifest = jarFile1.getManifest();
        // Attributes attributes = manifest.getMainAttributes();
        String mainClass = jarVersion.get(2);
        if (mainClass == null) {
            return new JsonMessage<>(405, "清单文件中没有找到对应的MainClass属性");
        }
        try (JarClassLoader jarClassLoader = JarClassLoader.load(jarFile)) {
            jarClassLoader.loadClass(mainClass);
        } catch (ClassNotFoundException notFound) {
            return new JsonMessage<>(405, "中没有找到对应的MainClass:" + mainClass);
        }
        String applicationClass = type.getApplicationClass();
        ZipEntry entry = jarFile1.getEntry(StrUtil.format("BOOT-INF/classes/{}.class", StrUtil.replace(applicationClass, ".", StrUtil.SLASH)));
        if (entry == null) {
            return new JsonMessage<>(405, "此包不是Jpom【" + type.name() + "】包");
        }
        String version = jarVersion.get(0);
        String timeStamp = jarVersion.get(1);
        String minVersion = jarVersion.get(4);
        if (StrUtil.hasEmpty(version, timeStamp, minVersion)) {
            return new JsonMessage<>(405, "此包没有版本号、打包时间、最小兼容版本");
        }
        if (checkRepeat) {
            // 
            JpomManifest jpomManifest = JpomManifest.getInstance();
            if (StrUtil.equals(version, jpomManifest.getVersion()) && StrUtil.equals(timeStamp, jpomManifest.getTimeStamp())) {
                return new JsonMessage<>(405, "新包和正在运行的包一致");
            }
            if (StrUtil.compareVersion(jpomManifest.getVersion(), minVersion) < 0) {
                return new JsonMessage<>(405, StrUtil.format("当前程序版本 {} 新版程序最低兼容 {} 不能直接升级", jpomManifest.getVersion(), minVersion));
            }
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("解析jar", e);
        return new JsonMessage<>(500, " 解析错误:" + e.getMessage());
    }
    return new JsonMessage<>(200, "", jarVersion);
}
Also used : JsonMessage(cn.jiangzeyin.common.JsonMessage) ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Tuple(cn.hutool.core.lang.Tuple) JpomRuntimeException(io.jpom.system.JpomRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JarClassLoader(cn.hutool.core.lang.JarClassLoader)

Example 2 with JarClassLoader

use of cn.hutool.core.lang.JarClassLoader in project Jpom by dromara.

the class AbstractProjectCommander method checkJar.

private static String checkJar(File jarFile) {
    try (JarFile jarFile1 = new JarFile(jarFile)) {
        Manifest manifest = jarFile1.getManifest();
        Attributes attributes = manifest.getMainAttributes();
        String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
        if (mainClass == null) {
            return jarFile.getAbsolutePath() + "中没有找到对应的MainClass属性";
        }
        JarClassLoader jarClassLoader = JarClassLoader.load(jarFile);
        try {
            jarClassLoader.loadClass(mainClass);
        } catch (ClassNotFoundException notFound) {
            return jarFile.getAbsolutePath() + "中没有找到对应的MainClass:" + mainClass;
        }
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("解析jar", e);
        return jarFile.getAbsolutePath() + " 解析错误:" + e.getMessage();
    }
    return null;
}
Also used : Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JpomRuntimeException(io.jpom.system.JpomRuntimeException) IOException(java.io.IOException) JarClassLoader(cn.hutool.core.lang.JarClassLoader)

Example 3 with JarClassLoader

use of cn.hutool.core.lang.JarClassLoader in project Jpom by dromara.

the class AbstractProjectCommander method checkStart.

/**
 * 启动项目前基本检查
 *
 * @param nodeProjectInfoModel 项目
 * @return null 检查一切正常
 * @throws Exception 异常
 */
private String checkStart(NodeProjectInfoModel nodeProjectInfoModel, NodeProjectInfoModel.JavaCopyItem javaCopyItem) throws Exception {
    int pid = this.getPid(nodeProjectInfoModel, javaCopyItem);
    if (pid > 0) {
        return "当前程序正常运行中,不能重复启动,PID:" + pid;
    }
    String lib = nodeProjectInfoModel.allLib();
    File fileLib = new File(lib);
    File[] files = fileLib.listFiles();
    if (files == null || files.length <= 0) {
        return "项目目录没有任何文件,请先到项目文件管理中上传文件";
    }
    // 
    RunMode runMode = nodeProjectInfoModel.getRunMode();
    if (runMode == RunMode.Dsl) {
        // 
        String dslContent = nodeProjectInfoModel.getDslContent();
    } else if (runMode == RunMode.ClassPath || runMode == RunMode.JavaExtDirsCp) {
        JarClassLoader jarClassLoader = JarClassLoader.load(fileLib);
        // 判断主类
        try {
            jarClassLoader.loadClass(nodeProjectInfoModel.getMainClass());
        } catch (ClassNotFoundException notFound) {
            return "没有找到对应的MainClass:" + nodeProjectInfoModel.getMainClass();
        }
    } else if (runMode == RunMode.Jar || runMode == RunMode.JarWar) {
        List<File> fileList = NodeProjectInfoModel.listJars(nodeProjectInfoModel);
        if (fileList.size() <= 0) {
            return String.format("没有%s包,请先到文件管理中上传程序的%s", runMode.name(), runMode.name());
        }
        File jarFile = fileList.get(0);
        String checkJar = checkJar(jarFile);
        if (checkJar != null) {
            return checkJar;
        }
    } else {
        return "当前项目类型不支持启动";
    }
    // 备份日志
    backLog(nodeProjectInfoModel, javaCopyItem);
    return null;
}
Also used : RunMode(io.jpom.model.RunMode) JarFile(java.util.jar.JarFile) File(java.io.File) JarClassLoader(cn.hutool.core.lang.JarClassLoader)

Aggregations

JarClassLoader (cn.hutool.core.lang.JarClassLoader)3 JarFile (java.util.jar.JarFile)3 JpomRuntimeException (io.jpom.system.JpomRuntimeException)2 File (java.io.File)2 IOException (java.io.IOException)2 Tuple (cn.hutool.core.lang.Tuple)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 RunMode (io.jpom.model.RunMode)1 FileNotFoundException (java.io.FileNotFoundException)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1