From 3f97b0c9172a9cced9f9c071d41267c70f00d4ca Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Tue, 22 Oct 2019 10:45:28 +0800 Subject: [PATCH] =?UTF-8?q?Generate=20jar=20file=20modify=20to=20generate?= =?UTF-8?q?=20temporary=20file=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blockchain/contract/ContractJarUtils.java | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java index 50643d06..b2a3764b 100644 --- a/source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java +++ b/source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java @@ -115,7 +115,7 @@ public class ContractJarUtils { throw new IllegalStateException("Contract's chaincode is empty !!!"); } // 首先生成合约文件 - File jarFile = newJarFile(contractPath); + File jarFile = newJarTempFile(); try { FileUtils.writeByteArrayToFile(jarFile, chainCode); // 校验合约文件 @@ -125,9 +125,9 @@ public class ContractJarUtils { } finally { // 删除文件 try { - FileUtils.forceDelete(jarFile); + jarFile.deleteOnExit(); } catch (Exception e) { - throw new IllegalStateException(e); + // DO NOTHING } } } @@ -144,6 +144,7 @@ public class ContractJarUtils { throw new IllegalStateException(CONTRACT_MF + " IS NULL !!!"); } byte[] bytes; + try { bytes = IOUtils.toByteArray(inputStream); } finally { @@ -156,20 +157,25 @@ public class ContractJarUtils { String txt = new String(bytes, StandardCharsets.UTF_8); // 生成新的Jar包文件,该文件路径与JarFile基本一致 - File tempJar = newJarFile(contractPath); - - // 复制除JDCHAIN.TXT之外的部分 - copy(jarFile, tempJar, null, null, CONTRACT_MF); - - // 生成新Jar包对应的Hash内容 - String verifyTxt = contractMF(FileUtils.readFileToByteArray(tempJar)); + File tempJar = newJarTempFile(); + try { + // 复制除JDCHAIN.TXT之外的部分 + copy(jarFile, tempJar, null, null, CONTRACT_MF); - // 删除临时文件 - FileUtils.forceDelete(tempJar); + // 生成新Jar包对应的Hash内容 + String verifyTxt = contractMF(FileUtils.readFileToByteArray(tempJar)); - // 校验Jar包内容 - if (!txt.equals(verifyTxt)) { - throw new IllegalStateException(String.format("Jar [%s] verify Illegal !!!", jarFile.getName())); + // 校验Jar包内容 + if (!txt.equals(verifyTxt)) { + throw new IllegalStateException(String.format("Jar [%s] verify Illegal !!!", jarFile.getName())); + } + } finally { + try { + // 删除临时文件 + tempJar.deleteOnExit(); + } catch (Exception e) { + // DO NOTHING + } } } @@ -227,20 +233,30 @@ public class ContractJarUtils { } } - private static File newJarFile(String contractPath) { + private static File newJarTempFile() { - if (contractPath != null && contractPath.length() > 0) { - return new File(contractPath + File.separator + - "contract-" + + try { + return File.createTempFile("contract-" + System.currentTimeMillis() + "-" + System.nanoTime() + "-" + - FILE_RANDOM.nextInt(1024) + - ".jar"); + FILE_RANDOM.nextInt(1024), ".jar"); + } catch (Exception e) { + throw new IllegalStateException(e); } - return new File("contract-" + - System.currentTimeMillis() + "-" + - System.nanoTime() + "-" + - FILE_RANDOM.nextInt(1024) + - ".jar"); +// +// if (contractPath != null && contractPath.length() > 0) { +// return new File(contractPath + File.separator + +// "contract-" + +// System.currentTimeMillis() + "-" + +// System.nanoTime() + "-" + +// FILE_RANDOM.nextInt(1024) + +// ".jar"); +// } +// +// return new File("contract-" + +// System.currentTimeMillis() + "-" + +// System.nanoTime() + "-" + +// FILE_RANDOM.nextInt(1024) + +// ".jar"); } }