mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(mongo): Finished source for MongoDB #105
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
+17
@@ -98,6 +98,11 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
|
||||
return Collections.unmodifiableMap(deep ? mappingValues(this, null, true, String.valueOf(pathSeparator())) : data());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull @UnmodifiableView Set<String> getKeys(boolean deep) {
|
||||
return Collections.unmodifiableSet(deep ? mappingKeys(this, null, true, String.valueOf(pathSeparator())) : data().keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
if (value instanceof Map) value = createSection(path, (Map<?, ?>) value);
|
||||
@@ -157,5 +162,17 @@ public abstract class AbstractMapSection<R extends AbstractMapSection<R>> implem
|
||||
return output;
|
||||
}
|
||||
|
||||
protected static Set<String> mappingKeys(@NotNull AbstractMapSection<?> section, @Nullable String parent, boolean deep, String pathSeparator) {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
for (Map.Entry<String, Object> entry : section.data().entrySet()) {
|
||||
String path = (parent == null ? "" : parent + pathSeparator) + entry.getKey();
|
||||
keys.add(path);
|
||||
if (deep && entry.getValue() instanceof AbstractMapSection<?>) {
|
||||
keys.addAll(mappingKeys((AbstractMapSection<?>) entry.getValue(), path, true, pathSeparator));
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<modules>
|
||||
<module>core</module>
|
||||
<module>features/section</module>
|
||||
@@ -28,7 +28,7 @@
|
||||
<module>providers/gson</module>
|
||||
<!-- <module>providers/hocon</module>-->
|
||||
<!-- <module>providers/sql</module>-->
|
||||
<!-- <module>providers/mongodb</module>-->
|
||||
<module>providers/mongodb</module>
|
||||
|
||||
<module>demo</module>
|
||||
</modules>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
+18
-10
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.0.0</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -15,6 +15,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
|
||||
<deps.mongodb.version>5.3.1</deps.mongodb.version>
|
||||
<log4j.version>2.24.3</log4j.version>
|
||||
</properties>
|
||||
<artifactId>easyconfiguration-mongodb</artifactId>
|
||||
@@ -30,7 +31,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-gson</artifactId>
|
||||
<artifactId>easyconfiguration-feature-section</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
@@ -38,14 +39,7 @@
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>5.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
<version>${deps.mongodb.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -69,6 +63,20 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-demo</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-gson</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package cc.carm.lib.configuration.source.mongodb;
|
||||
|
||||
import cc.carm.lib.configuration.source.ConfigurationFactory;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class MongoConfigFactory extends ConfigurationFactory<MongoSource, ConfigurationHolder<MongoSource>, MongoConfigFactory> {
|
||||
|
||||
public static MongoConfigFactory from(@NotNull Supplier<MongoCollection<Document>> collectionSupplier) {
|
||||
return new MongoConfigFactory(collectionSupplier);
|
||||
}
|
||||
|
||||
public static MongoConfigFactory from(@NotNull MongoCollection<Document> collection) {
|
||||
return from(() -> collection);
|
||||
}
|
||||
|
||||
public static MongoConfigFactory from(@NotNull MongoDatabase database, @NotNull String collectionName) {
|
||||
return from(() -> database.getCollection(collectionName));
|
||||
}
|
||||
|
||||
protected @NotNull Supplier<MongoCollection<Document>> collectionSupplier;
|
||||
protected @NotNull String namespace = "config";
|
||||
|
||||
public MongoConfigFactory(@NotNull Supplier<MongoCollection<Document>> collectionSupplier) {
|
||||
super();
|
||||
this.collectionSupplier = collectionSupplier;
|
||||
}
|
||||
|
||||
public MongoConfigFactory collection(@NotNull Supplier<MongoCollection<Document>> collectionSupplier) {
|
||||
this.collectionSupplier = collectionSupplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MongoConfigFactory collection(@NotNull MongoCollection<Document> collection) {
|
||||
return collection(() -> collection);
|
||||
}
|
||||
|
||||
public MongoConfigFactory namespace(@NotNull String namespace) {
|
||||
this.namespace = namespace;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MongoConfigFactory namespace(@NotNull Supplier<String> namespace) {
|
||||
return namespace(namespace.get());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected MongoConfigFactory self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigurationHolder<MongoSource> build() {
|
||||
MongoCollection<Document> collection = this.collectionSupplier.get();
|
||||
if (collection == null) {
|
||||
throw new IllegalStateException("Failed to get MongoCollection<Document> from supplier");
|
||||
}
|
||||
|
||||
return new ConfigurationHolder<MongoSource>(this.adapters, this.options, this.metadata, this.initializer) {
|
||||
final @NotNull MongoSource source = new MongoSource(this, System.currentTimeMillis(), collection, namespace);
|
||||
|
||||
@Override
|
||||
public @NotNull MongoSource config() {
|
||||
return this.source;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+73
-1
@@ -1,4 +1,76 @@
|
||||
package cc.carm.lib.configuration.source.mongodb;
|
||||
|
||||
public class MongoSource {
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSource;
|
||||
import cc.carm.lib.configuration.source.section.SourcedSection;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.model.ReplaceOptions;
|
||||
import org.bson.Document;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MongoSource extends ConfigureSource<SourcedSection, Map<String, Object>, MongoSource> {
|
||||
|
||||
protected final @NotNull MongoCollection<Document> collection;
|
||||
protected final @NotNull String namespace;
|
||||
|
||||
protected SourcedSection rootSection;
|
||||
|
||||
protected MongoSource(@NotNull ConfigurationHolder<? extends MongoSource> holder, long lastUpdateMillis,
|
||||
@NotNull MongoCollection<Document> collection, @NotNull String namespace) {
|
||||
super(holder, lastUpdateMillis);
|
||||
this.collection = collection;
|
||||
this.namespace = namespace;
|
||||
try {
|
||||
onReload();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull MongoSource self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> original() {
|
||||
return section().data();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull SourcedSection section() {
|
||||
return Objects.requireNonNull(rootSection, "RootSection is not initialized");
|
||||
}
|
||||
|
||||
public @NotNull String namespace() {
|
||||
return this.namespace;
|
||||
}
|
||||
|
||||
public @NotNull MongoCollection<Document> collection() {
|
||||
return this.collection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
Map<String, Object> data = this.rootSection.rawMap();
|
||||
if (data.isEmpty()) return; // Skip saving if empty
|
||||
if (data.containsKey("_id") && data.size() == 1) return; // Skip saving if only contains _id
|
||||
|
||||
ReplaceOptions options = new ReplaceOptions().upsert(true);
|
||||
Document storage = new Document(data).append("_id", this.namespace);
|
||||
this.collection.replaceOne(new Document("_id", this.namespace), storage, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
Document storage = this.collection.find(new Document("_id", this.namespace)).first();
|
||||
if (storage == null) storage = new Document();
|
||||
else storage.remove("_id"); // Remove _id
|
||||
this.rootSection = SourcedSection.root(this, storage);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.Configuration;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
|
||||
interface MongoConfig extends Configuration {
|
||||
ConfiguredValue<String> HOST = ConfiguredValue.of("127.0.0.1");
|
||||
ConfiguredValue<Integer> PORT = ConfiguredValue.of(27017);
|
||||
ConfiguredValue<String> USERNAME = ConfiguredValue.of("minecraft");
|
||||
ConfiguredValue<String> PASSWORD = ConfiguredValue.of("minecraft");
|
||||
ConfiguredValue<String> DATABASE = ConfiguredValue.of("minecraft");
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package config;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.json.JSONConfigFactory;
|
||||
import cc.carm.lib.configuration.source.mongodb.MongoConfigFactory;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.UuidRepresentation;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class MongoTest {
|
||||
|
||||
boolean local = false;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (!local) return;
|
||||
|
||||
ConfigurationHolder<?> gsonHolder = JSONConfigFactory.from(new File("target/mongo.json")).build();
|
||||
gsonHolder.initialize(MongoConfig.class);
|
||||
|
||||
MongoClientSettings settings = MongoClientSettings.builder()
|
||||
.applyConnectionString(new ConnectionString(
|
||||
"mongodb://" + MongoConfig.HOST.resolve() + ":" + MongoConfig.PORT.resolve()
|
||||
))
|
||||
.credential(MongoCredential.createCredential(
|
||||
MongoConfig.USERNAME.resolve(), MongoConfig.DATABASE.resolve(),
|
||||
MongoConfig.PASSWORD.resolve().toCharArray()
|
||||
))
|
||||
.uuidRepresentation(UuidRepresentation.STANDARD)
|
||||
.build();
|
||||
MongoClient mongoClient = MongoClients.create(settings);
|
||||
MongoDatabase mongoDatabase = mongoClient.getDatabase(MongoConfig.DATABASE.resolve());
|
||||
|
||||
ConfigurationHolder<?> mongoHolder = MongoConfigFactory
|
||||
.from(mongoDatabase, "configs")
|
||||
.namespace("my_plugin")
|
||||
.build();
|
||||
|
||||
// Test the configuration
|
||||
ConfigurationTest.testDemo(mongoHolder);
|
||||
ConfigurationTest.testInner(mongoHolder);
|
||||
|
||||
ConfigurationTest.save(mongoHolder);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<artifactId>easyconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>4.0.7</version>
|
||||
<version>4.0.8</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
|
||||
Reference in New Issue
Block a user