diff --git a/broker/src/main/java/org/eclipse/moquette/server/ConfigurationParser.java b/broker/src/main/java/org/eclipse/moquette/server/ConfigurationParser.java index 3e915247..31121d53 100644 --- a/broker/src/main/java/org/eclipse/moquette/server/ConfigurationParser.java +++ b/broker/src/main/java/org/eclipse/moquette/server/ConfigurationParser.java @@ -22,7 +22,9 @@ import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.text.ParseException; +import java.util.Map.Entry; import java.util.Properties; + import org.eclipse.moquette.commons.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,11 +44,24 @@ class ConfigurationParser { private Properties m_properties = new Properties(); ConfigurationParser() { + createDefaults(); + } + + ConfigurationParser(Properties properties) { + createDefaults(); + for (Entry entrySet : m_properties.entrySet()) { + if (properties.containsKey(entrySet.getKey())) { + entrySet.setValue(properties.get(entrySet.getKey())); + } + } + } + + private void createDefaults() { m_properties.put("port", Integer.toString(Constants.PORT)); m_properties.put("host", Constants.HOST); m_properties.put("websocket_port", Integer.toString(Constants.WEBSOCKET_PORT)); m_properties.put("password_file", ""); - m_properties.put("persistent_store", System.getProperty("user.home") + File.separator + "moquette_store.mapdb"); + m_properties.put(Constants.PERSISTENT_STORE_PROPERTY_NAME, Constants.DEFAULT_PERSISTENT_PATH); } /** diff --git a/broker/src/main/java/org/eclipse/moquette/server/Server.java b/broker/src/main/java/org/eclipse/moquette/server/Server.java index 0c915553..d7f58dae 100644 --- a/broker/src/main/java/org/eclipse/moquette/server/Server.java +++ b/broker/src/main/java/org/eclipse/moquette/server/Server.java @@ -19,6 +19,8 @@ import java.io.File; import java.io.IOException; import java.text.ParseException; import java.util.Properties; + +import org.eclipse.moquette.commons.Constants; import org.eclipse.moquette.spi.impl.SimpleMessaging; import org.eclipse.moquette.server.netty.NettyAcceptor; import org.slf4j.Logger; @@ -33,6 +35,7 @@ public class Server { private ServerAcceptor m_acceptor; SimpleMessaging messaging; + Properties m_properties; public static void main(String[] args) throws IOException { final Server server = new Server(); @@ -68,26 +71,28 @@ public class Server { } catch (ParseException pex) { LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex); } - Properties configProps = confParser.getProperties(); - startServer(configProps); + m_properties = confParser.getProperties(); + startServer(m_properties); } /** * Starts the server with the given properties. * - * Its need at least the following properties: + * Its suggested to at least have the following properties: * */ public void startServer(Properties configProps) throws IOException { - LOG.info("Persistent store file: " + configProps.get("persistent_store")); + ConfigurationParser confParser = new ConfigurationParser(configProps); + m_properties = confParser.getProperties(); + LOG.info("Persistent store file: " + m_properties.get(Constants.PERSISTENT_STORE_PROPERTY_NAME)); messaging = SimpleMessaging.getInstance(); - messaging.init(configProps); + messaging.init(m_properties); m_acceptor = new NettyAcceptor(); - m_acceptor.initialize(messaging, configProps); + m_acceptor.initialize(messaging, m_properties); } public void stopServer() { @@ -96,4 +101,8 @@ public class Server { m_acceptor.close(); System.out.println("Server stopped"); } + + public Properties getProperties() { + return m_properties; + } } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationFuseTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationFuseTest.java index be76cf71..1e578712 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationFuseTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationFuseTest.java @@ -45,15 +45,10 @@ public class ServerIntegrationFuseTest { MQTT m_mqtt; BlockingConnection m_subscriber; BlockingConnection m_publisher; - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; protected void startServer() throws IOException { - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before @@ -78,7 +73,7 @@ public class ServerIntegrationFuseTest { } m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationPahoTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationPahoTest.java index 3a8d33d8..9f0c4e6f 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationPahoTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationPahoTest.java @@ -34,13 +34,9 @@ public class ServerIntegrationPahoTest { static MqttClientPersistence s_dataStore; static MqttClientPersistence s_pubDataStore; - Server m_server; IMqttClient m_client; TestCallback m_callback; - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; @BeforeClass public static void beforeTests() { @@ -50,16 +46,14 @@ public class ServerIntegrationPahoTest { } protected void startServer() throws IOException { - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before public void setUp() throws Exception { - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); - assertFalse(String.format("The DB storagefile %s already exists", properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)), dbFile.exists()); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); + assertFalse(String.format("The DB storagefile %s already exists", m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)), dbFile.exists()); startServer(); @@ -75,7 +69,7 @@ public class ServerIntegrationPahoTest { } m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationQoSValidationTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationQoSValidationTest.java index 1fd28950..eee014e9 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationQoSValidationTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationQoSValidationTest.java @@ -50,10 +50,6 @@ public class ServerIntegrationQoSValidationTest { IMqttClient m_subscriber; IMqttClient m_publisher; TestCallback m_callback; - - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; @BeforeClass public static void beforeTests() { @@ -63,10 +59,8 @@ public class ServerIntegrationQoSValidationTest { } protected void startServer() throws IOException { - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before @@ -95,7 +89,7 @@ public class ServerIntegrationQoSValidationTest { } m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationSSLTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationSSLTest.java index 87ab2b82..38f20ab3 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationSSLTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationSSLTest.java @@ -60,10 +60,6 @@ public class ServerIntegrationSSLTest { IMqttClient m_client; TestCallback m_callback; - - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; @BeforeClass public static void beforeTests() { @@ -74,16 +70,14 @@ public class ServerIntegrationSSLTest { protected void startServer() throws IOException { String file = getClass().getResource("/").getPath(); System.setProperty("moquette.path", file); - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before public void setUp() throws Exception { - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); - assertFalse(String.format("The DB storagefile %s already exists", properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)), dbFile.exists()); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); + assertFalse(String.format("The DB storagefile %s already exists", m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)), dbFile.exists()); startServer(); @@ -101,7 +95,7 @@ public class ServerIntegrationSSLTest { } m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationWebSocketTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationWebSocketTest.java index c509296c..314acd83 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationWebSocketTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerIntegrationWebSocketTest.java @@ -44,16 +44,10 @@ public class ServerIntegrationWebSocketTest { Server m_server; WebSocketClient client; - - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; protected void startServer() throws IOException { - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before @@ -67,7 +61,7 @@ public class ServerIntegrationWebSocketTest { client.stop(); m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerLowlevelMessagesIntegrationTests.java b/broker/src/test/java/org/eclipse/moquette/server/ServerLowlevelMessagesIntegrationTests.java index b61452c4..64c1703d 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerLowlevelMessagesIntegrationTests.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerLowlevelMessagesIntegrationTests.java @@ -50,16 +50,10 @@ public class ServerLowlevelMessagesIntegrationTests { Server m_server; Client m_client; MQTT m_subscriberDef; - - Properties properties; - private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; - + protected void startServer() throws IOException { - properties = new Properties(); - properties.put(PERSISTENT_STORE_PROPERTY_NAME, PERSISTENT_STORE_FILE_NAME); m_server = new Server(); - m_server.startServer(properties); + m_server.startServer(new Properties()); } @Before @@ -78,7 +72,7 @@ public class ServerLowlevelMessagesIntegrationTests { Thread.sleep(300); //to let the close event pass before server stop event m_server.stopServer(); LOG.debug("After asked server to stop"); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/server/ServerRestartIntegrationTest.java b/broker/src/test/java/org/eclipse/moquette/server/ServerRestartIntegrationTest.java index 42d7cfc3..40ed8795 100644 --- a/broker/src/test/java/org/eclipse/moquette/server/ServerRestartIntegrationTest.java +++ b/broker/src/test/java/org/eclipse/moquette/server/ServerRestartIntegrationTest.java @@ -43,7 +43,7 @@ public class ServerRestartIntegrationTest { BlockingConnection m_publisher; Properties properties; private final static String PERSISTENT_STORE_PROPERTY_NAME = "persistent_store"; - private final static String PERSISTENT_STORE_FILE_NAME = "store.mapdb"; + private final static String PERSISTENT_STORE_FILE_NAME = System.getProperty("user.home") + File.separator + "moquette_store.mapdb"; protected void startServer() throws IOException { properties = new Properties(); @@ -73,7 +73,7 @@ public class ServerRestartIntegrationTest { } m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } @@ -92,7 +92,7 @@ public class ServerRestartIntegrationTest { //shutdown the server m_server.stopServer(); - File dbFile = new File(properties.getProperty(PERSISTENT_STORE_PROPERTY_NAME)); + File dbFile = new File(m_server.getProperties().getProperty(org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME)); if (dbFile.exists()) { dbFile.delete(); } diff --git a/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java b/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java index 3b56b927..857bd5c2 100644 --- a/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java +++ b/broker/src/test/java/org/eclipse/moquette/spi/impl/MapDBPersistentStoreTest.java @@ -17,8 +17,6 @@ package org.eclipse.moquette.spi.impl; import java.io.File; import java.util.List; -import java.util.Properties; - import org.eclipse.moquette.spi.impl.subscriptions.Subscription; import org.eclipse.moquette.spi.persistence.MapDBPersistentStore; import org.eclipse.moquette.proto.messages.AbstractMessage; @@ -36,16 +34,14 @@ import org.junit.Before; public class MapDBPersistentStoreTest { MapDBPersistentStore m_storageService; - Properties properties; - private final static String PERSISTENT_STORE_PATH = System.getProperty("user.home") + File.separator + "moquette_store.mapdb"; @Before public void setUp() throws Exception { - - File dbFile = new File(PERSISTENT_STORE_PATH); - assertFalse(String.format("The DB storagefile %s already exists", PERSISTENT_STORE_PATH), dbFile.exists()); + + File dbFile = new File(org.eclipse.moquette.commons.Constants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME); + assertFalse(String.format("The DB storagefile %s already exists", org.eclipse.moquette.commons.Constants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME), dbFile.exists()); - m_storageService = new MapDBPersistentStore(PERSISTENT_STORE_PATH); + m_storageService = new MapDBPersistentStore(org.eclipse.moquette.commons.Constants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME); m_storageService.initStore(); } @@ -55,7 +51,7 @@ public class MapDBPersistentStoreTest { m_storageService.close(); } - File dbFile = new File(PERSISTENT_STORE_PATH); + File dbFile = new File(org.eclipse.moquette.commons.Constants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME); if (dbFile.exists()) { dbFile.delete(); } diff --git a/parser_commons/src/main/java/org/eclipse/moquette/commons/Constants.java b/parser_commons/src/main/java/org/eclipse/moquette/commons/Constants.java index 47f209fd..d4fb67a5 100644 --- a/parser_commons/src/main/java/org/eclipse/moquette/commons/Constants.java +++ b/parser_commons/src/main/java/org/eclipse/moquette/commons/Constants.java @@ -15,6 +15,8 @@ */ package org.eclipse.moquette.commons; +import java.io.File; + /** * Contains some useful constants. */ @@ -23,4 +25,7 @@ public class Constants { public static final int WEBSOCKET_PORT = 8080; public static final String HOST = "0.0.0.0"; public static final int DEFAULT_CONNECT_TIMEOUT = 10; + public static final String DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME = "moquette_store.mapdb"; + public static final String DEFAULT_PERSISTENT_PATH = System.getProperty("user.home") + File.separator + DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME; + public static final String PERSISTENT_STORE_PROPERTY_NAME = "persistence_store"; }