kopia lustrzana https://github.com/bertrik/ttnhabbridge
Use BaseConfig.java as base class for config.
rodzic
561c86e0a0
commit
e8848616d0
|
@ -0,0 +1,77 @@
|
|||
package nl.sikken.bertrik;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Base configuration class.
|
||||
*/
|
||||
public abstract class BaseConfig {
|
||||
|
||||
private final Map<String, String> values = new LinkedHashMap<>();
|
||||
private final Map<String, String> comments = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Adds a property.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the default value
|
||||
* @param comment the comment
|
||||
*/
|
||||
protected void add(String key, String value, String comment) {
|
||||
values.put(key, value);
|
||||
comments.put(key, comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated with the key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return the value
|
||||
*/
|
||||
protected String get(String key) {
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load settings from stream.
|
||||
*
|
||||
* @param is input stream containing the settings
|
||||
* @throws IOException in case of an IO problem
|
||||
*/
|
||||
public final void load(InputStream is) throws IOException {
|
||||
final Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
for (String e : values.keySet()) {
|
||||
String value = properties.getProperty(e);
|
||||
if (value != null) {
|
||||
values.put(e, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save settings to stream.
|
||||
*
|
||||
* @param os the output stream
|
||||
* @throws IOException in case of an IO problem
|
||||
*/
|
||||
public final void save(OutputStream os) throws IOException {
|
||||
try (Writer writer = new OutputStreamWriter(os, StandardCharsets.US_ASCII)) {
|
||||
for (String e : values.keySet()) {
|
||||
// comment line
|
||||
writer.append("# " + comments.get(e) + "\n");
|
||||
writer.append(e + "=" + get(e) + "\n");
|
||||
writer.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,9 @@
|
|||
package nl.sikken.bertrik;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Configuration class.
|
||||
*/
|
||||
public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
|
||||
final class TtnHabBridgeConfig extends BaseConfig implements ITtnHabBridgeConfig {
|
||||
|
||||
/**
|
||||
* One enumeration item per configuration item.
|
||||
|
@ -31,18 +21,16 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
|
|||
;
|
||||
|
||||
private final String key;
|
||||
private final String def;
|
||||
private final String value;
|
||||
private final String comment;
|
||||
|
||||
EConfigItem(String key, String def, String comment) {
|
||||
this.key = key;
|
||||
this.def = def;
|
||||
this.value = def;
|
||||
this.comment = comment;
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<EConfigItem, String> props = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
@ -50,77 +38,43 @@ public final class TtnHabBridgeConfig implements ITtnHabBridgeConfig {
|
|||
*/
|
||||
public TtnHabBridgeConfig() {
|
||||
for (EConfigItem e : EConfigItem.values()) {
|
||||
props.put(e, e.def);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load settings from stream.
|
||||
*
|
||||
* @param is input stream containing the settings
|
||||
* @throws IOException in case of a problem reading the file
|
||||
*/
|
||||
public void load(InputStream is) throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
for (EConfigItem e : EConfigItem.values()) {
|
||||
String value = properties.getProperty(e.key);
|
||||
if (value != null) {
|
||||
props.put(e, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save settings to stream.
|
||||
*
|
||||
* @param os the output stream
|
||||
* @throws IOException in case of a file problem
|
||||
*/
|
||||
public void save(OutputStream os) throws IOException {
|
||||
try (Writer writer = new OutputStreamWriter(os, StandardCharsets.US_ASCII)) {
|
||||
for (EConfigItem e : EConfigItem.values()) {
|
||||
// comment line
|
||||
writer.append("# " + e.comment + "\n");
|
||||
writer.append(e.key + "=" + e.def + "\n");
|
||||
writer.append("\n");
|
||||
}
|
||||
add(e.key, e.value, e.comment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHabitatTimeout() {
|
||||
return Integer.parseInt(props.get(EConfigItem.HABITAT_TIMEOUT));
|
||||
return Integer.parseInt(get(EConfigItem.HABITAT_TIMEOUT.key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHabitatUrl() {
|
||||
return props.get(EConfigItem.HABITAT_URL);
|
||||
return get(EConfigItem.HABITAT_URL.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTtnMqttUrl() {
|
||||
return props.get(EConfigItem.TTN_MQTT_URL);
|
||||
return get(EConfigItem.TTN_MQTT_URL.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTtnAppId() {
|
||||
return props.get(EConfigItem.TTN_APP_ID);
|
||||
return get(EConfigItem.TTN_APP_ID.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTtnAppKey() {
|
||||
return props.get(EConfigItem.TTN_APP_KEY);
|
||||
return get(EConfigItem.TTN_APP_KEY.key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTtnGwCacheExpiry() {
|
||||
return Integer.parseInt(props.get(EConfigItem.TTN_GW_CACHE_EXPIRY));
|
||||
return Integer.parseInt(get(EConfigItem.TTN_GW_CACHE_EXPIRY.key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTtnPayloadEncoding() {
|
||||
return props.get(EConfigItem.TTN_PAYLOAD_ENCODING);
|
||||
return get(EConfigItem.TTN_PAYLOAD_ENCODING.key);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue