From d322d096045a43e2b74f98e25be68b645a0cb23b Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 24 Oct 2015 12:10:06 +0200 Subject: [PATCH] Changed the way Jabit Server is configured (via application.properties) --- .../server/JabitServerApplication.java | 84 ++++--------------- .../bitmessage/server/JabitServerConfig.java | 74 ++++++++++++++++ src/main/resources/application.properties | 5 +- src/test/resources/application.properties | 6 ++ 4 files changed, 98 insertions(+), 71 deletions(-) create mode 100644 src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java create mode 100644 src/test/resources/application.properties diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java index bca5b32..f5f1681 100644 --- a/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerApplication.java @@ -19,29 +19,20 @@ package ch.dissem.bitmessage.server; import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; -import ch.dissem.bitmessage.networking.DefaultNetworkHandler; -import ch.dissem.bitmessage.ports.MemoryNodeRegistry; -import ch.dissem.bitmessage.repository.JdbcAddressRepository; -import ch.dissem.bitmessage.repository.JdbcConfig; -import ch.dissem.bitmessage.repository.JdbcInventory; -import ch.dissem.bitmessage.repository.JdbcMessageRepository; -import ch.dissem.bitmessage.security.bc.BouncySecurity; import ch.dissem.bitmessage.server.entities.Broadcasts; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.inject.Inject; import java.util.List; -import java.util.Properties; import java.util.Set; import java.util.Timer; @@ -50,7 +41,7 @@ import static ch.dissem.bitmessage.server.Converter.message; @CrossOrigin @RestController -@EnableAutoConfiguration +@SpringBootApplication public class JabitServerApplication { private static final Logger LOG = LoggerFactory.getLogger(JabitServerApplication.class); @@ -61,9 +52,13 @@ public class JabitServerApplication { private static final int SHORTLIST_SIZE = 5; - private final Set whitelist; - private final Set shortlist; - private final Set blacklist; + @Resource + private Set whitelist; + @Resource + private Set shortlist; + @Resource + private Set blacklist; + @Inject private BitmessageContext ctx; @RequestMapping("status") @@ -100,60 +95,9 @@ public class JabitServerApplication { return broadcasts(broadcaster, messages); } - public JabitServerApplication() { - whitelist = Utils.readOrCreateList( - "whitelist.conf", - "# If there are any Bitmessage addresses in the whitelist, only those will be shown.\n" + - "# blacklist.conf will be ignored, but shortlist.conf will be applied to whitelisted addresses.\n" - ); - shortlist = Utils.readOrCreateList( - "shortlist.conf", - "# Broadcasts of these addresses will be restricted to the last " + SHORTLIST_SIZE + " entries.\n\n" + - "# Time Service:\n" + - "BM-BcbRqcFFSQUUmXFKsPJgVQPSiFA3Xash\n\n" + - "# Q's Aktivlist:\n" + - "BM-GtT7NLCCAu3HrT7dNTUTY9iDns92Z2ND\n" - ); - blacklist = Utils.readOrCreateList( - "blacklist.conf", - "# Bitmessage addresses in this file are being ignored and their broadcasts won't be returned.\n" - ); - - Properties properties = new Properties(); - int port = 8444; - try { - properties.load(new FileInputStream(CONFIG_FILE)); - String portProperty = properties.getProperty(PROPERTY_PORT); - if (portProperty != null) { - port = Integer.parseInt(portProperty); - } - } catch (FileNotFoundException ignore) { - try { - properties.setProperty(PROPERTY_PORT, String.valueOf(port)); - properties.store(new FileOutputStream(CONFIG_FILE), null); - } catch (IOException e) { - LOG.warn("Couldn't save default config file", e); - } - } catch (IOException e) { - LOG.error("Couldn't load config, using defaults", e); - } catch (NumberFormatException e) { - LOG.error("Couldn't read port property - is it a number?", e); - } - - JdbcConfig config = new JdbcConfig("jdbc:h2:file:jabit;AUTO_SERVER=TRUE", "sa", null); - ctx = new BitmessageContext.Builder() - .addressRepo(new JdbcAddressRepository(config)) - .inventory(new JdbcInventory(config)) - .messageRepo(new JdbcMessageRepository(config)) - .nodeRegistry(new MemoryNodeRegistry()) - .networkHandler(new DefaultNetworkHandler()) - .security(new BouncySecurity()) - .port(port) - .listener(plaintext -> { - }) - .build(); + @PostConstruct + public void setUp() { ctx.startup(); - new Timer().scheduleAtFixedRate(new CleanupJob(ctx), 1 * HOUR, 24 * HOUR); } diff --git a/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java new file mode 100644 index 0000000..066921d --- /dev/null +++ b/src/main/java/ch/dissem/bitmessage/server/JabitServerConfig.java @@ -0,0 +1,74 @@ +package ch.dissem.bitmessage.server; + +import ch.dissem.bitmessage.BitmessageContext; +import ch.dissem.bitmessage.networking.DefaultNetworkHandler; +import ch.dissem.bitmessage.ports.MemoryNodeRegistry; +import ch.dissem.bitmessage.repository.JdbcAddressRepository; +import ch.dissem.bitmessage.repository.JdbcConfig; +import ch.dissem.bitmessage.repository.JdbcInventory; +import ch.dissem.bitmessage.repository.JdbcMessageRepository; +import ch.dissem.bitmessage.security.bc.BouncySecurity; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Set; + +@Configuration +public class JabitServerConfig { + public static final int SHORTLIST_SIZE = 5; + + @Value("${bitmessage.port}") + private int port; + @Value("${bitmessage.connection.ttl.hours}") + private int connectionTTL; + @Value("${bitmessage.connection.limit}") + private int connectionLimit; + + @Bean + public BitmessageContext bitmessageContext() { + JdbcConfig config = new JdbcConfig("jdbc:h2:file:./jabit;AUTO_SERVER=TRUE", "sa", null); + return new BitmessageContext.Builder() + .addressRepo(new JdbcAddressRepository(config)) + .inventory(new JdbcInventory(config)) + .messageRepo(new JdbcMessageRepository(config)) + .nodeRegistry(new MemoryNodeRegistry()) + .networkHandler(new DefaultNetworkHandler()) + .security(new BouncySecurity()) + .port(port) + .connectionLimit(connectionLimit) + .connectionTTL(connectionTTL) + .listener(plaintext -> { + }) + .build(); + } + + @Bean + public Set whitelist() { + return Utils.readOrCreateList( + "whitelist.conf", + "# If there are any Bitmessage addresses in the whitelist, only those will be shown.\n" + + "# blacklist.conf will be ignored, but shortlist.conf will be applied to whitelisted addresses.\n" + ); + } + + @Bean + public Set shortlist() { + return Utils.readOrCreateList( + "shortlist.conf", + "# Broadcasts of these addresses will be restricted to the last " + SHORTLIST_SIZE + " entries.\n\n" + + "# Time Service:\n" + + "BM-BcbRqcFFSQUUmXFKsPJgVQPSiFA3Xash\n\n" + + "# Q's Aktivlist:\n" + + "BM-GtT7NLCCAu3HrT7dNTUTY9iDns92Z2ND\n" + ); + } + + @Bean + public Set blacklist() { + return Utils.readOrCreateList( + "blacklist.conf", + "# Bitmessage addresses in this file are being ignored and their broadcasts won't be returned.\n" + ); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5f53708..1242dbb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,6 @@ logging.file=jabit.log logging.level.*=ERROR -logging.level.ch.dissem.*=WARN \ No newline at end of file +logging.level.ch.dissem.*=WARN +bitmessage.port=8444 +bitmessage.connection.ttl.hours=12 +bitmessage.connection.limit=100 diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..9366013 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,6 @@ +logging.file=jabit.log +logging.level.*=DEBUG +logging.level.ch.dissem.*=WARN +bitmessage.port=18444 +bitmessage.connection.ttl.hours=12 +bitmessage.connection.limit=100