Introduce io.bitsquare.btc.UserAgent

This makes things generally more explicit and reduces the number of
String arguments in the WalletFacade constructor.
This commit is contained in:
Chris Beams 2014-11-11 12:05:36 +01:00
parent b10958e790
commit 8f8866da6a
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
4 changed files with 63 additions and 20 deletions

View file

@ -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");

View file

@ -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();
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View file

@ -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<AddressConfidenceListener> addressConfidenceListeners = new CopyOnWriteArrayList<>();
private final List<TxConfidenceListener> 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