Moved packages from old io.moquette.server to new io.moquette.broker

This commit is contained in:
andsel
2018-11-11 18:06:13 +01:00
parent 58fd90d33d
commit 1ba9eb3613
127 changed files with 274 additions and 1414 deletions
@@ -1,64 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClasspathResourceLoader implements IResourceLoader {
private static final Logger LOG = LoggerFactory.getLogger(ClasspathResourceLoader.class);
private final String defaultResource;
private final ClassLoader classLoader;
public ClasspathResourceLoader() {
this(IConfig.DEFAULT_CONFIG);
}
public ClasspathResourceLoader(String defaultResource) {
this(defaultResource, Thread.currentThread().getContextClassLoader());
}
public ClasspathResourceLoader(String defaultResource, ClassLoader classLoader) {
this.defaultResource = defaultResource;
this.classLoader = classLoader;
}
@Override
public Reader loadDefaultResource() {
return loadResource(defaultResource);
}
@Override
public Reader loadResource(String relativePath) {
LOG.info("Loading resource. RelativePath = {}.", relativePath);
InputStream is = this.classLoader.getResourceAsStream(relativePath);
return is != null ? new InputStreamReader(is, StandardCharsets.UTF_8) : null;
}
@Override
public String getName() {
return "classpath resource";
}
}
@@ -1,121 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.ParseException;
import java.util.Properties;
/**
* Mosquitto configuration parser.
*
* A line that at the very first has # is a comment Each line has key value format, where the
* separator used it the space.
*/
class ConfigurationParser {
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationParser.class);
private Properties m_properties = new Properties();
/**
* Parse the configuration from file.
*/
void parse(File file) throws ParseException {
if (file == null) {
LOG.warn("parsing NULL file, so fallback on default configuration!");
return;
}
if (!file.exists()) {
LOG.warn(
String.format(
"parsing not existing file %s, so fallback on default configuration!",
file.getAbsolutePath()));
return;
}
try {
Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
parse(reader);
} catch (IOException fex) {
LOG.warn("parsing not existing file {}, fallback on default configuration!", file.getAbsolutePath(), fex);
}
}
/**
* Parse the configuration
*
* @throws ParseException
* if the format is not compliant.
*/
void parse(Reader reader) throws ParseException {
if (reader == null) {
// just log and return default properties
LOG.warn("parsing NULL reader, so fallback on default configuration!");
return;
}
BufferedReader br = new BufferedReader(reader);
String line;
try {
while ((line = br.readLine()) != null) {
int commentMarker = line.indexOf('#');
if (commentMarker != -1) {
if (commentMarker == 0) {
// skip its a comment
continue;
} else {
// it's a malformed comment
throw new ParseException(line, commentMarker);
}
} else {
if (line.isEmpty() || line.matches("^\\s*$")) {
// skip it's a black line
continue;
}
// split till the first space
int delimiterIdx = line.indexOf(' ');
String key = line.substring(0, delimiterIdx).trim();
String value = line.substring(delimiterIdx).trim();
m_properties.put(key, value);
}
}
} catch (IOException ex) {
throw new ParseException("Failed to read", 1);
} finally {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
Properties getProperties() {
return m_properties;
}
}
@@ -1,88 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.nio.charset.StandardCharsets.UTF_8;
public class FileResourceLoader implements IResourceLoader {
private static final Logger LOG = LoggerFactory.getLogger(FileResourceLoader.class);
private final File defaultFile;
private final String parentPath;
public FileResourceLoader() {
this((File) null);
}
public FileResourceLoader(File defaultFile) {
this(defaultFile, System.getProperty("moquette.path", null));
}
public FileResourceLoader(String parentPath) {
this(null, parentPath);
}
public FileResourceLoader(File defaultFile, String parentPath) {
this.defaultFile = defaultFile;
this.parentPath = parentPath;
}
@Override
public Reader loadDefaultResource() {
if (defaultFile != null) {
return loadResource(defaultFile);
} else {
throw new IllegalArgumentException("Default file not set!");
}
}
@Override
public Reader loadResource(String relativePath) {
return loadResource(new File(parentPath, relativePath));
}
public Reader loadResource(File f) {
LOG.info("Loading file. Path = {}.", f.getAbsolutePath());
if (f.isDirectory()) {
LOG.error("The given file is a directory. Path = {}.", f.getAbsolutePath());
throw new ResourceIsDirectoryException("File \"" + f + "\" is a directory!");
}
try {
return Files.newBufferedReader(f.toPath(), UTF_8);
} catch (IOException e) {
LOG.error("The file does not exist. Path = {}.", f.getAbsolutePath());
return null;
}
}
@Override
public String getName() {
return "file";
}
}
@@ -1,79 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import io.moquette.BrokerConstants;
/**
* Base interface for all configuration implementations (filesystem, memory or classpath)
*/
public abstract class IConfig {
public static final String DEFAULT_CONFIG = "config/moquette.conf";
public abstract void setProperty(String name, String value);
/**
* Same semantic of Properties
*
* @param name property name.
* @return property value.
* */
public abstract String getProperty(String name);
/**
* Same semantic of Properties
*
* @param name property name.
* @param defaultValue default value to return in case the property doesn't exists.
* @return property value.
* */
public abstract String getProperty(String name, String defaultValue);
void assignDefaults() {
setProperty(BrokerConstants.PORT_PROPERTY_NAME, Integer.toString(BrokerConstants.PORT));
setProperty(BrokerConstants.HOST_PROPERTY_NAME, BrokerConstants.HOST);
// setProperty(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME,
// Integer.toString(BrokerConstants.WEBSOCKET_PORT));
setProperty(BrokerConstants.PASSWORD_FILE_PROPERTY_NAME, "");
// setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME,
// BrokerConstants.DEFAULT_PERSISTENT_PATH);
setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, Boolean.TRUE.toString());
setProperty(BrokerConstants.AUTHENTICATOR_CLASS_NAME, "");
setProperty(BrokerConstants.AUTHORIZATOR_CLASS_NAME, "");
setProperty(BrokerConstants.NETTY_MAX_BYTES_PROPERTY_NAME,
String.valueOf(BrokerConstants.DEFAULT_NETTY_MAX_BYTES_IN_MESSAGE));
}
public abstract IResourceLoader getResourceLoader();
public int intProp(String propertyName, int defaultValue) {
String propertyValue = getProperty(propertyName);
if (propertyValue == null) {
return defaultValue;
}
return Integer.parseInt(propertyValue);
}
public boolean boolProp(String propertyName, boolean defaultValue) {
String propertyValue = getProperty(propertyName);
if (propertyValue == null) {
return defaultValue;
}
return Boolean.parseBoolean(propertyValue);
}
}
@@ -1,38 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import java.io.Reader;
public interface IResourceLoader {
Reader loadDefaultResource();
Reader loadResource(String relativePath);
String getName();
class ResourceIsDirectoryException extends RuntimeException {
private static final long serialVersionUID = -6969292229582764176L;
public ResourceIsDirectoryException(String message) {
super(message);
}
}
}
@@ -1,69 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import java.util.Map;
import java.util.Properties;
/**
* Configuration backed by memory.
*/
public class MemoryConfig extends IConfig {
private final Properties m_properties = new Properties();
public MemoryConfig(Properties properties) {
assignDefaults();
for (Map.Entry<Object, Object> entrySet : properties.entrySet()) {
m_properties.put(entrySet.getKey(), entrySet.getValue());
}
}
// private void createDefaults() {
// m_properties.put(BrokerConstants.PORT_PROPERTY_NAME, Integer.toString(BrokerConstants.PORT));
// m_properties.put(BrokerConstants.HOST_PROPERTY_NAME, BrokerConstants.HOST);
// m_properties.put(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME,
// Integer.toString(BrokerConstants.WEBSOCKET_PORT));
// m_properties.put(BrokerConstants.PASSWORD_FILE_PROPERTY_NAME, "");
// m_properties.put(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME,
// BrokerConstants.DEFAULT_PERSISTENT_PATH);
// m_properties.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, true);
// m_properties.put(BrokerConstants.AUTHENTICATOR_CLASS_NAME, "");
// m_properties.put(BrokerConstants.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);
}
@Override
public IResourceLoader getResourceLoader() {
return new FileResourceLoader();
}
}
@@ -1,103 +0,0 @@
/*
* Copyright (c) 2012-2018 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 io.moquette.server.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Reader;
import java.text.ParseException;
import java.util.Properties;
/**
* Configuration that loads config stream from a {@link IResourceLoader} instance.
*/
public class ResourceLoaderConfig extends IConfig {
private static final Logger LOG = LoggerFactory.getLogger(ResourceLoaderConfig.class);
private final Properties m_properties;
private final IResourceLoader resourceLoader;
public ResourceLoaderConfig(IResourceLoader resourceLoader) {
this(resourceLoader, null);
}
public ResourceLoaderConfig(IResourceLoader resourceLoader, String configName) {
LOG.info("Loading configuration. ResourceLoader = {}, configName = {}.", resourceLoader.getName(), configName);
this.resourceLoader = resourceLoader;
/*
* If we use a conditional operator, the loadResource() and the loadDefaultResource()
* methods will be always called. This makes the log traces confusing.
*/
Reader configReader;
if (configName != null) {
configReader = resourceLoader.loadResource(configName);
} else {
configReader = resourceLoader.loadDefaultResource();
}
if (configReader == null) {
LOG.error(
"The resource loader returned no configuration reader. ResourceLoader = {}, configName = {}.",
resourceLoader.getName(),
configName);
throw new IllegalArgumentException("Can't locate " + resourceLoader.getName() + " \"" + configName + "\"");
}
LOG.info(
"Parsing configuration properties. ResourceLoader = {}, configName = {}.",
resourceLoader.getName(),
configName);
ConfigurationParser confParser = new ConfigurationParser();
m_properties = confParser.getProperties();
assignDefaults();
try {
confParser.parse(configReader);
} catch (ParseException pex) {
LOG.warn(
"Unable to parse configuration properties. Using default configuration. "
+ "ResourceLoader = {}, configName = {}, cause = {}, errorMessage = {}.",
resourceLoader.getName(),
configName,
pex.getCause(),
pex.getMessage());
}
}
@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);
}
@Override
public IResourceLoader getResourceLoader() {
return resourceLoader;
}
}