Skip to content

Commit 2009b3a

Browse files
author
burdo
committed
use configuration manager
1 parent 21a75c6 commit 2009b3a

4 files changed

Lines changed: 94 additions & 48 deletions

File tree

src/main/java/org/comroid/api/data/seri/adp/Jackson.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.comroid.annotations.Instance;
66
import org.comroid.api.data.seri.DataNode;
77
import org.comroid.api.data.seri.Serializer;
8+
import org.comroid.api.func.ext.Context;
89
import org.intellij.lang.annotations.Language;
910
import org.jetbrains.annotations.Nullable;
1011

@@ -17,12 +18,16 @@ public enum Jackson implements Serializer<JSON.Node> {
1718
@Override
1819
@SneakyThrows
1920
public @Nullable JSON.Node parse(@Language("JSON") @Nullable String data) {
21+
final var mapper = Context.root().getFromContext(ObjectMapper.class, true).assertion();
22+
2023
//noinspection unchecked
21-
return data == null ? DataNode.Value.NULL.json()
22-
: data.trim().startsWith("{") ? org.comroid.api.data.seri.adp.JSON.Object.of(new ObjectMapper().readValue(data, Map.class))
23-
: data.trim().startsWith("[") ? org.comroid.api.data.seri.adp.JSON.Array.of(
24-
new ObjectMapper().readValue(data, List.class))
25-
: DataNode.of(new ObjectMapper().readTree(data)).json();
24+
return data == null
25+
? DataNode.Value.NULL.json()
26+
: data.trim().startsWith("{")
27+
? org.comroid.api.data.seri.adp.JSON.Object.of(mapper.readValue(data,
28+
Map.class))
29+
: data.trim().startsWith("[") ? org.comroid.api.data.seri.adp.JSON.Array.of(mapper.readValue(data,
30+
List.class)) : DataNode.of(mapper.readTree(data)).json();
2631
}
2732

2833
@Override

src/main/java/org/comroid/api/func/ext/Context.java

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.comroid.api.data.seri.Serializer;
1010
import org.comroid.api.func.exc.ThrowingSupplier;
1111
import org.comroid.api.func.util.Debug;
12+
import org.comroid.api.func.util.RootContextSource;
1213
import org.comroid.api.java.ReflectionHelper;
1314
import org.jetbrains.annotations.ApiStatus.Experimental;
1415
import org.jetbrains.annotations.ApiStatus.Internal;
@@ -19,11 +20,14 @@
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.util.Arrays;
23+
import java.util.Collection;
24+
import java.util.Collections;
2225
import java.util.HashSet;
2326
import java.util.Map;
2427
import java.util.NoSuchElementException;
2528
import java.util.Objects;
2629
import java.util.Properties;
30+
import java.util.ServiceLoader;
2731
import java.util.Set;
2832
import java.util.function.Supplier;
2933
import java.util.logging.Level;
@@ -53,7 +57,7 @@
5357
@MustExtend(Context.Base.class)
5458
public interface Context extends Named, Convertible, LoggerCarrier {
5559
static Context root() {
56-
return Base.ROOT;
60+
return Base.ROOT.get();
5761
}
5862

5963
static <T> @Nullable T get(Class<T> type) {
@@ -120,7 +124,9 @@ static <T> Wrap<T> getFromContexts(final Class<T> member) {
120124

121125
@Deprecated(forRemoval = true)
122126
static <T> Wrap<T> getFromContexts(final Class<T> member, boolean includeChildren) {
123-
return () -> Base.ROOT.children.stream()
127+
return () -> Base.ROOT.get()
128+
.getChildren()
129+
.stream()
124130
.flatMap(sub -> sub.getFromContext(member, includeChildren).stream())
125131
.findFirst()
126132
.orElse(null);
@@ -142,6 +148,10 @@ static <T> T getFromRoot(Class<T> type, Supplier<? extends T> elseGet) {
142148
return wrap(type).orElseGet(elseGet);
143149
}
144150

151+
default Collection<Context> getChildren() {
152+
return Collections.emptySet();
153+
}
154+
145155
@Internal
146156
@NonExtendable
147157
default boolean isRoot() {
@@ -294,50 +304,60 @@ default Context plus(Object... plus) {
294304

295305
@Internal
296306
class Base implements Context {
297-
@SuppressWarnings("ConstantConditions") public static final Context.Base ROOT;
307+
@SuppressWarnings("ConstantConditions") public static final Supplier<Context> ROOT;
298308

299309
static {
300-
try {
301-
ROOT = new Context.Base(null, "ROOT", new Object[0]);
302-
InputStream resource = ClassLoader.getSystemClassLoader()
303-
.getResourceAsStream("org/comroid/api/context.properties");
304-
if (resource != null) {
305-
Properties props = new Properties();
306-
props.load(resource);
307-
308-
int c = 0;
309-
Object[] values = new Object[props.size()];
310-
for (Map.Entry<Object, Object> entry : props.entrySet()) {
311-
final int fc = c;
312-
Class<?> targetClass = Class.forName(String.valueOf(entry.getValue()));
313-
createInstance(targetClass).ifPresent(it -> values[fc] = it);
314-
c++;
315-
}
316-
Debug.logger.log(Level.FINE,
317-
"Initializing ContextualProvider Root with: {}",
318-
Arrays.toString(values));
319-
ROOT.addToContext(values);
320-
}
321-
} catch (IOException e) {
322-
throw new RuntimeException("Could not read context properties", e);
323-
} catch (ClassNotFoundException e) {
324-
throw new RuntimeException("Could not find Context Class", e);
325-
}
310+
ROOT = Wrap.onDemand(() -> ServiceLoader.load(RootContextSource.class)
311+
.findFirst()
312+
.map(RootContextSource::getRootContext)
313+
.orElseGet(() -> {
314+
try {
315+
var rootContext = new Context.Base(null, "ROOT", new Object[0]);
316+
InputStream resource = ClassLoader.getSystemClassLoader()
317+
.getResourceAsStream("org/comroid/api/context.properties");
318+
if (resource != null) {
319+
Properties props = new Properties();
320+
props.load(resource);
321+
322+
int c = 0;
323+
Object[] values = new Object[props.size()];
324+
for (Map.Entry<Object, Object> entry : props.entrySet()) {
325+
final int fc = c;
326+
Class<?> targetClass = Class.forName(String.valueOf(entry.getValue()));
327+
createInstance(targetClass).ifPresent(it -> values[fc] = it);
328+
c++;
329+
}
330+
Debug.logger.log(Level.FINE,
331+
"Initializing ContextualProvider Root with: {}",
332+
Arrays.toString(values));
333+
rootContext.addToContext(values);
334+
}
335+
return rootContext;
336+
} catch (IOException e) {
337+
throw new RuntimeException("Could not read context properties", e);
338+
} catch (ClassNotFoundException e) {
339+
throw new RuntimeException("Could not find Context Class", e);
340+
}
341+
}));
326342
}
327343

328344
@Getter protected final Set<Context> children;
329345
@Getter private final Set<Object> myMembers;
330346
private final Context parent;
331347
private final String name;
332348

333-
protected Base(Object... initialMembers) {
334-
this(ROOT, initialMembers);
335-
}
336-
337349
protected Base(@NotNull Context parent, Object... initialMembers) {
338350
this(parent, callerClass(1).getSimpleName(), initialMembers);
339351
}
340352

353+
protected Base(String name, Object... initialMembers) {
354+
this(null, name, initialMembers);
355+
}
356+
357+
protected Base(Object... initialMembers) {
358+
this(null, "ROOT", initialMembers);
359+
}
360+
341361
protected Base(Context parent, String name, Object... initialMembers) {
342362
this.myMembers = new HashSet<>();
343363
this.children = new HashSet<>();
@@ -349,10 +369,6 @@ protected Base(Context parent, String name, Object... initialMembers) {
349369
addToContext(initialMembers);
350370
}
351371

352-
protected Base(String name, Object... initialMembers) {
353-
this(ROOT, name, initialMembers);
354-
}
355-
356372
@Override
357373
public final @Nullable Context getParentContext() {
358374
return parent;

src/main/java/org/comroid/api/func/ext/Wrap.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ static <R extends DataNode, T extends Throwable> Wrap<R> exceptionally(ThrowingS
8787
return exceptionally(supplier, t -> Log.at(Level.SEVERE, "An internal error occurred", t));
8888
}
8989

90-
static <R extends DataNode, T extends Throwable> Wrap<R> exceptionally(@NotNull ThrowingSupplier<R, T> supplier, @NotNull Consumer<T> handler) {
90+
static <R extends DataNode, T extends Throwable> Wrap<R> exceptionally(
91+
@NotNull ThrowingSupplier<R, T> supplier, @NotNull Consumer<T> handler) {
9192
return () -> {
9293
try {
9394
return supplier.get();
@@ -98,6 +99,18 @@ static <R extends DataNode, T extends Throwable> Wrap<R> exceptionally(@NotNull
9899
};
99100
}
100101

102+
static <O> Wrap<O> onDemand(Supplier<O> source) {
103+
return new Wrap<>() {
104+
private O object;
105+
106+
@Override
107+
public O get() {
108+
if (object == null) object = source.get();
109+
return object;
110+
}
111+
};
112+
}
113+
101114
@Override
102115
default boolean isMutable() {
103116
return false;
@@ -221,11 +234,13 @@ default <R> R cast() throws ClassCastException {
221234
return remapper.apply(get());
222235
}
223236

224-
default <X, R> @NotNull Wrap<@Nullable R> combine(@Nullable Supplier<@Nullable X> other, BiFunction<T, @Nullable X, @Nullable R> accumulator) {
237+
default <X, R> @NotNull Wrap<@Nullable R> combine(
238+
@Nullable Supplier<@Nullable X> other, BiFunction<T, @Nullable X, @Nullable R> accumulator) {
225239
return () -> accumulate(other, accumulator);
226240
}
227241

228-
default <X, R> @Nullable R accumulate(@Nullable Supplier<@Nullable X> other, BiFunction<T, X, @Nullable R> accumulator) {
242+
default <X, R> @Nullable R accumulate(
243+
@Nullable Supplier<@Nullable X> other, BiFunction<T, X, @Nullable R> accumulator) {
229244
if (other == null || other.get() == null) return null;
230245
return accumulator.apply(get(), other.get());
231246
}
@@ -249,7 +264,8 @@ default void consume(Consumer<@Nullable T> consumer) {
249264
consumer.accept(get());
250265
}
251266

252-
default <EX extends Throwable> void ifPresentOrElseThrow(Consumer<T> consumer, Supplier<EX> exceptionSupplier) throws EX {
267+
default <EX extends Throwable> void ifPresentOrElseThrow(Consumer<T> consumer, Supplier<EX> exceptionSupplier)
268+
throws EX {
253269
if (isNonNull()) consume(consumer);
254270
else throw exceptionSupplier.get();
255271
}
@@ -276,7 +292,8 @@ default <R> R ifPresentMapOrElseGet(Function<? super T, ? extends R> consumer, S
276292
} else return task.get();
277293
}
278294

279-
default <R, X extends Throwable> R ifPresentMapOrElseThrow(Function<T, R> consumer, Supplier<X> exceptionSupplier) throws X {
295+
default <R, X extends Throwable> R ifPresentMapOrElseThrow(Function<T, R> consumer, Supplier<X> exceptionSupplier)
296+
throws X {
280297
if (isNonNull()) return into(consumer);
281298
throw exceptionSupplier.get();
282299
}
@@ -296,7 +313,8 @@ default <O> void ifBothPresent(@Nullable Supplier<O> other, BiConsumer<@NotNull
296313
}
297314
}
298315

299-
default <O, R> @Nullable R ifBothPresentMap(@Nullable Supplier<O> other, BiFunction<@NotNull T, @NotNull O, R> accumulator) {
316+
default <O, R> @Nullable R ifBothPresentMap(
317+
@Nullable Supplier<O> other, BiFunction<@NotNull T, @NotNull O, R> accumulator) {
300318
if (other != null) {
301319
O o = other.get();
302320
if (isNonNull() && o != null) return accumulator.apply(assertion(), o);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.comroid.api.func.util;
2+
3+
import org.comroid.api.func.ext.Context;
4+
5+
public interface RootContextSource {
6+
Context getRootContext();
7+
}

0 commit comments

Comments
 (0)