Favor use of Properties vs. for configuration

This reverts a number of changes made in commit 3033a19. Primary changes
include:

 - Restoring the immutability of the Node class

 - The argparse4j Namespace object is no longer passed down through
   Guice modules

 - Instead, arguments are eagerly read from the Namespace object by the
   #main method and these values are used to populate the Properties
   object that is already supplied to each Guice module

Other changes include:

 - The addition of a BootstrapNodes#DEFAULT_BOOTSTRAP_NODE field as
   a convenient alias to BootstrapNodes#DIGITAL_OCEAN_1 (or whatever the
   future default bootstrap node may be)

 - A Node#getPortAsString method has been added for convenience when
   dealing with String-based properties

 - A variant of the Node#at static factory method has been added which
   accepts the port value as a String vs. an int--again this is for
   convenience when dealing with String-based properties

 - Tests have been added to NodeTests to reflect the above
This commit is contained in:
Chris Beams 2014-11-09 22:14:18 +01:00
parent 017ebb3f38
commit 162fc3da0e
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
9 changed files with 64 additions and 57 deletions

View File

@ -40,7 +40,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
import net.sourceforge.argparse4j.inf.Namespace;
import scala.concurrent.duration.Duration;
/**
@ -49,12 +48,10 @@ import scala.concurrent.duration.Duration;
public class AppModule extends BitsquareModule {
private static final Logger log = LoggerFactory.getLogger(AppModule.class);
private Namespace argumentsNamespace;
private final String appName;
public AppModule(Properties properties, Namespace argumentsNamespace, String appName) {
public AppModule(Properties properties, String appName) {
super(properties);
this.argumentsNamespace = argumentsNamespace;
this.appName = appName;
}
@ -78,7 +75,7 @@ public class AppModule extends BitsquareModule {
}
protected MessageModule messageModule() {
return new TomP2PMessageModule(properties, argumentsNamespace);
return new TomP2PMessageModule(properties);
}
protected BitcoinModule bitcoinModule() {

View File

@ -26,6 +26,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.ConfigLoader;
import com.google.common.base.Throwables;
@ -34,6 +35,8 @@ import com.google.inject.Injector;
import java.io.IOException;
import java.util.Properties;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.*;
@ -46,25 +49,39 @@ import org.slf4j.LoggerFactory;
import lighthouse.files.AppDirectory;
import net.sourceforge.argparse4j.inf.Namespace;
import static io.bitsquare.app.ArgumentParser.*;
public class Main extends Application {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private static Namespace argumentsNamespace;
private static String appName = "Bitsquare";
private static Properties properties;
private MainModule mainModule;
private Injector injector;
public static void main(String[] args) {
argumentsNamespace = new ArgumentParser().parseArgs(args);
properties = ConfigLoader.loadConfig(appName);
Namespace argumentsNamespace = new ArgumentParser().parseArgs(args);
if (argumentsNamespace.getString(NAME_FLAG) != null)
appName = appName + "-" + argumentsNamespace.getString(NAME_FLAG);
if (argumentsNamespace.getString(SEED_ID_FLAG) != null)
properties.setProperty(SEED_ID_FLAG, argumentsNamespace.getString(SEED_ID_FLAG));
if (argumentsNamespace.getString(SEED_IP_FLAG) != null)
properties.setProperty(SEED_IP_FLAG, argumentsNamespace.getString(SEED_IP_FLAG));
if (argumentsNamespace.getString(SEED_PORT_FLAG) != null)
properties.setProperty(SEED_PORT_FLAG, argumentsNamespace.getString(SEED_PORT_FLAG));
Application.launch(Main.class, args);
}
@Override
public void start(Stage primaryStage) {
String appName = "Bitsquare";
if (argumentsNamespace.getString(ArgumentParser.NAME_FLAG) != null)
appName = "Bitsquare-" + argumentsNamespace.getString(ArgumentParser.NAME_FLAG);
mainModule = new MainModule(appName, argumentsNamespace, primaryStage);
mainModule = new MainModule(properties, appName, primaryStage);
injector = Guice.createInjector(mainModule);

View File

@ -20,28 +20,25 @@ package io.bitsquare.app.gui;
import io.bitsquare.BitsquareModule;
import io.bitsquare.app.AppModule;
import io.bitsquare.gui.GuiModule;
import io.bitsquare.util.ConfigLoader;
import java.util.Properties;
import javafx.stage.Stage;
import net.sourceforge.argparse4j.inf.Namespace;
class MainModule extends BitsquareModule {
private final String appName;
private final Stage primaryStage;
private final Namespace argumentsNamespace;
public MainModule(String appName, Namespace argumentsNamespace, Stage primaryStage) {
super(ConfigLoader.loadConfig(appName));
public MainModule(Properties properties, String appName, Stage primaryStage) {
super(properties);
this.appName = appName;
this.argumentsNamespace = argumentsNamespace;
this.primaryStage = primaryStage;
}
@Override
protected void configure() {
install(new AppModule(properties, argumentsNamespace, appName));
install(new AppModule(properties, appName));
install(new GuiModule(properties, primaryStage));
}
}

View File

@ -27,15 +27,13 @@ import com.google.inject.name.Names;
import java.util.Properties;
import net.sourceforge.argparse4j.inf.Namespace;
import static io.bitsquare.app.ArgumentParser.*;
import static io.bitsquare.network.BootstrapNodes.DEFAULT_BOOTSTRAP_NODE;
public abstract class MessageModule extends BitsquareModule {
private final Namespace argumentsNamespace;
protected MessageModule(Properties properties, Namespace argumentsNamespace) {
protected MessageModule(Properties properties) {
super(properties);
this.argumentsNamespace = argumentsNamespace;
}
@Override
@ -46,18 +44,13 @@ public abstract class MessageModule extends BitsquareModule {
// we will probably later use disk storage instead of memory storage for TomP2P
bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false);
Node bootstrapNode = BootstrapNodes.DIGITAL_OCEAN_1;
// Passed program args will override the properties of the default bootstrapNode
// So you can use the same id and ip but different ports (e.g. running several nodes on one server with
// different ports)
if (argumentsNamespace.getString(ArgumentParser.SEED_ID_FLAG) != null)
bootstrapNode.setId(argumentsNamespace.getString(ArgumentParser.SEED_ID_FLAG));
Node bootstrapNode = Node.at(
properties.getProperty(SEED_ID_FLAG, DEFAULT_BOOTSTRAP_NODE.getId()),
properties.getProperty(SEED_IP_FLAG, DEFAULT_BOOTSTRAP_NODE.getIp()),
properties.getProperty(SEED_PORT_FLAG, DEFAULT_BOOTSTRAP_NODE.getPortAsString())
);
if (argumentsNamespace.getString(ArgumentParser.SEED_IP_FLAG) != null)
bootstrapNode.setIp(argumentsNamespace.getString(ArgumentParser.SEED_IP_FLAG));
if (argumentsNamespace.getString(ArgumentParser.SEED_PORT_FLAG) != null)
bootstrapNode.setPort(Integer.valueOf(argumentsNamespace.getString(ArgumentParser.SEED_PORT_FLAG)));
System.out.println("bootstrapNode = " + bootstrapNode);
bind(Node.class)
.annotatedWith(Names.named("bootstrapNode"))

View File

@ -22,12 +22,10 @@ import io.bitsquare.msg.MessageModule;
import java.util.Properties;
import net.sourceforge.argparse4j.inf.Namespace;
public class TomP2PMessageModule extends MessageModule {
public TomP2PMessageModule(Properties properties, Namespace argumentsNamespace) {
super(properties, argumentsNamespace);
public TomP2PMessageModule(Properties properties) {
super(properties);
}
@Override

View File

@ -20,7 +20,7 @@ package io.bitsquare.network;
import java.util.Arrays;
import java.util.List;
// Ports 7366-7390 are not registered @see
// Ports 7366-7390 are not registered @see
// <a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?&page=103</a>
// Lets use ports in that range 7366-7390
// 7366 will be used as default port
@ -28,6 +28,8 @@ public interface BootstrapNodes {
Node LOCALHOST = Node.at("localhost", "127.0.0.1");
Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109");
Node DEFAULT_BOOTSTRAP_NODE = DIGITAL_OCEAN_1;
static List<Node> all() {
return Arrays.asList(
LOCALHOST, DIGITAL_OCEAN_1

View File

@ -22,9 +22,9 @@ import com.google.common.base.Objects;
public final class Node {
public static final int DEFAULT_PORT = 7366;
private String id;
private String ip;
private int port;
private final String id;
private final String ip;
private final int port;
private Node(String id, String ip, int port) {
this.id = id;
@ -40,16 +40,8 @@ public final class Node {
return new Node(id, ip, port);
}
public void setId(String id) {
this.id = id;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(int port) {
this.port = port;
public static Node at(String id, String ip, String port) {
return new Node(id, ip, Integer.valueOf(port));
}
public String getId() {
@ -64,6 +56,10 @@ public final class Node {
return port;
}
public String getPortAsString() {
return String.valueOf(port);
}
@Override
public boolean equals(Object object) {
if (this == object)

View File

@ -25,6 +25,7 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.HashMap;
import java.util.Properties;
import javafx.application.Application;
import javafx.stage.Stage;
@ -63,9 +64,7 @@ public class ViewLoaderTests {
@Before
public void setUp() {
Injector injector = Guice.createInjector(new MainModule("testApp",
new Namespace(new HashMap<>()),
TestApp.primaryStage));
Injector injector = Guice.createInjector(new MainModule(new Properties(), "testApp", TestApp.primaryStage));
ViewLoader.setInjector(injector);
}

View File

@ -44,6 +44,14 @@ public class NodeTests {
Node node2 = Node.at("bitsquare2.example.com", "203.0.113.2");
assertThat(node1a.hashCode(), equalTo(node1b.hashCode()));
assertThat(node1a.hashCode(), not(equalTo(node2.hashCode())));
assertThat(node1a.getPort(), equalTo(Node.DEFAULT_PORT));
assertThat(node1a.getPortAsString(), equalTo(String.valueOf(Node.DEFAULT_PORT)));
Node node3a = Node.at("bitsquare3.example.com", "203.0.113.3", 1234);
Node node3b = Node.at("bitsquare3.example.com", "203.0.113.3", "1234");
assertThat(node3a, equalTo(node3b));
}
@Test