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

View file

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

View file

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

View file

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

View file

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

View file

@ -28,6 +28,8 @@ public interface BootstrapNodes {
Node LOCALHOST = Node.at("localhost", "127.0.0.1"); Node LOCALHOST = Node.at("localhost", "127.0.0.1");
Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109"); Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109");
Node DEFAULT_BOOTSTRAP_NODE = DIGITAL_OCEAN_1;
static List<Node> all() { static List<Node> all() {
return Arrays.asList( return Arrays.asList(
LOCALHOST, DIGITAL_OCEAN_1 LOCALHOST, DIGITAL_OCEAN_1

View file

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

View file

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

View file

@ -44,6 +44,14 @@ public class NodeTests {
Node node2 = Node.at("bitsquare2.example.com", "203.0.113.2"); Node node2 = Node.at("bitsquare2.example.com", "203.0.113.2");
assertThat(node1a.hashCode(), equalTo(node1b.hashCode())); assertThat(node1a.hashCode(), equalTo(node1b.hashCode()));
assertThat(node1a.hashCode(), not(equalTo(node2.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 @Test