From 45a215c15b6457f13d5e192ed959a40092cbe4e4 Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Thu, 6 Sep 2018 16:06:50 -0700 Subject: [PATCH] Move OANDA account info into separate file --- .gitignore | 1 + build.gradle | 6 +++- config/config.properties.sample | 11 +++++++ src/main/java/AccountInfo.java | 16 --------- src/main/java/App.java | 6 ++-- src/main/java/Config.java | 57 +++++++++++++++++++++++++++++++++ src/main/java/OANDAReader.java | 18 +++++------ 7 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 config/config.properties.sample delete mode 100644 src/main/java/AccountInfo.java create mode 100644 src/main/java/Config.java diff --git a/.gitignore b/.gitignore index 8c4e63a..2ff0aa9 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.emacs.desktop.lock /.meghanada/ *.csv +/config/config.properties diff --git a/build.gradle b/build.gradle index ac8adcf..6e999b9 100644 --- a/build.gradle +++ b/build.gradle @@ -59,7 +59,11 @@ distributions { include '*.epl' } } - + into('config') { + from('config') { + include '*.properties' + } + } // exclude('**/.data/**') // from('src/main/webapp') { // include '**/*.jsp' diff --git a/config/config.properties.sample b/config/config.properties.sample new file mode 100644 index 0000000..950c556 --- /dev/null +++ b/config/config.properties.sample @@ -0,0 +1,11 @@ +# Configuration properties + +# An OANDA numeric account id +OANDA.accountID=000-000-0000000-000 + +# The personal API access token for the account ID above +OANDA.accessToken=00000000000000000000000000000000-00000000000000000000000000000000 + +# Set to 'true' if the account is on the 'fxlive' service. +# Set to 'false' if the account is on the 'fxpractice' service. +OANDA.isLiveAccount=false diff --git a/src/main/java/AccountInfo.java b/src/main/java/AccountInfo.java deleted file mode 100644 index 1ce5657..0000000 --- a/src/main/java/AccountInfo.java +++ /dev/null @@ -1,16 +0,0 @@ -public class AccountInfo { - - // TODO use properties file - - public String accountID() { - return "101-001-7935538-001"; - } - - public boolean isLiveAccount() { - return false; - } - - public String accessToken() { - return "9a480f0b83e987f4015cf0846790c7d9-695ced635526744abd61bdf0e2ae8b71"; - } -} diff --git a/src/main/java/App.java b/src/main/java/App.java index 09c056f..995dbbb 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -48,11 +48,7 @@ public class App { String csv = cmdline.getOptionValue("historical"); reader = new CSVReader(new File(csv)); log.info("Using CSV file {}", csv); - } else { - // TODO: separate - AccountInfo acctInfo = new AccountInfo(); String[] instruments = getInstruments(cmdline); - reader = new OANDAReader(acctInfo, instruments); log.info("Reading from stream."); } @@ -122,4 +118,6 @@ public class App { formatter.printHelp("java -jar ATS_Esper-all.jar [args] rules.epl", options); System.exit(1); } + Config config = new Config(); + reader = new OANDAReader(config, instruments); } diff --git a/src/main/java/Config.java b/src/main/java/Config.java new file mode 100644 index 0000000..ca238cd --- /dev/null +++ b/src/main/java/Config.java @@ -0,0 +1,57 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Config provides access to runtime configuration properties defined + * in a Java properties file. The file is located in + * "config/config.properties" relative to the installation root. + */ +public class Config { + static final Logger log = LoggerFactory.getLogger("Config"); + private Properties properties = new Properties(); + + + /** + * Load the configuration properties file. + */ + public Config() { + try { + String cwd = System.getProperty("user.dir"); + String configFile = cwd + File.separator + "config" + + File.separator + "config.properties"; + log.info("Loading config file " + configFile); + properties.load(new FileInputStream(configFile)); + } catch (IOException e) { + log.error("Error loading config.properties", e); + System.exit(1); + } + } + + /** + * Return the OANDA account id. + */ + public String accountID() { + return properties.getProperty("OANDA.accountID"); + } + + /** + * Return true if the OANDA account is on the live ('fxlive') site. + * Return false if it is on the practice ('fxpractice') site. + */ + public boolean isLiveAccount() { + return Boolean.parseBoolean(properties.getProperty("OANDA.isLiveAccount")); + } + + /** + * + */ + public String accessToken() { + return properties.getProperty("OANDA.accessToken"); + } +} diff --git a/src/main/java/OANDAReader.java b/src/main/java/OANDAReader.java index e486b78..f7c607d 100644 --- a/src/main/java/OANDAReader.java +++ b/src/main/java/OANDAReader.java @@ -21,13 +21,13 @@ import org.slf4j.LoggerFactory; public class OANDAReader implements TickStreamReader { final Logger log = LoggerFactory.getLogger(OANDAReader.class); ObjectMapper mapper; - AccountInfo accountInfo; String[] instruments; + Config config; - public OANDAReader(AccountInfo accountInfo, String[] instruments) { - this.accountInfo = accountInfo; - this.instruments = instruments; + public OANDAReader(Config config, String[] instruments) { + this.config = config; + this.instruments = instruments; mapper = new ObjectMapper(); mapper.registerModule(new JodaModule()); @@ -38,7 +38,6 @@ public class OANDAReader implements TickStreamReader { } private String streamURL() { - String type = accountInfo.isLiveAccount() ? "fxlive" : "fxpractice"; String instrList = String.join(",", instruments); String query = ""; try { @@ -46,10 +45,11 @@ public class OANDAReader implements TickStreamReader { } catch (UnsupportedEncodingException e) { log.error("Creating stream URL", e); } + String type = config.isLiveAccount() ? "fxlive" : "fxpractice"; - return String.format("https://stream-%s.oanda.com/v3/accounts/%s/pricing/stream?%s", - type, accountInfo.accountID(), query); - } + return String.format("https://stream-%s.oanda.com/v3/accounts/%s/pricing/stream?%s", + type, config.accountID(), query); + } private void readConnection(String urlStr, TickProcessor processor) { HttpsURLConnection httpConn = null; @@ -65,8 +65,8 @@ public class OANDAReader implements TickStreamReader { //httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); //httpConn.setReadTimeout(50 * 1000); - httpConn.setRequestProperty("Authorization", "Bearer 9a480f0b83e987f4015cf0846790c7d9-695ced635526744abd61bdf0e2ae8b71"); BufferedReader is = + httpConn.setRequestProperty("Authorization", "Bearer " + config.accessToken()); new BufferedReader(new InputStreamReader(httpConn.getInputStream())); while ((line = is.readLine( )) != null) {