Search in sources :

Example 1 with JarSig

use of io.nuls.client.rpc.resources.model.JarSig in project nuls by nuls-io.

the class UpgradeThread method run.

@Override
public void run() {
    if (!upgrading) {
        return;
    }
    try {
        SyncVersionRunner syncor = SyncVersionRunner.getInstance();
        this.version = syncor.getNewestVersion();
        String versionFileHash = syncor.getVersionFileHash();
        if (!VersionUtils.higherThan(version, NulsConfig.VERSION)) {
            setFailedMessage("The version is wrong!");
            return;
        }
        process.setPercentage(1);
        VersionFile versionJson = getVersionJson(syncor, versionFileHash);
        if (null == versionJson) {
            return;
        }
        process.setPercentage(5);
        process.setStatus(VersionConstant.DOWNLOADING);
        String root = this.getClass().getClassLoader().getResource("").getPath();
        root = URLDecoder.decode(root, "UTF-8");
        String newDirPath = root + "/temp/" + version;
        File newDir = new File(newDirPath);
        if (!newDir.exists()) {
            newDir.mkdirs();
        }
        String urlRoot = syncor.getRootUrl() + "/" + version;
        boolean result = this.download(urlRoot + "/bin.zip", newDirPath + "/bin.zip", versionJson.getBinSig());
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(12);
        result = this.download(urlRoot + "/conf.zip", newDirPath + "/conf.zip", versionJson.getConfSig());
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(15);
        File libsDir = new File(newDirPath + "/libs");
        if (libsDir.exists()) {
            FileUtil.deleteFolder(libsDir);
        }
        libsDir.mkdirs();
        process.setPercentage(20);
        int count = 0;
        int size = versionJson.getJarSigList().size();
        for (JarSig jarSig : versionJson.getJarSigList()) {
            File file = new File(root + "/libs/" + jarSig.getFileName());
            if (file.exists() && this.verifySig(file, Hex.decode(jarSig.getSig()))) {
                FileUtil.copyFile(file, new File(newDirPath + "/libs/" + jarSig.getFileName()));
                result = true;
            } else {
                result = this.download(urlRoot + "/libs/" + jarSig.getFileName(), newDirPath + "/libs/" + jarSig.getFileName(), jarSig.getSig());
            }
            if (!result) {
                deleteTemp(root + "/temp/");
                return;
            }
            count++;
            process.setPercentage(20 + (count * 70) / (size));
        }
        process.setStatus(VersionConstant.INSTALLING);
        String oldDirPath = root + "/temp/old";
        result = this.copy(root + "/bin", oldDirPath + "/bin");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(92);
        result = this.copy(root + "/conf", oldDirPath + "/conf");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(95);
        result = this.copy(root + "/libs", oldDirPath + "/libs");
        if (!result) {
            deleteTemp(root + "/temp/");
            return;
        }
        process.setPercentage(100);
        process.setStatus(VersionConstant.WAITING_RESTART);
    } catch (Exception e) {
        Log.error(e);
        setFailedMessage(e.getMessage());
        upgrading = false;
    }
}
Also used : VersionFile(io.nuls.client.rpc.resources.model.VersionFile) JarSig(io.nuls.client.rpc.resources.model.JarSig) SyncVersionRunner(io.nuls.client.version.SyncVersionRunner) VersionFile(io.nuls.client.rpc.resources.model.VersionFile)

Aggregations

JarSig (io.nuls.client.rpc.resources.model.JarSig)1 VersionFile (io.nuls.client.rpc.resources.model.VersionFile)1 SyncVersionRunner (io.nuls.client.version.SyncVersionRunner)1