Abstracted configuration part to use filesystem or classpath files

This commit is contained in:
andrea
2015-08-22 15:35:11 +02:00
parent b6554eb0ac
commit 560559fad2
18 changed files with 309 additions and 76 deletions
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2012-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.eclipse.moquette.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.ParseException;
import java.util.Properties;
/**
* Configuration that loads file from the classpath
*
* @author andrea
*/
public class ClasspathConfig implements IConfig {
private static final Logger LOG = LoggerFactory.getLogger(ClasspathConfig.class);
private final Properties m_properties;
public ClasspathConfig() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("config/moquette.conf");
if (is == null) {
throw new RuntimeException("Can't locate the resourse \"config/moquette.conf\"");
}
Reader configReader = new InputStreamReader(is);
ConfigurationParser confParser = new ConfigurationParser();
try {
confParser.parse(configReader);
} catch (ParseException pex) {
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
}
m_properties = confParser.getProperties();
}
@Override
public void setProperty(String name, String value) {
m_properties.setProperty(name, value);
}
@Override
public String getProperty(String name) {
return m_properties.getProperty(name);
}
@Override
public String getProperty(String name, String defaultValue) {
return m_properties.getProperty(name, defaultValue);
}
}
@@ -43,16 +43,6 @@ class ConfigurationParser {
createDefaults();
}
/**
* Crate a ConfigurationParser merging the default properties with the provided ones.
* */
ConfigurationParser(Properties properties) {
this();
for (Entry<Object, Object> entrySet : properties.entrySet()) {
m_properties.put(entrySet.getKey(), entrySet.getValue());
}
}
private void createDefaults() {
m_properties.put(PORT_PROPERTY_NAME, Integer.toString(PORT));
m_properties.put(HOST_PROPERTY_NAME, HOST);
@@ -117,9 +107,9 @@ class ConfigurationParser {
}
//split till the first space
int deilimiterIdx = line.indexOf(' ');
String key = line.substring(0, deilimiterIdx).trim();
String value = line.substring(deilimiterIdx).trim();
int delimiterIdx = line.indexOf(' ');
String key = line.substring(0, delimiterIdx).trim();
String value = line.substring(delimiterIdx).trim();
m_properties.put(key, value);
}
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2012-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.eclipse.moquette.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.text.ParseException;
import java.util.Properties;
/**
* Configuration that loads file from the file system
*
* @author andrea
*/
public class FilesystemConfig implements IConfig {
private static final Logger LOG = LoggerFactory.getLogger(FilesystemConfig.class);
private final Properties m_properties;
public FilesystemConfig(File file) {
ConfigurationParser confParser = new ConfigurationParser();
try {
confParser.parse(file);
} catch (ParseException pex) {
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
}
m_properties = confParser.getProperties();
}
public FilesystemConfig() {
this(defaultConfigFile());
}
private static File defaultConfigFile() {
String configPath = System.getProperty("moquette.path", null);
return new File(configPath, "config/moquette.conf");
}
@Override
public void setProperty(String name, String value) {
m_properties.setProperty(name, value);
}
@Override
public String getProperty(String name) {
return m_properties.getProperty(name);
}
@Override
public String getProperty(String name, String defaultValue) {
return m_properties.getProperty(name, defaultValue);
}
}
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2012-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.eclipse.moquette.server;
/**
* Base interface for all configuration implementations (filesystem, memory or classpath)
*
* @author andrea
*/
public interface IConfig {
void setProperty(String name, String value);
String getProperty(String name);
String getProperty(String name, String defaultValue);
}
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2012-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.eclipse.moquette.server;
import java.util.Map;
import java.util.Properties;
import static org.eclipse.moquette.commons.Constants.*;
import static org.eclipse.moquette.commons.Constants.AUTHENTICATOR_CLASS_NAME;
import static org.eclipse.moquette.commons.Constants.AUTHORIZATOR_CLASS_NAME;
/**
* Configuration backed by memory.
*
* @author andrea
*/
public class MemoryConfig implements IConfig {
private final Properties m_properties = new Properties();
public MemoryConfig(Properties properties) {
createDefaults();
for (Map.Entry<Object, Object> entrySet : properties.entrySet()) {
m_properties.put(entrySet.getKey(), entrySet.getValue());
}
}
private void createDefaults() {
m_properties.put(PORT_PROPERTY_NAME, Integer.toString(PORT));
m_properties.put(HOST_PROPERTY_NAME, HOST);
m_properties.put(WEB_SOCKET_PORT_PROPERTY_NAME, Integer.toString(WEBSOCKET_PORT));
m_properties.put(PASSWORD_FILE_PROPERTY_NAME, "");
m_properties.put(PERSISTENT_STORE_PROPERTY_NAME, DEFAULT_PERSISTENT_PATH);
m_properties.put(ALLOW_ANONYMOUS_PROPERTY_NAME, true);
m_properties.put(AUTHENTICATOR_CLASS_NAME, "");
m_properties.put(AUTHORIZATOR_CLASS_NAME, "");
}
@Override
public void setProperty(String name, String value) {
m_properties.setProperty(name, value);
}
@Override
public String getProperty(String name) {
return m_properties.getProperty(name);
}
@Override
public String getProperty(String name, String defaultValue) {
return m_properties.getProperty(name, defaultValue);
}
}
@@ -15,7 +15,6 @@
*/
package org.eclipse.moquette.server;
import org.eclipse.moquette.interception.InterceptHandler;
import org.eclipse.moquette.server.netty.NettyAcceptor;
import org.eclipse.moquette.spi.impl.SimpleMessaging;
import org.slf4j.Logger;
@@ -23,7 +22,6 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Properties;
import static org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME;
@@ -37,8 +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();
server.startServer();
@@ -54,27 +51,20 @@ public class Server {
/**
* Starts Moquette bringing the configuration from the file
* located at config/moquette.conf
* located at m_config/moquette.conf
*/
public void startServer() throws IOException {
String configPath = System.getProperty("moquette.path", null);
startServer(new File(configPath, "config/moquette.conf"));
final IConfig config = new FilesystemConfig();
startServer(config);
}
/**
* Starts Moquette bringing the configuration from the given file
*/
public void startServer(File configFile) throws IOException {
LOG.info("Using config file: " + configFile.getAbsolutePath());
ConfigurationParser confParser = new ConfigurationParser();
try {
confParser.parse(configFile);
} catch (ParseException pex) {
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
}
m_properties = confParser.getProperties();
startServer(m_properties);
LOG.info("Using m_config file: " + configFile.getAbsolutePath());
final IConfig config = new FilesystemConfig(configFile);
startServer(config);
}
/**
@@ -87,18 +77,24 @@ public class Server {
* </ul>
*/
public void startServer(Properties configProps) throws IOException {
ConfigurationParser confParser = new ConfigurationParser(configProps);
m_properties = confParser.getProperties();
final IConfig config = new MemoryConfig(configProps);
startServer(config);
}
/**
* Starts Moquette bringing the configuration files from the given Config implementation.
*/
public void startServer(IConfig config) throws IOException {
final String handlerProp = System.getProperty("intercept.handler");
if (handlerProp != null) {
m_properties.setProperty("intercept.handler", handlerProp);
config.setProperty("intercept.handler", handlerProp);
}
LOG.info("Persistent store file: " + m_properties.get(PERSISTENT_STORE_PROPERTY_NAME));
LOG.info("Persistent store file: " + config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
messaging = SimpleMessaging.getInstance();
messaging.init(m_properties);
messaging.init(config);
m_acceptor = new NettyAcceptor();
m_acceptor.initialize(messaging, m_properties);
m_acceptor.initialize(messaging, config);
}
public void stopServer() {
@@ -107,8 +103,4 @@ public class Server {
messaging.stop();
LOG.info("Server stopped");
}
public Properties getProperties() {
return m_properties;
}
}
@@ -25,7 +25,7 @@ import org.eclipse.moquette.spi.IMessaging;
*/
public interface ServerAcceptor {
void initialize(IMessaging messaging, Properties props) throws IOException;
void initialize(IMessaging messaging, IConfig props) throws IOException;
void close();
}
@@ -42,14 +42,13 @@ import java.security.*;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.Properties;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import org.eclipse.moquette.commons.Constants;
import org.eclipse.moquette.server.IConfig;
import org.eclipse.moquette.spi.IMessaging;
import org.eclipse.moquette.parser.netty.MQTTDecoder;
import org.eclipse.moquette.parser.netty.MQTTEncoder;
@@ -101,7 +100,7 @@ public class NettyAcceptor implements ServerAcceptor {
MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector();
@Override
public void initialize(IMessaging messaging, Properties props) throws IOException {
public void initialize(IMessaging messaging, IConfig props) throws IOException {
m_bossGroup = new NioEventLoopGroup();
m_workerGroup = new NioEventLoopGroup();
@@ -150,7 +149,7 @@ public class NettyAcceptor implements ServerAcceptor {
}
}
private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException {
private void initializePlainTCPTransport(IMessaging messaging, IConfig props) throws IOException {
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
@@ -170,7 +169,7 @@ public class NettyAcceptor implements ServerAcceptor {
});
}
private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
private void initializeWebSocketTransport(IMessaging messaging, IConfig props) throws IOException {
String webSocketPortProp = props.getProperty(Constants.WEB_SOCKET_PORT_PROPERTY_NAME);
if (webSocketPortProp == null) {
//Do nothing no WebSocket configured
@@ -204,7 +203,7 @@ public class NettyAcceptor implements ServerAcceptor {
});
}
private void initializeSSLTCPTransport(IMessaging messaging, Properties props, final SslHandler sslHandler) throws IOException {
private void initializeSSLTCPTransport(IMessaging messaging, IConfig props, final SslHandler sslHandler) throws IOException {
String sslPortProp = props.getProperty(Constants.SSL_PORT_PROPERTY_NAME);
if (sslPortProp == null) {
//Do nothing no SSL configured
@@ -234,7 +233,7 @@ public class NettyAcceptor implements ServerAcceptor {
});
}
private void initializeWSSTransport(IMessaging messaging, Properties props, final SslHandler sslHandler) throws IOException {
private void initializeWSSTransport(IMessaging messaging, IConfig props, final SslHandler sslHandler) throws IOException {
String sslPortProp = props.getProperty(Constants.WSS_PORT_PROPERTY_NAME);
if (sslPortProp == null) {
//Do nothing no SSL configured
@@ -296,7 +295,7 @@ public class NettyAcceptor implements ServerAcceptor {
}
private SslHandler initSSLHandler(Properties props) {
private SslHandler initSSLHandler(IConfig props) {
final String jksPath = props.getProperty(Constants.JKS_PATH_PROPERTY_NAME);
LOG.info("Starting SSL using keystore at {}", jksPath);
if (jksPath == null || jksPath.isEmpty()) {
@@ -22,6 +22,7 @@ import io.netty.util.internal.StringUtil;
import org.HdrHistogram.Histogram;
import org.eclipse.moquette.interception.InterceptHandler;
import org.eclipse.moquette.proto.messages.AbstractMessage;
import org.eclipse.moquette.server.IConfig;
import org.eclipse.moquette.spi.impl.security.*;
import org.eclipse.moquette.server.ServerChannel;
import org.eclipse.moquette.spi.IMessagesStore;
@@ -99,7 +100,7 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
return INSTANCE;
}
public void init(Properties configProps) {
public void init(IConfig configProps) {
subscriptions = new SubscriptionsStore();
m_executor = Executors.newFixedThreadPool(1);
m_disruptor = new Disruptor<>(ValueEvent.EVENT_FACTORY, 1024 * 32, m_executor);
@@ -113,10 +114,8 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
annotationSupport.processAnnotations(m_processor);
processInit(configProps);
// disruptorPublish(new InitEvent(configProps));
}
private void disruptorPublish(MessagingEvent msgEvent) {
LOG.debug("disruptorPublish publishing event {}", msgEvent);
long sequence = m_ringBuffer.next();
@@ -187,7 +186,7 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
}
}
private void processInit(Properties props) {
private void processInit(IConfig props) {
benchmarkEnabled = Boolean.parseBoolean(System.getProperty("moquette.processor.benchmark", "false"));
//TODO use a property to select the storage path
@@ -45,20 +45,19 @@ public class ConfigurationClassLoaderTest implements IAuthenticator, IAuthorizat
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationClassLoaderTest.class);
Server m_server;
MQTT m_mqtt;
BlockingConnection m_subscriber;
BlockingConnection m_publisher;
IConfig m_config;
protected void startServer(Properties props) throws IOException {
m_server = new Server();
m_server.startServer(props);
m_server = new Server();
m_config = new MemoryConfig(props);
m_server.startServer(m_config);
}
@After
public void tearDown() throws Exception {
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -82,7 +82,7 @@ public class ConfigurationParserTest {
@Test
public void parseCompleteFile() throws ParseException {
String content = "# This is initial config format \r\n"
String content = "# This is initial m_config format \r\n"
+ " \r\n"
+ "port 1234 \r\n"
+ "host localhost \r\n"
@@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import static org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME;
@@ -40,10 +41,13 @@ public class ServerIntegrationFuseTest {
MQTT m_mqtt;
BlockingConnection m_subscriber;
BlockingConnection m_publisher;
IConfig m_config;
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -68,7 +72,7 @@ public class ServerIntegrationFuseTest {
}
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -44,6 +44,7 @@ public class ServerIntegrationPahoTest {
Server m_server;
IMqttClient m_client;
TestCallback m_callback;
IConfig m_config;
@BeforeClass
public static void beforeTests() {
@@ -54,7 +55,9 @@ public class ServerIntegrationPahoTest {
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -81,7 +84,7 @@ public class ServerIntegrationPahoTest {
private void stopServer() {
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -50,6 +50,7 @@ public class ServerIntegrationQoSValidationTest {
IMqttClient m_subscriber;
IMqttClient m_publisher;
TestCallback m_callback;
IConfig m_config;
@BeforeClass
public static void beforeTests() {
@@ -60,7 +61,9 @@ public class ServerIntegrationQoSValidationTest {
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -89,7 +92,7 @@ public class ServerIntegrationQoSValidationTest {
}
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -44,10 +44,13 @@ public class ServerIntegrationWebSocketTest {
Server m_server;
WebSocketClient client;
IConfig m_config;
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -61,7 +64,7 @@ public class ServerIntegrationWebSocketTest {
client.stop();
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -45,10 +46,13 @@ public class ServerLowlevelMessagesIntegrationTests {
Server m_server;
Client m_client;
MQTT m_subscriberDef;
IConfig m_config;
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -67,7 +71,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(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -18,7 +18,6 @@ package org.eclipse.moquette.server;
import org.fusesource.mqtt.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,10 +42,13 @@ public class ServerRestartIntegrationTest {
MQTT m_mqtt;
BlockingConnection m_subscriber;
BlockingConnection m_publisher;
IConfig m_config;
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer(IntegrationUtils.prepareTestPropeties());
final Properties configProps = IntegrationUtils.prepareTestPropeties();
m_config = new MemoryConfig(configProps);
m_server.startServer(m_config);
}
@Before
@@ -70,7 +72,7 @@ public class ServerRestartIntegrationTest {
}
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -89,7 +91,7 @@ public class ServerRestartIntegrationTest {
//shutdown the server
m_server.stopServer();
File dbFile = new File(m_server.getProperties().getProperty(PERSISTENT_STORE_PROPERTY_NAME));
File dbFile = new File(m_config.getProperty(PERSISTENT_STORE_PROPERTY_NAME));
if (dbFile.exists()) {
dbFile.delete();
}
@@ -1,13 +1,17 @@
package org.eclipse.moquette.testembedded;
import org.eclipse.moquette.server.ClasspathConfig;
import org.eclipse.moquette.server.IConfig;
import org.eclipse.moquette.server.Server;
import java.io.IOException;
public class EmbeddedLauncher {
public static void main(String[] args) throws InterruptedException, IOException {
final IConfig classPathConfig = new ClasspathConfig();
final Server mqttBroker = new Server();
mqttBroker.startServer();
mqttBroker.startServer(classPathConfig);
System.out.println("Broker started");
Thread.sleep(1000);
System.out.println("Stopping broker");