From 8f8866da6a87f918299dd6b6c89e98743e114f96 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 11 Nov 2014 12:05:36 +0100 Subject: [PATCH] Introduce io.bitsquare.btc.UserAgent This makes things generally more explicit and reduces the number of String arguments in the WalletFacade constructor. --- .../bitsquare/app/BitsquareEnvironment.java | 7 ++- .../java/io/bitsquare/btc/BitcoinModule.java | 13 +++--- src/main/java/io/bitsquare/btc/UserAgent.java | 44 +++++++++++++++++++ .../java/io/bitsquare/btc/WalletFacade.java | 19 +++----- 4 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 src/main/java/io/bitsquare/btc/UserAgent.java diff --git a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java index b3dbfecac9..e66520517d 100644 --- a/src/main/java/io/bitsquare/app/BitsquareEnvironment.java +++ b/src/main/java/io/bitsquare/app/BitsquareEnvironment.java @@ -18,6 +18,7 @@ package io.bitsquare.app; import io.bitsquare.BitsquareException; +import io.bitsquare.btc.UserAgent; import io.bitsquare.btc.WalletFacade; import io.bitsquare.persistence.Persistence; @@ -71,10 +72,12 @@ public class BitsquareEnvironment extends StandardEnvironment { private PropertySource defaultProperties(String appName) { return new PropertiesPropertySource(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME, new Properties() {{ setProperty(APP_NAME_KEY, appName); + + setProperty(UserAgent.NAME_KEY, appName); + setProperty(UserAgent.VERSION_KEY, "0.1"); + setProperty(WalletFacade.DIR_KEY, AppDirectory.dir(appName).toString()); setProperty(WalletFacade.PREFIX_KEY, appName); - setProperty(WalletFacade.USERAGENT_NAME_KEY, appName); - setProperty(WalletFacade.USERAGENT_VERSION_KEY, "0.1"); setProperty(Persistence.DIR_KEY, AppDirectory.dir(appName).toString()); setProperty(Persistence.PREFIX_KEY, appName + "_pref"); diff --git a/src/main/java/io/bitsquare/btc/BitcoinModule.java b/src/main/java/io/bitsquare/btc/BitcoinModule.java index 7e133d97fc..8293e194ab 100644 --- a/src/main/java/io/bitsquare/btc/BitcoinModule.java +++ b/src/main/java/io/bitsquare/btc/BitcoinModule.java @@ -31,7 +31,6 @@ import java.io.File; import org.springframework.core.env.Environment; import static com.google.inject.name.Names.named; -import static io.bitsquare.btc.WalletFacade.*; public class BitcoinModule extends BitsquareModule { @@ -44,14 +43,18 @@ public class BitcoinModule extends BitsquareModule { @Override protected void configure() { + bind(NetworkParameters.class).toInstance(network()); + bind(FeePolicy.class).asEagerSingleton(); + + bindConstant().annotatedWith(named(UserAgent.NAME_KEY)).to(env.getRequiredProperty(UserAgent.NAME_KEY)); + bindConstant().annotatedWith(named(UserAgent.VERSION_KEY)).to(env.getRequiredProperty(UserAgent.VERSION_KEY)); + bind(UserAgent.class).asEagerSingleton(); + File walletDir = new File(env.getRequiredProperty(WalletFacade.DIR_KEY)); bind(File.class).annotatedWith(named(WalletFacade.DIR_KEY)).toInstance(walletDir); bindConstant().annotatedWith(named(WalletFacade.PREFIX_KEY)).to(env.getRequiredProperty(WalletFacade.PREFIX_KEY)); - bindConstant().annotatedWith(named(USERAGENT_NAME_KEY)).to(env.getRequiredProperty(USERAGENT_NAME_KEY)); - bindConstant().annotatedWith(named(USERAGENT_VERSION_KEY)).to(env.getRequiredProperty(USERAGENT_VERSION_KEY)); - bind(NetworkParameters.class).toInstance(network()); - bind(FeePolicy.class).asEagerSingleton(); bind(WalletFacade.class).asEagerSingleton(); + bind(BlockChainFacade.class).asEagerSingleton(); } diff --git a/src/main/java/io/bitsquare/btc/UserAgent.java b/src/main/java/io/bitsquare/btc/UserAgent.java new file mode 100644 index 0000000000..dc5b50f9d3 --- /dev/null +++ b/src/main/java/io/bitsquare/btc/UserAgent.java @@ -0,0 +1,44 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + +package io.bitsquare.btc; + +import javax.inject.Inject; +import javax.inject.Named; + +public class UserAgent { + public static final String NAME_KEY = "useragent.name"; + public static final String VERSION_KEY = "useragent.version"; + + private final String name; + private final String version; + + @Inject + public UserAgent(@Named(NAME_KEY) String name, @Named(VERSION_KEY) String version) { + this.name = name; + this.version = version; + } + + public String getName() { + return name; + } + + public String getVersion() { + return version; + } +} + diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 5445060a40..fa2db163a1 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -97,8 +97,6 @@ public class WalletFacade { public static final String DIR_KEY = "wallet.dir"; public static final String PREFIX_KEY = "wallet.prefix"; - public static final String USERAGENT_NAME_KEY = "bitcoin.useragent.name"; - public static final String USERAGENT_VERSION_KEY = "bitcoin.useragent.version"; private final List addressConfidenceListeners = new CopyOnWriteArrayList<>(); private final List txConfidenceListeners = new CopyOnWriteArrayList<>(); @@ -111,8 +109,7 @@ public class WalletFacade { private final Persistence persistence; private final File walletDir; private final String walletPrefix; - private final String userAgentName; - private final String userAgentVersion; + private final UserAgent userAgent; private WalletAppKit walletAppKit; private Wallet wallet; @@ -126,20 +123,16 @@ public class WalletFacade { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public WalletFacade(NetworkParameters params, FeePolicy feePolicy, - CryptoFacade cryptoFacade, Persistence persistence, - @Named(DIR_KEY) File walletDir, - @Named(PREFIX_KEY) String walletPrefix, - @Named(USERAGENT_NAME_KEY) String userAgentName, - @Named(USERAGENT_VERSION_KEY) String userAgentVersion) { + public WalletFacade(NetworkParameters params, FeePolicy feePolicy, CryptoFacade cryptoFacade, + Persistence persistence, UserAgent userAgent, + @Named(DIR_KEY) File walletDir, @Named(PREFIX_KEY) String walletPrefix) { this.params = params; this.feePolicy = feePolicy; this.cryptoFacade = cryptoFacade; this.persistence = persistence; this.walletDir = walletDir; this.walletPrefix = walletPrefix; - this.userAgentName = userAgentName; - this.userAgentVersion = userAgentVersion; + this.userAgent = userAgent; } @@ -210,7 +203,7 @@ public class WalletFacade { walletAppKit.setDownloadListener(downloadListener) .setBlockingStartup(false) - .setUserAgent(userAgentName, userAgentVersion); + .setUserAgent(userAgent.getName(), userAgent.getVersion()); /* // TODO restore from DeterministicSeed