Remove static access to Bitsquare#getAppName

This commit is contained in:
Chris Beams 2014-11-04 19:40:01 +01:00
parent 6f27c5ce29
commit 865cf39e64
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
8 changed files with 114 additions and 108 deletions

View File

@ -38,7 +38,6 @@ import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
@ -49,95 +48,82 @@ public class Bitsquare {
private static String appName = "Bitsquare";
private static String interfaceHint;
public static String getAppName() {
return appName;
}
public static void main(String[] args) {
BitsquareArgumentParser parser = new BitsquareArgumentParser();
Namespace namespace = null;
try {
namespace = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
Namespace namespace = parser.parseArgs(args);
//
// global args
//
if (namespace.getString(BitsquareArgumentParser.NAME_FLAG) != null) {
appName = appName + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
}
if (namespace != null) {
//
// global args
//
if (namespace.getString(BitsquareArgumentParser.NAME_FLAG) != null) {
appName = appName + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
}
if (namespace.getString(BitsquareArgumentParser.INFHINT_FLAG) != null) {
interfaceHint = namespace.getString(BitsquareArgumentParser.INFHINT_FLAG);
}
if (namespace.getString(BitsquareArgumentParser.INFHINT_FLAG) != null) {
interfaceHint = namespace.getString(BitsquareArgumentParser.INFHINT_FLAG);
}
int port = -1;
if (namespace.getString(BitsquareArgumentParser.PORT_FLAG) != null) {
port = Integer.valueOf(namespace.getString(BitsquareArgumentParser.PORT_FLAG));
}
int port = -1;
if (namespace.getString(BitsquareArgumentParser.PORT_FLAG) != null) {
port = Integer.valueOf(namespace.getString(BitsquareArgumentParser.PORT_FLAG));
}
//
// seed-node only
//
String seedID = SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN1.getId();
if (namespace.getString(BitsquareArgumentParser.PEER_ID_FLAG) != null) {
seedID = namespace.getString(BitsquareArgumentParser.PEER_ID_FLAG);
}
//
// seed-node only
//
String seedID = SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN1.getId();
if (namespace.getString(BitsquareArgumentParser.PEER_ID_FLAG) != null) {
seedID = namespace.getString(BitsquareArgumentParser.PEER_ID_FLAG);
}
ActorSystem actorSystem = ActorSystem.create(appName);
ActorSystem actorSystem = ActorSystem.create(getAppName());
final Set<PeerAddress> peerAddresses = new HashSet<PeerAddress>();
final String sid = seedID;
SeedNodeAddress.StaticSeedNodeAddresses.getAllSeedNodeAddresses().forEach(a -> {
if (!a.getId().equals(sid)) {
try {
peerAddresses.add(new PeerAddress(Number160.createHash(a.getId()), a.getIp(),
a.getPort(), a.getPort()));
} catch (UnknownHostException uhe) {
log.error("Unknown Host [" + a.getIp() + "]: " + uhe.getMessage());
}
}
});
int serverPort = (port == -1) ? BitsquareArgumentParser.PORT_DEFAULT : port;
ActorRef seedNode = actorSystem.actorOf(DHTManager.getProps(), DHTManager.SEED_NAME);
Inbox inbox = Inbox.create(actorSystem);
inbox.send(seedNode, new InitializePeer(Number160.createHash(sid), serverPort, interfaceHint,
peerAddresses));
Thread seedNodeThread = new Thread(() -> {
Boolean quit = false;
while (!quit) {
try {
Object m = inbox.receive(FiniteDuration.create(5L, "seconds"));
if (m instanceof PeerInitialized) {
log.debug("Seed Peer Initialized on port " + ((PeerInitialized) m).getPort
());
}
} catch (Exception e) {
if (!(e instanceof TimeoutException)) {
quit = true;
log.error(e.getMessage());
}
}
}
actorSystem.shutdown();
final Set<PeerAddress> peerAddresses = new HashSet<PeerAddress>();
final String sid = seedID;
SeedNodeAddress.StaticSeedNodeAddresses.getAllSeedNodeAddresses().forEach(a -> {
if (!a.getId().equals(sid)) {
try {
actorSystem.awaitTermination(Duration.create(5L, "seconds"));
} catch (Exception ex) {
if (ex instanceof TimeoutException)
log.error("ActorSystem did not shutdown properly.");
else
log.error(ex.getMessage());
peerAddresses.add(new PeerAddress(Number160.createHash(a.getId()), a.getIp(),
a.getPort(), a.getPort()));
} catch (UnknownHostException uhe) {
log.error("Unknown Host [" + a.getIp() + "]: " + uhe.getMessage());
}
});
seedNodeThread.start();
}
}
});
int serverPort = (port == -1) ? BitsquareArgumentParser.PORT_DEFAULT : port;
ActorRef seedNode = actorSystem.actorOf(DHTManager.getProps(), DHTManager.SEED_NAME);
Inbox inbox = Inbox.create(actorSystem);
inbox.send(seedNode, new InitializePeer(Number160.createHash(sid), serverPort, interfaceHint,
peerAddresses));
Thread seedNodeThread = new Thread(() -> {
Boolean quit = false;
while (!quit) {
try {
Object m = inbox.receive(FiniteDuration.create(5L, "seconds"));
if (m instanceof PeerInitialized) {
log.debug("Seed Peer Initialized on port " + ((PeerInitialized) m).getPort
());
}
} catch (Exception e) {
if (!(e instanceof TimeoutException)) {
quit = true;
log.error(e.getMessage());
}
}
}
actorSystem.shutdown();
try {
actorSystem.awaitTermination(Duration.create(5L, "seconds"));
} catch (Exception ex) {
if (ex instanceof TimeoutException)
log.error("ActorSystem did not shutdown properly.");
else
log.error(ex.getMessage());
}
});
seedNodeThread.start();
}
}

View File

@ -25,6 +25,7 @@ import io.bitsquare.gui.util.ImageUtil;
import io.bitsquare.persistence.Persistence;
import io.bitsquare.settings.Settings;
import io.bitsquare.user.User;
import io.bitsquare.util.BitsquareArgumentParser;
import io.bitsquare.util.ViewLoader;
import com.google.common.base.Throwables;
@ -44,20 +45,29 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lighthouse.files.AppDirectory;
import net.sourceforge.argparse4j.inf.Namespace;
public class BitsquareUI extends Application {
private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class);
private static String appName = "Bitsquare";
private BitsquareModule bitsquareModule;
private Injector injector;
public static void main(String[] args) {
BitsquareArgumentParser parser = new BitsquareArgumentParser();
Namespace namespace = parser.parseArgs(args);
if (namespace.getString(BitsquareArgumentParser.NAME_FLAG) != null) {
appName = appName + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
}
Application.launch(BitsquareUI.class, args);
}
@Override
public void start(Stage primaryStage) {
bitsquareModule = new BitsquareModule(primaryStage);
bitsquareModule = new BitsquareModule(primaryStage, appName);
injector = Guice.createInjector(bitsquareModule);
@ -70,7 +80,7 @@ public class BitsquareUI extends Application {
// configure the Bitsquare application data directory
try {
AppDirectory.initAppDir(Bitsquare.getAppName());
AppDirectory.initAppDir(appName);
} catch (IOException e) {
log.error(e.getMessage());
}
@ -113,7 +123,7 @@ public class BitsquareUI extends Application {
// configure the primary stage
primaryStage.setTitle("Bitsquare (" + Bitsquare.getAppName() + ")");
primaryStage.setTitle("Bitsquare (" + appName + ")");
primaryStage.setScene(scene);
primaryStage.setMinWidth(75);
primaryStage.setMinHeight(50);

View File

@ -17,7 +17,6 @@
package io.bitsquare.btc;
import io.bitsquare.Bitsquare;
import io.bitsquare.btc.listeners.AddressConfidenceListener;
import io.bitsquare.btc.listeners.BalanceListener;
import io.bitsquare.btc.listeners.TxConfidenceListener;
@ -73,6 +72,7 @@ import java.util.stream.Collectors;
import javax.annotation.concurrent.GuardedBy;
import javax.inject.Inject;
import javax.inject.Named;
import javafx.application.Platform;
import javafx.util.Pair;
@ -92,14 +92,13 @@ import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN;
public class WalletFacade {
private static final Logger log = LoggerFactory.getLogger(WalletFacade.class);
public static final String WALLET_PREFIX = Bitsquare.getAppName();
private final ReentrantLock lock = Threading.lock("lock");
private final NetworkParameters params;
private WalletAppKit walletAppKit;
private final FeePolicy feePolicy;
private final CryptoFacade cryptoFacade;
private final Persistence persistence;
private final String appName;
// private final List<DownloadListener> downloadListeners = new CopyOnWriteArrayList<>();
private final List<AddressConfidenceListener> addressConfidenceListeners = new CopyOnWriteArrayList<>();
private final List<TxConfidenceListener> txConfidenceListeners = new CopyOnWriteArrayList<>();
@ -118,11 +117,12 @@ public class WalletFacade {
@Inject
public WalletFacade(NetworkParameters params, FeePolicy feePolicy, CryptoFacade cryptoFacade,
Persistence persistence) {
Persistence persistence, @Named("appName") String appName) {
this.params = params;
this.feePolicy = feePolicy;
this.cryptoFacade = cryptoFacade;
this.persistence = persistence;
this.appName = appName;
}
@ -138,7 +138,7 @@ public class WalletFacade {
Threading.USER_THREAD = Platform::runLater;
// If seed is non-null it means we are restoring from backup.
walletAppKit = new WalletAppKit(params, AppDirectory.dir().toFile(), WALLET_PREFIX) {
walletAppKit = new WalletAppKit(params, AppDirectory.dir().toFile(), appName) {
@Override
protected void onSetupCompleted() {
// Don't make the user wait for confirmations for now, as the intention is they're sending it

View File

@ -17,7 +17,6 @@
package io.bitsquare.di;
import io.bitsquare.Bitsquare;
import io.bitsquare.btc.BitcoinModule;
import io.bitsquare.crypto.CryptoModule;
import io.bitsquare.gui.GuiModule;
@ -48,14 +47,16 @@ public class BitsquareModule extends AbstractBitsquareModule {
private static final Logger log = LoggerFactory.getLogger(BitsquareModule.class);
private final Stage primaryStage;
private final String appName;
public BitsquareModule(Stage primaryStage) {
this(primaryStage, ConfigLoader.loadConfig());
public BitsquareModule(Stage primaryStage, String appName) {
this(primaryStage, appName, ConfigLoader.loadConfig());
}
public BitsquareModule(Stage primaryStage, Properties properties) {
public BitsquareModule(Stage primaryStage, String appName, Properties properties) {
super(properties);
this.primaryStage = primaryStage;
this.appName = appName;
}
@Override
@ -70,7 +71,8 @@ public class BitsquareModule extends AbstractBitsquareModule {
install(tradeModule());
install(guiModule());
bind(ActorSystem.class).toInstance(ActorSystem.create(Bitsquare.getAppName()));
bindConstant().annotatedWith(Names.named("appName")).to(appName);
bind(ActorSystem.class).toInstance(ActorSystem.create(appName));
int randomPort = new Ports().tcpPort();
bindConstant().annotatedWith(Names.named("clientPort")).to(randomPort);

View File

@ -17,7 +17,6 @@
package io.bitsquare.gui.main;
import io.bitsquare.Bitsquare;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.OverlayManager;
@ -35,6 +34,7 @@ import java.net.URL;
import java.util.ResourceBundle;
import javax.inject.Inject;
import javax.inject.Named;
import javafx.animation.Interpolator;
import javafx.application.Platform;
@ -56,9 +56,9 @@ public class MainViewCB extends ViewCB<MainPM> {
private final Navigation navigation;
private final OverlayManager overlayManager;
private Settings settings;
private final ToggleGroup navButtonsGroup = new ToggleGroup();
private final Settings settings;
private final String appName;
private BorderPane baseApplicationContainer;
private VBox splashScreen;
@ -76,12 +76,13 @@ public class MainViewCB extends ViewCB<MainPM> {
@Inject
private MainViewCB(MainPM presentationModel, Navigation navigation, OverlayManager overlayManager,
TradeManager tradeManager, Settings settings) {
TradeManager tradeManager, Settings settings, @Named("appName") String appName) {
super(presentationModel);
this.navigation = navigation;
this.overlayManager = overlayManager;
this.settings = settings;
this.appName = appName;
tradeManager.featureNotImplementedWarningProperty().addListener((ov, oldValue, newValue) -> {
if (oldValue == null && newValue != null) {
@ -205,8 +206,8 @@ public class MainViewCB extends ViewCB<MainPM> {
numPendingTradesLabel.setText(String.valueOf(numPendingTrades));
}
log.trace("openInfoNotification " + Bitsquare.getAppName());
SystemNotification.openInfoNotification(Bitsquare.getAppName(), "You got a new trade message.");
log.trace("openInfoNotification " + appName);
SystemNotification.openInfoNotification(appName, "You got a new trade message.");
}
else {
if (portfolioButtonButtonPane.getChildren().size() > 1)

View File

@ -17,7 +17,6 @@
package io.bitsquare.persistence;
import io.bitsquare.Bitsquare;
import io.bitsquare.util.FileUtil;
import org.bitcoinj.utils.Threading;
@ -39,6 +38,7 @@ import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.concurrent.GuardedBy;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -52,19 +52,20 @@ public class Persistence {
private static final Logger log = LoggerFactory.getLogger(Persistence.class);
private static final ReentrantLock lock = Threading.lock("Storage");
private final String prefix = Bitsquare.getAppName() + "_pref";
private final File storageFile = FileUtil.getFile(prefix, "ser");
@GuardedBy("lock")
private Map<String, Serializable> rootMap = new HashMap<>();
private final String prefix;
private final File storageFile;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public Persistence() {
public Persistence(@Named("appName") String appName) {
this.prefix = appName + "_pref";
this.storageFile = FileUtil.getFile(prefix, "ser");
}
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -58,8 +58,14 @@ public class BitsquareArgumentParser {
.help("Append name to application name.");
}
public Namespace parseArgs(String... args) throws ArgumentParserException {
return parser.parseArgs(args);
public Namespace parseArgs(String... args) {
try {
return parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
return null;
}
}
public void handleError(ArgumentParserException e) {

View File

@ -58,7 +58,7 @@ public class ViewLoaderTest {
@Before
public void setUp() {
Injector injector = Guice.createInjector(new BitsquareModule(TestApp.primaryStage));
Injector injector = Guice.createInjector(new BitsquareModule(TestApp.primaryStage, "testApp"));
ViewLoader.setInjector(injector);
}