2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-01-04 14:37:29 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authorsgetRockQuestions()
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server.config;
|
2014-02-22 11:14:56 +01:00
|
|
|
|
2015-03-15 16:45:11 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2016-12-26 15:59:19 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.Reader;
|
2014-02-22 11:14:56 +01:00
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.util.Properties;
|
2015-03-11 10:44:00 -04:00
|
|
|
|
2014-02-22 11:14:56 +01:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @author andrea
|
|
|
|
|
*/
|
|
|
|
|
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 {
|
|
|
|
|
FileReader reader = new FileReader(file);
|
|
|
|
|
parse(reader);
|
|
|
|
|
} catch (FileNotFoundException fex) {
|
|
|
|
|
LOG.warn(String.format("parsing not existing file %s, so fallback on default configuration!", file.getAbsolutePath()), fex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2015-08-22 15:35:11 +02:00
|
|
|
int delimiterIdx = line.indexOf(' ');
|
|
|
|
|
String key = line.substring(0, delimiterIdx).trim();
|
|
|
|
|
String value = line.substring(delimiterIdx).trim();
|
2014-02-22 11:14:56 +01:00
|
|
|
|
|
|
|
|
m_properties.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
throw new ParseException("Failed to read", 1);
|
|
|
|
|
}
|
2016-12-26 15:59:19 +02:00
|
|
|
finally {
|
|
|
|
|
try {
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-22 11:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Properties getProperties() {
|
|
|
|
|
return m_properties;
|
|
|
|
|
}
|
|
|
|
|
}
|