Skip to content

Commit

Permalink
Merge in new YAML parser
Browse files Browse the repository at this point in the history
- YAML parser has much better support for lists
- YamlElement is now abstract and has separate inheritors for unordered maps, ordered lists, key-value pairs, and values by themselves.
- Calls in tests have been changed to use the new YAML parser.
- Application configuration now uses the new YAML parser.
  • Loading branch information
lavajuno authored Dec 27, 2023
2 parents 9209c27 + 5154e6a commit 53f5d44
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 312 deletions.
7 changes: 4 additions & 3 deletions config/mirrorlog.conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# Configuration file for MirrorLog

# Please note that the YAML parser is very small and has some limitations:
# 1- Lists must be in multiple-line form, and can only contain values.
# 1- Lists must be in multiple-line form with proper indentation.
# This will work:
# MyList:
# - MyValue
# - MyValue
# This will NOT work:
# MyList: [MyValue]
# MyList:
# - MyElement: MyValue
# - MyValue
# 2- Values must be a single line, spanning & folding are not recognized.
# However, escape characters will be preserved, so you can make newlines this way.
# This will work:
Expand Down
2 changes: 1 addition & 1 deletion docs/jdoc/org/lavajuno/mirrorlog/yaml/YamlElement.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<h1 title="Class YamlElement" class="title">Class YamlElement</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.lavajuno.mirrorlog.yaml.YamlElement</div>
<div class="inheritance">org.lavajuno.mirrorlog.yaml.YamlElementOld</div>
</div>
<section class="class-description" id="class-description">
<dl class="notes">
Expand Down
2 changes: 1 addition & 1 deletion docs/jdoc/org/lavajuno/mirrorlog/yaml/YamlList.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<h1 title="Class YamlList" class="title">Class YamlList</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance"><a href="YamlElement.html" title="class in org.lavajuno.mirrorlog.yaml">org.lavajuno.mirrorlog.yaml.YamlElement</a>
<div class="inheritance"><a href="YamlElement.html" title="class in org.lavajuno.mirrorlog.yaml">org.lavajuno.mirrorlog.yaml.YamlElementOld</a>
<div class="inheritance">org.lavajuno.mirrorlog.yaml.YamlList</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions docs/jdoc/org/lavajuno/mirrorlog/yaml/YamlValue.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
<h1 title="Class YamlValue" class="title">Class YamlValue</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance"><a href="YamlElement.html" title="class in org.lavajuno.mirrorlog.yaml">org.lavajuno.mirrorlog.yaml.YamlElement</a>
<div class="inheritance">org.lavajuno.mirrorlog.yaml.YamlValue</div>
<div class="inheritance"><a href="YamlElement.html" title="class in org.lavajuno.mirrorlog.yaml">org.lavajuno.mirrorlog.yaml.YamlElementOld</a>
<div class="inheritance">org.lavajuno.mirrorlog.yaml.YamlValueOld</div>
</div>
</div>
<section class="class-description" id="class-description">
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
50 changes: 24 additions & 26 deletions src/main/java/org/lavajuno/mirrorlog/config/ApplicationConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.lavajuno.mirrorlog.config;

import org.lavajuno.mirrorlog.yaml.YamlElement;
import org.lavajuno.mirrorlog.yaml.YamlList;
import org.lavajuno.mirrorlog.yaml.YamlValue;
import org.lavajuno.mirrorlog.yaml.*;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -39,85 +37,85 @@ public class ApplicationConfig {
*/
public ApplicationConfig(String config_file_path) throws IOException {
/* Parse configuration file */
final YamlElement config_root = new YamlElement(readLinesFromFile(config_file_path));
final YamlMap config_root = new YamlMap(readLinesFromFile(config_file_path));

/* Get configuration revision */
final YamlValue config_revision = (YamlValue) config_root.getElement("revision");
final YamlValue config_revision = ((YamlPair) config_root.getElement("revision")).getValue();

/* Get server configuration */
final YamlElement config_server = config_root.getElement("server");
final YamlValue config_threads = (YamlValue) config_server.getElement("threads");
final YamlValue config_port = (YamlValue) config_server.getElement("port");
final YamlValue config_timeout = (YamlValue) config_server.getElement("timeout");
final YamlValue config_restricted = (YamlValue) config_server.getElement("restricted");
final YamlMap config_server = (YamlMap) config_root.getElement("server");
final YamlValue config_threads = ((YamlPair) config_server.getElement("threads")).getValue();
final YamlValue config_port = ((YamlPair) config_server.getElement("port")).getValue();
final YamlValue config_timeout = ((YamlPair) config_server.getElement("timeout")).getValue();
final YamlValue config_restricted = ((YamlPair) config_server.getElement("restricted")).getValue();
final YamlList config_addresses = (YamlList) config_server.getElement("allowed_addresses");

/* Get log file configuration */
final YamlElement config_output = config_root.getElement("output");
final YamlValue config_component_pad = (YamlValue) config_output.getElement("component_pad");
final YamlValue config_log_to_file = (YamlValue) config_output.getElement("log_to_file");
final YamlValue config_file_duration = (YamlValue) config_output.getElement("file_duration");
final YamlValue config_file_history = (YamlValue) config_output.getElement("file_history");
final YamlMap config_output = (YamlMap) config_root.getElement("output");
final YamlValue config_component_pad = ((YamlPair) config_output.getElement("component_pad")).getValue();
final YamlValue config_log_to_file = ((YamlPair) config_output.getElement("log_to_file")).getValue();
final YamlValue config_file_duration = ((YamlPair) config_output.getElement("file_duration")).getValue();
final YamlValue config_file_history = ((YamlPair) config_output.getElement("file_history")).getValue();

/* Read in and set configuration values */
try {
this.revision = Integer.parseInt(config_revision.getContents());
this.revision = config_revision.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"version\".");
throw new IOException("Illegal value for key \"version\".");
}

try {
this.threads = Integer.parseInt(config_threads.getContents());
this.threads = config_threads.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"threads\".");
throw new IOException("Illegal value for key \"threads\".");
}

try {
this.port = Integer.parseInt(config_port.getContents());
this.port = config_port.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"port\".");
throw new IOException("Illegal value for key \"port\".");
}

try {
this.timeout = Integer.parseInt(config_timeout.getContents());
this.timeout = config_timeout.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"timeout\".");
throw new IOException("Illegal value for key \"timeout\".");
}

this.restricted = Boolean.parseBoolean(config_restricted.getContents());
this.restricted = Boolean.parseBoolean(config_restricted.toString());

allowed_addresses = new Vector<>();
try {
for(String i : config_addresses.getContents()) {
allowed_addresses.add(InetAddress.getByName(i));
for(YamlElement i : config_addresses.getElements()) {
allowed_addresses.add(InetAddress.getByName( i.toString()) );
}
} catch(UnknownHostException e) {
System.err.println("Illegal value for key \"allowed_addresses\".");
throw new IOException("Illegal value for key \"allowed_addresses\".");
}

try {
this.component_pad = Integer.parseInt(config_component_pad.getContents());
this.component_pad = config_component_pad.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"component_pad\".");
throw new IOException("Illegal value for key \"component_pad\".");
}

this.log_to_file = Boolean.parseBoolean(config_log_to_file.getContents());
this.log_to_file = Boolean.parseBoolean(config_log_to_file.toString());

try {
this.file_duration = Integer.parseInt(config_file_duration.getContents());
this.file_duration = config_file_duration.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"file_duration\".");
throw new IOException("Illegal value for key \"file_duration\".");
}

try {
this.file_history = Integer.parseInt(config_file_history.getContents());
this.file_history = config_file_history.toInt();
} catch(NumberFormatException e) {
System.err.println("Illegal value for key \"file_history\".");
throw new IOException("Illegal value for key \"file_history\".");
Expand Down
Loading

0 comments on commit 53f5d44

Please sign in to comment.