Update for default configs

Update to allow for default config properties if some are not specified
with the start server and properties passed in.  Updated the tests to
match and also moved a few more things into the Constants.java file.
This commit is contained in:
swarwick
2015-03-14 16:37:41 +01:00
committed by andrea
parent 8aad95525c
commit 6cc5b0edb9
11 changed files with 61 additions and 71 deletions
@@ -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<Object, Object> 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);
}
/**
@@ -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:
* <ul>
* <li>port</li>
* <li>password_file</li>
* </ul>
*/
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;
}
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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";
}