Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions SimpleAPI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@
<version>5.24.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,22 @@ public final class YamlConfigDocument implements ConfigDocument {
public static final int DEFAULT_MAX_BYTES = 1024 * 1024;
private static final int MAX_NODES = 100_000;
private static final int MAX_DEPTH = 64;
// Serialize publication per canonical parent, including case-alias target names.
private static final Object[] WRITE_LOCKS = new Object[64];
static {
for (int index = 0; index < WRITE_LOCKS.length; index++) WRITE_LOCKS[index] = new Object();
}
private final Path path;
private final int maxBytes;
private final Object writeLock;
private ConfigurationNode root;
private String revision;
private boolean editing;

private YamlConfigDocument(Path path, int maxBytes) {
this.path = path;
this.maxBytes = maxBytes;
this.writeLock = WRITE_LOCKS[Math.floorMod(path.getParent().hashCode(), WRITE_LOCKS.length)];
}

public static YamlConfigDocument open(Path path) throws IOException { return open(path, DEFAULT_MAX_BYTES); }
Expand All @@ -86,6 +93,19 @@ public static YamlConfigDocument open(Path path, int maxBytes) throws IOExceptio

@Override public Path path() { return path; }

/** Seeds a missing document using the same parser, limits and atomic replacement as updates. */
synchronized ConfigSnapshot initializeFrom(byte[] bytes) throws IOException {
if (editing) throw new IllegalStateException("Cannot initialize inside an edit callback");
if (!revision.equals("missing")) return snapshot();
if (bytes.length > maxBytes) throw new IOException("Default configuration exceeds byte limit");
ConfigurationNode candidate = parse(bytes);
ConfigSnapshot result = new ConfigSnapshot(revisionOf(bytes), new ConfigurateConfigView(candidate.copy()));
replace(bytes, true);
root = candidate;
revision = result.revision();
return result;
}

@Override public synchronized ConfigSnapshot snapshot() {
return new ConfigSnapshot(revision, new ConfigurateConfigView(root.copy()));
}
Expand Down Expand Up @@ -170,6 +190,16 @@ private byte[] serialize(ConfigurationNode candidate) throws IOException {
}

private void replace(byte[] encoded) throws IOException {
replace(encoded, false);
}

private void replace(byte[] encoded, boolean initializeOnly) throws IOException {
synchronized (writeLock) {
replaceLocked(encoded, initializeOnly);
}
}

private void replaceLocked(byte[] encoded, boolean initializeOnly) throws IOException {
requireUnchanged();
Set<PosixFilePermission> permissions = null;
if (Files.getFileAttributeView(path.getParent(), java.nio.file.attribute.PosixFileAttributeView.class) != null) {
Expand All @@ -178,6 +208,7 @@ private void replace(byte[] encoded) throws IOException {
: Files.getPosixFilePermissions(path, LinkOption.NOFOLLOW_LINKS);
}
Path temporary = Files.createTempFile(path.getParent(), ".simpleapi-config-", ".tmp");
boolean committed = false;
try {
if (permissions != null) Files.setPosixFilePermissions(temporary, permissions);
try (FileChannel channel = FileChannel.open(temporary, StandardOpenOption.WRITE, LinkOption.NOFOLLOW_LINKS)) {
Expand All @@ -186,9 +217,24 @@ private void replace(byte[] encoded) throws IOException {
channel.force(true);
}
requireUnchanged();
Files.move(temporary, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
if (initializeOnly) {
// ATOMIC_MOVE is allowed to replace an existing target even without REPLACE_EXISTING.
// A hard link publishes the complete staged inode only if the destination is absent.
try { Files.createLink(path, temporary); }
catch (UnsupportedOperationException unsupported) {
throw new IOException("Atomic missing-file initialization is not supported", unsupported);
}
} else {
Files.move(temporary, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
}
committed = true;
} finally {
Files.deleteIfExists(temporary);
try { Files.deleteIfExists(temporary); }
catch (IOException | SecurityException cleanupFailure) {
// Publication already succeeded: do not report a failed write or retain old active data.
// An unremovable staged sibling may require application-owned directory cleanup.
if (!committed) throw cleanupFailure;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.bencodez.simpleapi.core.config;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import com.bencodez.simpleapi.file.config.ConfigEditor;
import com.bencodez.simpleapi.file.config.ConfigSnapshot;

/**
* Platform-neutral YAML file facade with typed reads and atomic, revision-checked
* persistence. All file operations are synchronous; the caller owns its I/O thread
* and trusted parent directory. Existing files are never automatically rewritten.
*
* <p>Resource defaults initialize missing files only, not a read-time overlay.
* Getter defaults, strict scalar reads, legacy list conversions and literal keys
* follow the existing configuration views. Sections and snapshots are detached;
* obtain them again after a successful reload/update. Writes are immediately
* persisted, not staged in a mutable configuration tree. Formatting/inline comment
* preservation across edits is not promised.</p>
*/
public final class YamlConfiguration implements StructuredConfigView {
private final YamlConfigDocument document;
private volatile ConfigSnapshot current;
private volatile boolean ignoreCase;

private YamlConfiguration(YamlConfigDocument document) {
this.document = document;
publish(document.snapshot());
}

/** Reads an existing file, or an empty missing document without creating it. */
public static YamlConfiguration load(Path path) throws IOException {
return load(path, YamlConfigDocument.DEFAULT_MAX_BYTES);
}

public static YamlConfiguration load(Path path, int maxBytes) throws IOException {
return new YamlConfiguration(YamlConfigDocument.open(path, maxBytes));
}

/**
* Initializes a missing file from a classpath resource. An existing file is
* loaded without opening the resource. Resource names follow Class.getResourceAsStream.
*/
public static YamlConfiguration load(Path path, Class<?> resourceOwner, String resourceName) throws IOException {
Objects.requireNonNull(resourceOwner, "resourceOwner");
Objects.requireNonNull(resourceName, "resourceName");
YamlConfiguration configuration = load(path);
if (configuration.current.revision().equals("missing")) {
byte[] bytes;
try (InputStream defaults = resourceOwner.getResourceAsStream(resourceName)) {
if (defaults == null) throw new IOException("Default YAML resource is missing");
bytes = defaults.readNBytes(YamlConfigDocument.DEFAULT_MAX_BYTES + 1);
}
configuration.publish(configuration.document.initializeFrom(bytes));
}
return configuration;
}

/** The supplied stream is consumed only for a missing file and always closed, including on failure. */
public static YamlConfiguration load(Path path, InputStream defaults) throws IOException {
return load(path, defaults, YamlConfigDocument.DEFAULT_MAX_BYTES);
}

public static YamlConfiguration load(Path path, InputStream defaults, int maxBytes) throws IOException {
Objects.requireNonNull(defaults, "defaults");
YamlConfiguration configuration;
byte[] bytes = null;
try (defaults) {
configuration = load(path, maxBytes);
if (configuration.current.revision().equals("missing")) bytes = defaults.readNBytes(maxBytes + 1);
}
if (bytes != null) configuration.publish(configuration.document.initializeFrom(bytes));
return configuration;
}

public Path path() { return document.path(); }
public ConfigSnapshot snapshot() { return current; }
public StructuredConfigView getData() { return (StructuredConfigView) current.view(); }
public boolean isIgnoreCase() { return ignoreCase; }

/** Opt-in read policy only. Edits always use exact keys; use setAt for literal segments. */
public synchronized void setIgnoreCase(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
publish(document.snapshot());
}

/** Failed reloads leave both the active snapshot and file untouched. */
public synchronized ConfigSnapshot reload() throws IOException { return publish(document.reload()); }

/** Atomically persists an edit using the active revision. Failed edits publish nothing. */
public synchronized ConfigSnapshot update(Consumer<ConfigEditor> edit) throws IOException {
return update(current.revision(), edit);
}

public synchronized ConfigSnapshot update(String expectedRevision, Consumer<ConfigEditor> edit) throws IOException {
return publish(document.update(expectedRevision, edit));
}

/** Immediately persists a value; null removes it. This is not an in-memory-only setter. */
public ConfigSnapshot set(String path, Object value) throws IOException {
return update(edit -> edit.set(path, value));
}

public ConfigSnapshot setAt(Object value, String... keys) throws IOException {
return update(edit -> edit.setAt(value, keys));
}

/** Explicitly saves the current document, including creation of an empty missing file. */
public ConfigSnapshot save() throws IOException { return update(edit -> { }); }

private ConfigSnapshot publish(ConfigSnapshot snapshot) {
StructuredConfigView view = new ConfigurateStructuredConfigView((ConfigurateConfigView) snapshot.view());
if (ignoreCase) view = new CaseInsensitiveConfigView(view);
current = new ConfigSnapshot(snapshot.revision(), view);
return current;
}

public String getString(String path) { return getString(path, null); }
public boolean getBoolean(String path) { return getBoolean(path, false); }
public int getInt(String path) { return getInt(path, 0); }
public long getLong(String path) { return getLong(path, 0L); }
public double getDouble(String path) { return getDouble(path, 0D); }

@Override public boolean contains(String path) { return getData().contains(path); }
@Override public String getString(String path, String fallback) { return getData().getString(path, fallback); }
@Override public boolean getBoolean(String path, boolean fallback) { return getData().getBoolean(path, fallback); }
@Override public int getInt(String path, int fallback) { return getData().getInt(path, fallback); }
@Override public long getLong(String path, long fallback) { return getData().getLong(path, fallback); }
@Override public double getDouble(String path, double fallback) { return getData().getDouble(path, fallback); }
@Override public List<String> getStringList(String path) { return getData().getStringList(path); }
@Override public List<Integer> getIntegerList(String path) { return getData().getIntegerList(path); }
@Override public boolean isConfigurationSection(String path) { return getData().isConfigurationSection(path); }
@Override public StructuredConfigView getConfigurationSection(String path) { return getData().getConfigurationSection(path); }
@Override public Set<String> getKeys(boolean deep) { return getData().getKeys(deep); }
@Override public Kind kind(String path) { return getData().kind(path); }
@Override public Kind kindAt(String... keys) { return getData().kindAt(keys); }
@Override public Object value(String path) { return getData().value(path); }
@Override public Object valueAt(String... keys) { return getData().valueAt(keys); }
@Override public StructuredConfigView at(String... keys) { return getData().at(keys); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.bencodez.simpleapi.tests.shared;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

import com.bencodez.simpleapi.core.config.StructuredConfigView.Kind;
import com.bencodez.simpleapi.core.config.YamlConfiguration;

/** A native consumer uses only the public facade: no Configurate nodes or casts. */
public final class NativeYamlApiFixture {
private NativeYamlApiFixture() { }

public static void run() throws Exception {
var directory = Files.createTempDirectory("simpleapi-yaml-api-");
var path = directory.resolve("settings.yml");
try {
var missing = YamlConfiguration.load(path);
require(!Files.exists(path) && missing.getInt("PointsOnVote", 3) == 3, "Missing read changed disk");
var config = YamlConfiguration.load(path, NativeYamlApiFixture.class, "/native-yaml-defaults.yml");
byte[] resource;
try (var defaults = NativeYamlApiFixture.class.getResourceAsStream("/native-yaml-defaults.yml")) {
resource = defaults.readAllBytes();
}
require(java.util.Arrays.equals(resource, Files.readAllBytes(path)), "Initial resource bytes changed");
require(config.getInt("PointsOnVote") == 1 && config.getBoolean("ProcessRewards"), "Typed options failed");
require(config.getLong("PointsOnVote") == 1L && config.getDouble("PointsOnVote") == 1D, "Numeric reads failed");
require(config.getInt("Quoted", 99) == 99, "Quoted scalar was coerced");
require(config.kind("Empty") == Kind.LIST && config.kind("Absent") == Kind.MISSING, "Empty/missing lost");
require(config.getString("Absent") == null && !config.getBoolean("Absent"), "Convenience defaults changed");
var sites = config.getConfigurationSection("VoteSites");
require(sites.at("some.site").getStringList("Rewards.Commands").equals(List.of("say voted")),
"Literal site/list traversal failed");
var before = config.snapshot();
require(config.getInt("pointsonvote", 99) == 99, "Default reads are not case-sensitive");
config.setIgnoreCase(true);
require(config.getInt("POINTSONVOTE") == 1, "Opt-in case policy failed");
require(config.at("votesites", "SOME.SITE").getBoolean("enabled", false), "Case/literal traversal failed");
config.setIgnoreCase(false);
config.update(edit -> {
edit.set("PointsOnVote", 4);
edit.set("Unknown", "preserve");
edit.set("Milestones.10.Points", 3);
edit.setAt(List.of("say updated"), "VoteSites", "some.site", "Rewards", "Commands");
});
require(before.view().getInt("PointsOnVote", 0) == 1, "Snapshot was not detached");
require(sites.at("some.site").getStringList("Rewards.Commands").equals(List.of("say voted")),
"Retained section changed");
config.setAt(Map.of("Enabled", false), "another.site");
config.save();
var reopened = YamlConfiguration.load(path, NativeYamlApiFixture.class, "/missing-resource.yml");
require(reopened.getInt("PointsOnVote") == 4 && "preserve".equals(reopened.getString("Unknown")),
"Existing file overwritten by defaults");
require(reopened.getConfigurationSection("Milestones").getKeys(false).equals(java.util.Set.of("10")),
"Numeric key duplicated");
require(!reopened.at("another.site").getBoolean("Enabled", true), "Literal setter failed");
String bytes = Files.readString(path);
try {
config.update(before.revision(), edit -> edit.set("PointsOnVote", 8));
throw new AssertionError("Stale snapshot accepted");
} catch (IOException expected) { }
require(bytes.equals(Files.readString(path)), "Stale write changed file");
var lastGood = config.snapshot();
Files.writeString(path, "PointsOnVote: [unterminated");
try { config.reload(); throw new AssertionError("Malformed reload accepted"); }
catch (IOException expected) { }
require(config.snapshot() == lastGood && config.getInt("PointsOnVote") == 4, "Failed reload changed active data");
try { config.save(); throw new AssertionError("Save overwrote external malformed edit"); }
catch (IOException expected) { }
Files.writeString(path, "PointsOnVote: 9\n");
require(config.reload().view().getInt("PointsOnVote", 0) == 9, "Reload did not recover");
} finally {
Files.deleteIfExists(path);
Files.deleteIfExists(directory);
}
}

private static void require(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
}
Loading
Loading