From bec9ff0895db274ce15ed31266305665b75a5724 Mon Sep 17 00:00:00 2001 From: Mans Ziesel Date: Sun, 4 Feb 2024 17:04:35 +0100 Subject: [PATCH] Add PKL lib, add test config, create initial jmonit config template --- .idea/pklSettings.xml | 6 + .idea/workspace.xml | 121 +++++++++++++++++--- build.gradle.kts | 3 + src/main/java/nl/mziesel/JmonitConfig.java | 55 +++++++++ src/main/java/nl/mziesel/Main.java | 34 ++++-- src/main/resources/JmonitConfigTemplate.pkl | 13 +++ 6 files changed, 204 insertions(+), 28 deletions(-) create mode 100644 .idea/pklSettings.xml create mode 100644 src/main/java/nl/mziesel/JmonitConfig.java create mode 100644 src/main/resources/JmonitConfigTemplate.pkl diff --git a/.idea/pklSettings.xml b/.idea/pklSettings.xml new file mode 100644 index 0000000..7171c59 --- /dev/null +++ b/.idea/pklSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 485a71a..f78dd24 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,21 +5,11 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index c6b8eb4..84d8f67 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,6 @@ plugins { + idea + `java-library` id("java") } @@ -12,6 +14,7 @@ repositories { dependencies { testImplementation(platform("org.junit:junit-bom:5.9.1")) testImplementation("org.junit.jupiter:junit-jupiter") + implementation("org.pkl-lang:pkl-config-java:0.25.1") } tasks.test { diff --git a/src/main/java/nl/mziesel/JmonitConfig.java b/src/main/java/nl/mziesel/JmonitConfig.java new file mode 100644 index 0000000..505d974 --- /dev/null +++ b/src/main/java/nl/mziesel/JmonitConfig.java @@ -0,0 +1,55 @@ +package nl.mziesel; + +import org.pkl.config.java.Config; +import org.pkl.config.java.ConfigEvaluator; +import org.pkl.config.java.JavaType; +import org.pkl.core.ModuleSource; + +public class JmonitConfig { + public static void main(String[] args) { + // Configuration is represented as a tree of `Config` objects. + Config config; + + // To evaluate a Pkl module to a `Config` tree, + // create a `ConfigEvaluator` and call its `evaluate()` method. + // + // There are two ways to create a `ConfigEvaluator`: + // * `ConfigEvaluator.preconfigured()` + // * `ConfigEvaluatorBuilder` + // + // `ConfigEvaluator.preconfigured()` behaves much the same as the CLI. + // For example, it allows evaluating modules over HTTPS, which isn't always desirable. + // `ConfigEvaluatorBuilder` gives more control over a `ConfigEvaluator`'s behavior. + // + // It's important to close an evaluator after the last `evaluate()` call. + // Previously returned `Config` objects remain valid. + try (var evaluator = ConfigEvaluator.preconfigured()) { + config = evaluator.evaluate(ModuleSource.text(""" + parrot { + name = "Polly"; + age = 31; + favoriteFoods = new Listing { + apples"; + "crackers" + } + } + """ + )); + } + + // To descend the `Config` tree, use the `get()` method, + // which returns another `Config` object. + var parrot = config.get("parrot"); + + // To access a `Config` object's value, + // call `as()` and pass the Java type to convert the value to. + // The conversion is performed by `ConfigEvaluator.getValueMapper()`, + // which can be customized via `ConfigEvaluatorBuilder`. + var age = parrot.get("age").as(int.class); + System.out.println(age); + + // The `as()` method also accepts parameterized types such as `List`. + var favoriteFoods = parrot.get("favoriteFoods").as(JavaType.listOf(String.class)); + System.out.println(favoriteFoods); + } +} \ No newline at end of file diff --git a/src/main/java/nl/mziesel/Main.java b/src/main/java/nl/mziesel/Main.java index 29bcd8f..87bb35a 100644 --- a/src/main/java/nl/mziesel/Main.java +++ b/src/main/java/nl/mziesel/Main.java @@ -2,21 +2,33 @@ package nl.mziesel; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import org.pkl.config.java.Config; +import org.pkl.config.java.ConfigEvaluator; +import org.pkl.config.java.JavaType; +import org.pkl.core.ModuleSource; public class Main { public static void main(String[] args) throws InterruptedException { - HttpMonitor mzieselHttpMonit = new HttpMonitor("Website van Mans", "https://mziesel.nl/"); - HttpMonitor testHttpMonit = new HttpMonitor("Test website", "https://whoami.mzsl.nl/"); - HttpMonitor mcaHttpMonit = new HttpMonitor("Microcenter website", "https://microcenter.nl/"); + Config config; - ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); - - executorService.execute(mzieselHttpMonit); - executorService.execute(testHttpMonit); - executorService.execute(mcaHttpMonit); - - while (!executorService.isTerminated()) { - Thread.sleep(1000); + try (var evaluator = ConfigEvaluator.preconfigured()){ + config = evaluator.evaluate(ModuleSource.modulePath("jmonitconfig.pkl")); } + + System.out.println(config); + +// ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); + +// HttpMonitor mzieselHttpMonit = new HttpMonitor("Website van Mans", "https://mziesel.nl/"); +// HttpMonitor testHttpMonit = new HttpMonitor("Test website", "https://whoami.mzsl.nl/"); +// HttpMonitor mcaHttpMonit = new HttpMonitor("Microcenter website", "https://microcenter.nl/"); +// +// executorService.execute(mzieselHttpMonit); +// executorService.execute(testHttpMonit); +// executorService.execute(mcaHttpMonit); + +// while (!executorService.isTerminated()) { +// Thread.sleep(1000); +// } } } \ No newline at end of file diff --git a/src/main/resources/JmonitConfigTemplate.pkl b/src/main/resources/JmonitConfigTemplate.pkl new file mode 100644 index 0000000..602e764 --- /dev/null +++ b/src/main/resources/JmonitConfigTemplate.pkl @@ -0,0 +1,13 @@ +typealias MonitorType = "http"|"https"|"icmp" + +const defaultTimeout: Int = 5000 +const defaultInterval: Int = 60 + +class Monitor { + friendlyName: String + target: String + timeout: Int = defaultTimeout + interval: Int = defaultInterval +} + +monitors: Listing \ No newline at end of file