Skip to content

Commit

Permalink
fix the system.exit when generate java file
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyanho committed Apr 13, 2020
1 parent da8d8c6 commit 325b513
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.webank.webase.front.base.config;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MySecurityManagerConfig {

public static void forbidSystemExitCall() {
// Set current security manager.
// If the argument is null and no security manager has been established then no action is taken
System.setSecurityManager(new NoExitSecurityManager());
}

public static void enableSystemExitCall() {
System.setSecurityManager( null ) ;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.webank.webase.front.base.config;

import java.security.Permission;

public class NoExitSecurityManager extends SecurityManager {
@Override
public void checkExit(int status) {
throw new SecurityException();
}

@Override
public void checkPermission( Permission permission ) {
if( "exitVM".equals( permission.getName() ) ) {
throw new SecurityException();
}
}

}
67 changes: 32 additions & 35 deletions src/main/java/com/webank/webase/front/contract/ContractService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,21 @@
*/
package com.webank.webase.front.contract;

import static com.webank.webase.front.base.code.ConstantCode.GROUPID_NOT_EXIST;
import static org.fisco.bcos.web3j.solidity.compiler.SolidityCompiler.Options.ABI;
import static org.fisco.bcos.web3j.solidity.compiler.SolidityCompiler.Options.BIN;
import static org.fisco.bcos.web3j.solidity.compiler.SolidityCompiler.Options.INTERFACE;
import static org.fisco.bcos.web3j.solidity.compiler.SolidityCompiler.Options.METADATA;
import com.alibaba.fastjson.JSON;
import com.webank.webase.front.base.code.ConstantCode;
import com.webank.webase.front.base.config.MySecurityManagerConfig;
import com.webank.webase.front.base.enums.ContractStatus;
import com.webank.webase.front.base.exception.FrontException;
import com.webank.webase.front.base.properties.Constants;
import com.webank.webase.front.base.response.BaseResponse;
import com.webank.webase.front.contract.entity.Contract;
import com.webank.webase.front.contract.entity.ContractPath;
import com.webank.webase.front.contract.entity.ContractPathKey;
import com.webank.webase.front.contract.entity.FileContentHandle;
import com.webank.webase.front.contract.entity.ReqContractPath;
import com.webank.webase.front.contract.entity.ReqContractSave;
import com.webank.webase.front.contract.entity.ReqDeploy;
import com.webank.webase.front.contract.entity.ReqPageContract;
import com.webank.webase.front.contract.entity.ReqSendAbi;
import com.webank.webase.front.contract.entity.RspContractCompile;
import com.webank.webase.front.contract.entity.*;
import com.webank.webase.front.keystore.KeyStoreService;
import com.webank.webase.front.transaction.TransService;
import com.webank.webase.front.util.AbiUtil;
import com.webank.webase.front.util.CommonUtils;
import com.webank.webase.front.util.ContractAbiUtil;
import com.webank.webase.front.util.FrontUtils;
import com.webank.webase.front.web3api.Web3ApiService;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -82,6 +52,23 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static com.webank.webase.front.base.code.ConstantCode.GROUPID_NOT_EXIST;
import static org.fisco.bcos.web3j.solidity.compiler.SolidityCompiler.Options.*;

/**
* contract management.
*/
Expand Down Expand Up @@ -410,16 +397,15 @@ public static FileContentHandle compileToJavaFile(String contractName,
List<AbiDefinition> abiInfo, String contractBin, String packageName)
throws IOException {


File abiFile = new File(Constants.ABI_DIR + Constants.DIAGONAL + contractName + ".abi");
FrontUtils.createFileIfNotExist(abiFile, true);
FileUtils.writeStringToFile(abiFile, JSON.toJSONString(abiInfo));
File binFile = new File(Constants.BIN_DIR + Constants.DIAGONAL + contractName + ".bin");
FrontUtils.createFileIfNotExist(binFile, true);
FileUtils.writeStringToFile(binFile, contractBin);

SolidityFunctionWrapperGenerator
.main(Arrays.asList("-a", abiFile.getPath(), "-b", binFile.getPath(), "-p",
packageName, "-o", Constants.JAVA_DIR).toArray(new String[0]));
generateJavaFile(packageName, abiFile, binFile);

String outputDirectory = "";
if (!packageName.isEmpty()) {
Expand All @@ -435,6 +421,17 @@ public static FileContentHandle compileToJavaFile(String contractName,
return new FileContentHandle(contractName + ".java", targetStream);
}

private static synchronized void generateJavaFile(String packageName, File abiFile, File binFile) {
try {
MySecurityManagerConfig.forbidSystemExitCall();
SolidityFunctionWrapperGenerator
.main(Arrays.asList("-a", abiFile.getPath(), "-b", binFile.getPath(), "-p",
packageName, "-o", Constants.JAVA_DIR).toArray(new String[0]));
} finally {
MySecurityManagerConfig.enableSystemExitCall();
}
}


/**
* delete contract by contractId.
Expand Down

0 comments on commit 325b513

Please sign in to comment.