Skip to content

Commit

Permalink
Merge pull request #146 from chaoxinhu/feature/remove-value-spring
Browse files Browse the repository at this point in the history
Eradicate all Spring dependency in SDK
  • Loading branch information
chenhaozx committed Jun 18, 2019
2 parents dc6458b + 23adbaf commit cfb089c
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 150 deletions.
16 changes: 10 additions & 6 deletions build-tools/bin/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ config_file=${java_source_code_dir}/dist/bin/run.config
app_xml_config_dir=${java_source_code_dir}/dist/conf/
app_xml_config_tpl=${java_source_code_dir}/src/main/resources/fisco.properties.tpl
app_xml_config=${java_source_code_dir}/src/main/resources/fisco.properties
weid_config_tpl=${java_source_code_dir}/src/main/resources/weidentity.properties.tpl
weid_config=${java_source_code_dir}/src/main/resources/weidentity.properties

CLASSPATH=${java_source_code_dir}/dist/conf

Expand Down Expand Up @@ -81,9 +83,12 @@ function modify_config()
export ISSUER_ADDRESS=${issuer_address}
export EVIDENCE_ADDRESS=${evidence_address}
export SPECIFICISSUER_ADDRESS=${specificissuer_address}
MYVARS='${BLOCKCHIAN_NODE_INFO}:${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
MYVARS='${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
envsubst ${MYVARS} < ${app_xml_config_tpl} >${app_xml_config}
cp ${app_xml_config} ${app_xml_config_dir}
NODEVAR='${BLOCKCHIAN_NODE_INFO}'
envsubst ${NODEVAR} < ${weid_config_tpl} >${weid_config}
cp ${weid_config} ${app_xml_config_dir}
echo "modify sdk config finished..."
}

Expand Down Expand Up @@ -119,19 +124,18 @@ function gradle_build_sdk()
content=
for var in ${array[@]}
do
if [ ! -z ${content} ];then
content="${content}\n"
fi
content="${content}WeIdentity@$var;"
content="${content}WeIdentity@$var,"
done
export BLOCKCHIAN_NODE_INFO=$(echo -e ${content})
export WEID_ADDRESS="0x0"
export CPT_ADDRESS="0x0"
export ISSUER_ADDRESS="0x0"
export EVIDENCE_ADDRESS="0x0"
export SPECIFICISSUER_ADDRESS="0x0"
MYVARS='${BLOCKCHIAN_NODE_INFO}:${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
MYVARS='${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
envsubst ${MYVARS} < ${app_xml_config_tpl} >${app_xml_config}
NODEVAR='${BLOCKCHIAN_NODE_INFO}'
envsubst ${NODEVAR} < ${weid_config_tpl} >${weid_config}

cd ${java_source_code_dir}/
if [ -d dist/ ];then
Expand Down
17 changes: 4 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,9 @@ List logger = [
"org.apache.logging.log4j:log4j-web:2.1"
]

def spring_version = "4.1.8.RELEASE"
List spring = [
"org.springframework:spring-core:$spring_version",
"org.springframework:spring-beans:$spring_version",
"org.springframework:spring-context:$spring_version",
"org.springframework:spring-test:$spring_version"
]

// junit test
List junit = [
"junit:junit:4.12",
"org.springframework:spring-test:$spring_version"
"junit:junit:4.12"
]

List apache_commons = [
Expand All @@ -78,7 +69,7 @@ List weidentity_contract = [
"com.webank:weidentity-contract-java:1.2.2"
]

List db_driver = [
List mysql_driver = [
"mysql:mysql-connector-java:5.1.44",
"org.apache.commons:commons-dbcp2:2.5.0"
]
Expand All @@ -93,8 +84,8 @@ List zxing = [

// In this section you declare the dependencies for your production and test code
dependencies {
compile logger, spring, lombok, apache_commons, json, web3sdk, weidentity_contract, db_driver, zxing
testCompile logger, spring, lombok, apache_commons, json, web3sdk, junit, jmockit
compile logger, lombok, apache_commons, json, web3sdk, weidentity_contract, mysql_driver, zxing
testCompile logger, lombok, apache_commons, json, web3sdk, junit, jmockit
}

jacocoTestReport {
Expand Down
12 changes: 9 additions & 3 deletions ci/script/build-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ java_source_code_dir=$(pwd)

app_xml_config_tpl=${java_source_code_dir}/src/main/resources/fisco.properties.tpl
app_xml_config=${java_source_code_dir}/src/main/resources/fisco.properties
weid_config_tpl=${java_source_code_dir}/src/main/resources/weidentity.properties.tpl
weid_config=${java_source_code_dir}/src/main/resources/weidentity.properties

function modify_config()
{
Expand All @@ -17,10 +19,12 @@ function modify_config()
export ISSUER_ADDRESS=${issuer_address}
export EVIDENCE_ADDRESS=${evidence_address}
export SPECIFICISSUER_ADDRESS=${specificissuer_address}
MYVARS='${BLOCKCHIAN_NODE_INFO}:${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
MYVARS='${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
envsubst ${MYVARS} < ${app_xml_config_tpl} >${app_xml_config}
cp ${app_xml_config} ${java_source_code_dir}/src/test/resources/
cp ${java_source_code_dir}/src/main/resources/weidentity.properties ${java_source_code_dir}/src/test/resources/
NODEVAR='${BLOCKCHIAN_NODE_INFO}'
envsubst ${NODEVAR} < ${weid_config_tpl} >${weid_config}
cp ${weid_config} ${java_source_code_dir}/src/test/resources/
if [ -e ${java_source_code_dir}/privateKey.txt ];then
cp ${java_source_code_dir}/privateKey.txt ${java_source_code_dir}/src/test/resources/
fi
Expand All @@ -40,8 +44,10 @@ function gradle_build_sdk()
export ISSUER_ADDRESS="0x0"
export EVIDENCE_ADDRESS="0x0"
export SPECIFICISSUER_ADDRESS="0x0"
MYVARS='${BLOCKCHIAN_NODE_INFO}:${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
MYVARS='${WEID_ADDRESS}:${CPT_ADDRESS}:${ISSUER_ADDRESS}:${EVIDENCE_ADDRESS}:${SPECIFICISSUER_ADDRESS}'
envsubst ${MYVARS} < ${app_xml_config_tpl} >${app_xml_config}
NODEVAR='${BLOCKCHIAN_NODE_INFO}'
envsubst ${NODEVAR} < ${weid_config_tpl} >${weid_config}

echo "Begin to compile java code......"
if [ -d ${java_source_code_dir}/dist ]; then
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/webank/weid/config/ContractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.webank.weid.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

/**
* contract address config.
Expand All @@ -33,30 +32,25 @@ public class ContractConfig {
/**
* The WeIdentity DID Contract address.
*/
@Value("${weId.contractaddress}")
private String weIdAddress;

/**
* The CPT Contract address.
*/
@Value("${cpt.contractaddress}")
private String cptAddress;

/**
* The AuthorityIssuerController Contract address.
*/
@Value("${issuer.contractaddress}")
private String issuerAddress;

/**
* The EvidenceController Contract address.
*/
@Value("${evidence.contractaddress}")
private String evidenceAddress;

/**
* The Specific issuer Contract address.
*/
@Value("${specificissuer.contractaddress}")
private String specificIssuerAddress;
}
130 changes: 31 additions & 99 deletions src/main/java/com/webank/weid/config/FiscoConfig.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.webank.weid.config;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;

import com.webank.weid.util.PropertyUtils;

/**
* FISCO-BCOS Config that loaded by java process, not Spring applicationContext anymore.
Expand All @@ -19,71 +13,33 @@
* @since 2019.6
*/
@Data
@PropertySource(value = "classpath:fisco.properties")
public class FiscoConfig {

private static final Logger logger = LoggerFactory.getLogger(FiscoConfig.class);

// Note that all keys are appended with a colon ":" to support regex auto-loading

@Value("${bcos.version:}")
private String version;

@Value("${nodes:}")
private String nodes;

@Value("${weId.contractaddress:}")
private String weIdAddress;

@Value("${cpt.contractaddress:}")
private String cptAddress;

@Value("${issuer.contractaddress:}")
private String issuerAddress;

@Value("${evidence.contractaddress:}")
private String evidenceAddress;

@Value("${specificissuer.contractaddress:}")
private String specificIssuerAddress;

@Value("${chain.id:}")
private String chainId;

@Value("${web3sdk.timeout:10000}")
private String web3sdkTimeout;

@Value("${web3sdk.core-pool-size:100}")
private String web3sdkCorePoolSize;

@Value("${web3sdk.max-pool-size:200}")
private String web3sdkMaxPoolSize;

@Value("${web3sdk.queue-capacity:1000}")
private String web3sdkQueueSize;

@Value("${web3sdk.keep-alive-seconds:60}")
private String web3sdkKeepAliveSeconds;

@Value("${v1.ca-crt-path:ca.crt}")
private String groupId;
private String encryptType;
private String v1CaCrtPath;

@Value("${v1.client-crt-password:123456}")
private String v1ClientCrtPassword;

@Value("${v1.client-key-store-path:client.keystore}")
private String v1ClientKeyStorePath;

@Value("${v1.key-store-password:123456}")
private String v1KeyStorePassword;

@Value("${v2.ca-crt-path:./v2/ca.crt}")
private String v2CaCrtPath;

@Value("${v2.node-crt-path:./v2/node.crt}")
private String v2NodeCrtPath;

@Value("${v2.node-key-path:./v2/node.key}")
private String v2NodeKeyPath;

/**
Expand All @@ -92,59 +48,35 @@ public class FiscoConfig {
* @return true if success, else false
*/
public boolean load() {
if (!FiscoConfig.class.isAnnotationPresent(PropertySource.class)) {
logger.error("set configuration file name use @PropertySource");
return false;
}

PropertySource propertySource = FiscoConfig.class.getAnnotation(PropertySource.class);
String[] files = propertySource.value();
if (!files[0].startsWith("classpath:")) {
logger.error("configuration file must be in classpath");
return false;
}
logger.info("load properties from file: {}", files[0]);

// be careful of the path
String file = "/" + files[0].replace("classpath:", "");
try (InputStream inputStream = FiscoConfig.class.getResourceAsStream(file)) {
Properties properties = new Properties();
properties.load(inputStream);

Field[] fields = FiscoConfig.class.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Value.class)) {
Value value = field.getAnnotation(Value.class);

//String.split can not support this regex
Pattern pattern = Pattern.compile("\\$\\{(\\S+):(\\S*)}");
Matcher matcher = pattern.matcher(value.value());
String k = "";
String v = "";
if (matcher.find()) {
if (matcher.groupCount() >= 1) {

k = matcher.group(1);
}
if (matcher.groupCount() >= 2) {
v = matcher.group(2);
}
}

if (properties.containsKey(k)) {
v = properties.getProperty(k);
}
field.setAccessible(true);
Object obj = field.getType().getConstructor(String.class).newInstance(v);
field.set(this, obj);
}
}
try {
// node info is obtained from weidentity.properties
nodes = PropertyUtils.getProperty("nodes");

version = PropertyUtils.getProperty("bcos.version");
weIdAddress = PropertyUtils.getProperty("weId.contractaddress");
cptAddress = PropertyUtils.getProperty("cpt.contractaddress");
issuerAddress = PropertyUtils.getProperty("issuer.contractaddress");
evidenceAddress = PropertyUtils.getProperty("evidence.contractaddress");
specificIssuerAddress = PropertyUtils.getProperty("specificissuer.contractaddress");
chainId = PropertyUtils.getProperty("chain.id");
web3sdkTimeout = PropertyUtils.getProperty("web3sdk.timeout");
web3sdkCorePoolSize = PropertyUtils.getProperty("web3sdk.core-pool-size");
web3sdkMaxPoolSize = PropertyUtils.getProperty("web3sdk.max-pool-size");
web3sdkQueueSize = PropertyUtils.getProperty("web3sdk.queue-capacity");
web3sdkKeepAliveSeconds = PropertyUtils.getProperty("web3sdk.keep-alive-seconds");
groupId = PropertyUtils.getProperty("group.id");
encryptType = PropertyUtils.getProperty("encrypt.type");
v1CaCrtPath = PropertyUtils.getProperty("v1.ca-crt-path");
v1ClientCrtPassword = PropertyUtils.getProperty("v1.client-crt-password");
v1ClientKeyStorePath = PropertyUtils.getProperty("v1.client-key-store-path");
v1KeyStorePassword = PropertyUtils.getProperty("v1.key-store-password");
v2CaCrtPath = PropertyUtils.getProperty("v2.ca-crt-path");
v2NodeCrtPath = PropertyUtils.getProperty("v2.node-crt-path");
v2NodeKeyPath = PropertyUtils.getProperty("v2.node-key-path");
return true;
} catch (Exception e) {
logger.error("load properties failed", e);
logger.error("Error occurred during loading Fisco-Bcos properties: ", e);
return false;
}

logger.info("read from fisco.properties: {}", this);
return true;
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public final class WeIdConstant {
* UUID Separator.
*/
public static final String UUID_SEPARATOR = "-";

/**
* WeId Separator.
*/
Expand Down Expand Up @@ -209,4 +209,9 @@ public final class WeIdConstant {
* The FISCO-BCOS Address pattern.
*/
public static final String FISCO_BCOS_ADDRESS_PATTERN = "0x[a-fA-f0-9]{40}";

/**
* The FISCO-BCOS Address pattern.
*/
public static final String FISCO_BCOS_1_X_VERSION_PREFIX = "1";
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class DeployContract {
fiscoConfig = new FiscoConfig();
if (!fiscoConfig.load()) {
logger.error("[BaseService] Failed to load Fisco-BCOS blockchain node information.");
System.exit(1);
}
loadConfig();
}
Expand All @@ -121,7 +122,12 @@ private static boolean loadConfig() {

private static boolean initWeb3j() {
logger.info("[BaseService] begin to init web3j instance..");
Service service = TransactionUtils.buildFiscoBcosService(fiscoConfig);
Service service;
if (fiscoConfig.getVersion().startsWith(WeIdConstant.FISCO_BCOS_1_X_VERSION_PREFIX)) {
service = TransactionUtils.buildFiscoBcosService(fiscoConfig);
} else {
return false;
}
try {
service.run();
} catch (Exception e) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public BaseService() {
private static boolean initWeb3j() {

logger.info("[BaseService] begin to init web3j instance..");
service = TransactionUtils.buildFiscoBcosService(fiscoConfig);

if (fiscoConfig.getVersion().startsWith(WeIdConstant.FISCO_BCOS_1_X_VERSION_PREFIX)) {
service = TransactionUtils.buildFiscoBcosService(fiscoConfig);
}
OnNotifyCallback pushCallBack = new OnNotifyCallback();
service.setPushCallback(pushCallBack);
pushCallBack.registAmopCallback(
Expand Down

0 comments on commit cfb089c

Please sign in to comment.