Skip to content
This repository was archived by the owner on May 27, 2022. It is now read-only.

Commit 257d132

Browse files
authored
Merge pull request #10 from jpmorganchase/fix_registry_loading
Properly load ContractRegistry address on startup (fixes #4)
2 parents fc8b593 + aadb438 commit 257d132

File tree

4 files changed

+37
-57
lines changed

4 files changed

+37
-57
lines changed

cakeshop-api/src/main/java/com/jpmorgan/cakeshop/config/AppConfig.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@
77
import java.io.FileInputStream;
88
import java.io.FileOutputStream;
99
import java.io.IOException;
10-
import java.util.Arrays;
11-
import java.util.List;
1210
import java.util.Properties;
1311
import java.util.concurrent.Executor;
1412

15-
import org.apache.commons.collections4.ListUtils;
16-
import org.apache.commons.collections4.Predicate;
1713
import org.apache.commons.lang3.StringUtils;
1814
import org.slf4j.LoggerFactory;
1915
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
2016
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
21-
import org.springframework.context.EnvironmentAware;
2217
import org.springframework.context.annotation.Bean;
2318
import org.springframework.context.annotation.Configuration;
2419
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
25-
import org.springframework.core.env.Environment;
2620
import org.springframework.core.io.FileSystemResource;
2721
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
2822
import org.springframework.scheduling.annotation.AsyncConfigurer;
@@ -31,43 +25,19 @@
3125

3226
@Configuration
3327
@EnableAsync
34-
public class AppConfig implements AsyncConfigurer, EnvironmentAware {
28+
public class AppConfig implements AsyncConfigurer {
3529

3630
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(AppConfig.class);
3731

3832
public static final String CONFIG_FILE = "application.properties";
3933

40-
private Environment env;
41-
42-
@Override
43-
public void setEnvironment(Environment e) {
44-
this.env = e;
45-
}
46-
47-
4834
/**
49-
* Return the configured environment name
35+
* Return the configured environment name (via spring.profiles.active system prop)
5036
*
51-
* Search order:
52-
* - ETH_ENV environment variable
53-
* - eth.environment system property (-Deth.environment param)
54-
* - Default to 'local' if none found
55-
*
56-
* @return Environment name
37+
* @return String Environment name
5738
*/
58-
public String getEnv() {
59-
//String env = getProp("ETH_ENV", "eth.environment");
60-
if (null == env) {
61-
// FIXME only default to local based on a flag passed down from maven build?
62-
LOG.warn("spring.profiles.active is not set");
63-
}
64-
List <String> profiles = ListUtils.selectRejected(Arrays.asList(env.getActiveProfiles()), new Predicate <String>() {
65-
@Override
66-
public boolean evaluate(String profile) {
67-
return profile.equalsIgnoreCase("container") || profile.equalsIgnoreCase("spring-boot");
68-
}
69-
});
70-
return profiles.get(0);
39+
public static String getEnv() {
40+
return System.getProperty("spring.profiles.active");
7141
}
7242

7343
/**
@@ -80,7 +50,7 @@ public boolean evaluate(String profile) {
8050
*
8151
* @return
8252
*/
83-
public String getConfigPath() {
53+
public static String getConfigPath() {
8454
String configPath = getProp("ETH_CONFIG", "eth.config.dir");
8555
if (!StringUtils.isBlank(configPath)) {
8656
return FileUtils.expandPath(configPath, getEnv());
@@ -108,20 +78,19 @@ private static String getProp(String env, String java) {
10878
}
10979

11080
@Bean
111-
// @Profile("container")
112-
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
113-
return createPropConfigurer(env, getConfigPath());
81+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
82+
return createPropConfigurer(getConfigPath());
11483
}
11584

116-
public String getVendorConfigFile() {
85+
public static String getVendorConfigFile() {
11786
return "config".concat(File.separator).concat("application.properties");
11887
}
11988

120-
public String getVendorEnvConfigFile() {
89+
public static String getVendorEnvConfigFile() {
12190
return "config".concat(File.separator).concat("application-").concat(getEnv()).concat(".properties");
12291
}
12392

124-
public void initVendorConfig(File configFile) throws IOException {
93+
public static void initVendorConfig(File configFile) throws IOException {
12594
// copy default file
12695
String path = FileUtils.getClasspathPath(getVendorEnvConfigFile()).toString();
12796
LOG.info("Initializing new config from " + path);
@@ -134,14 +103,13 @@ public void initVendorConfig(File configFile) throws IOException {
134103
SortedProperties.store(mergedProps, new FileOutputStream(configFile));
135104
}
136105

137-
public PropertySourcesPlaceholderConfigurer createPropConfigurer(Environment env,
138-
String configDir) throws IOException {
106+
public static PropertySourcesPlaceholderConfigurer createPropConfigurer(String configDir)
107+
throws IOException {
139108

140-
if (null == env) {
141-
throw new IOException("ENV var 'spring.profiles.active' not set; unable to load config");
109+
if (StringUtils.isBlank(getEnv())) {
110+
throw new IOException("System property 'spring.profiles.active' not set; unable to load config");
142111
}
143112

144-
LOG.info("eth.environment=" + env);
145113
LOG.info("eth.config.dir=" + configDir);
146114

147115
File configPath = new File(configDir);
@@ -171,6 +139,9 @@ public PropertySourcesPlaceholderConfigurer createPropConfigurer(Environment en
171139
PropertySourcesPlaceholderConfigurer propConfig = new PropertySourcesPlaceholderConfigurer();
172140
propConfig.setLocation(new FileSystemResource(configFile));
173141
propConfig.setProperties(localProps);
142+
propConfig.setLocalOverride(true);
143+
144+
LOG.info("Loading config from " + configFile.toString());
174145

175146
return propConfig;
176147
}

cakeshop-api/src/main/java/com/jpmorgan/cakeshop/config/SpringBootApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static void main(String[] args) {
3939
System.setProperty("spring.profiles.active", "local");
4040
}
4141

42+
if (StringUtils.isNotBlank(System.getProperty("spring.profiles.active"))) {
43+
System.setProperty("spring.config.location", "file:" + FileUtils.expandPath(AppConfig.getConfigPath(), "application.properties"));
44+
}
45+
4246
// extract geth from WAR (if necessary)
4347
try {
4448
extractGeth(configDir);

cakeshop-api/src/main/java/com/jpmorgan/cakeshop/service/task/BlockchainInitializerTask.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.jpmorgan.cakeshop.error.APIException;
66
import com.jpmorgan.cakeshop.model.Account;
77
import com.jpmorgan.cakeshop.model.TransactionResult;
8-
import com.jpmorgan.cakeshop.service.BlockService;
98
import com.jpmorgan.cakeshop.service.ContractRegistryService;
109
import com.jpmorgan.cakeshop.service.ContractService;
1110
import com.jpmorgan.cakeshop.service.ContractService.CodeType;
@@ -39,9 +38,6 @@ public class BlockchainInitializerTask implements Runnable {
3938

4039
private static final Logger LOG = LoggerFactory.getLogger(BlockchainInitializerTask.class);
4140

42-
@Autowired
43-
private BlockService blockService;
44-
4541
@Autowired
4642
private GethHttpService geth;
4743

@@ -89,7 +85,7 @@ private void syncWalletDb() {
8985
return;
9086
}
9187

92-
LOG.info("Storing existing wallet accounts");
88+
LOG.info("Storing existing wallet account balances");
9389
try {
9490
List<Account> list = walletService.list();
9591
for (Account account : list) {
@@ -120,7 +116,12 @@ private void deployContractRegistry() {
120116
contractRegistryAddress = regAddr;
121117
}
122118

123-
if (StringUtils.isNotBlank(contractRegistryAddress)) {
119+
if (StringUtils.isBlank(contractRegistryAddress)) {
120+
LOG.info("Contract registry address not found");
121+
122+
} else {
123+
// test stored address
124+
LOG.info("Loaded contract registry address " + contractRegistryAddress);
124125
Map<String, Object> contractRes;
125126
try {
126127
contractRes = geth.executeGethCall("eth_getCode", new Object[]{contractRegistryAddress, "latest"});
@@ -130,7 +131,11 @@ private void deployContractRegistry() {
130131
}
131132

132133
String binaryCode = (String) contractRes.get("_result");
133-
if (!binaryCode.contentEquals("0x")) {
134+
if (binaryCode.contentEquals("0x")) {
135+
LOG.warn("eth_getCode for " + contractRegistryAddress + " returned 0x");
136+
137+
} else {
138+
// got code, contract exists
134139
try {
135140
contractRegistry.updateRegistryAddress(this.contractRegistryAddress);
136141
} catch (APIException e) {
@@ -141,7 +146,8 @@ private void deployContractRegistry() {
141146
// continue to deploy
142147
}
143148

144-
LOG.info("Initializing empty blockchain");
149+
LOG.info("ContractRegistry not found on chain");
150+
145151
try {
146152
nodeService.update(null, null, null, true, null, null); // make sure mining is enabled
147153

cakeshop-api/src/test/java/com/jpmorgan/cakeshop/test/config/TestAppConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public static void setUp() {
5656
@Profile("test")
5757
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
5858
AppConfig appConfig = new AppConfig();
59-
appConfig.setEnvironment(env);
60-
return appConfig.createPropConfigurer(env, TempFileManager.getTempPath());
59+
return appConfig.createPropConfigurer(TempFileManager.getTempPath());
6160
}
6261

6362
@Bean(name="asyncExecutor")

0 commit comments

Comments
 (0)