Skip to content

Commit 12e9824

Browse files
author
burdo
committed
fix context initialization
1 parent 588b796 commit 12e9824

1 file changed

Lines changed: 39 additions & 31 deletions

File tree

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.comroid.api.func.exc.ThrowingSupplier;
1111
import org.comroid.api.func.util.Debug;
1212
import org.comroid.api.func.util.RootContextSource;
13+
import org.comroid.api.info.Log;
1314
import org.comroid.api.java.ReflectionHelper;
1415
import org.jetbrains.annotations.ApiStatus.Experimental;
1516
import org.jetbrains.annotations.ApiStatus.Internal;
@@ -307,38 +308,45 @@ class Base implements Context {
307308
@SuppressWarnings("ConstantConditions") public static final Supplier<Context> ROOT;
308309

309310
static {
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++;
311+
ROOT = Wrap.onDemand(() -> {
312+
try {
313+
return ServiceLoader.load(RootContextSource.class)
314+
.findFirst()
315+
.map(RootContextSource::getRootContext)
316+
.orElseGet(() -> {
317+
try {
318+
var rootContext = new Context.Base(null, "ROOT", new Object[0]);
319+
InputStream resource = ClassLoader.getSystemClassLoader()
320+
.getResourceAsStream("/org/comroid/api/context.properties");
321+
if (resource != null) {
322+
Properties props = new Properties();
323+
props.load(resource);
324+
325+
int c = 0;
326+
Object[] values = new Object[props.size()];
327+
for (Map.Entry<Object, Object> entry : props.entrySet()) {
328+
final int fc = c;
329+
Class<?> targetClass = Class.forName(String.valueOf(entry.getValue()));
330+
createInstance(targetClass).ifPresent(it -> values[fc] = it);
331+
c++;
332+
}
333+
Debug.logger.log(Level.FINE,
334+
"Initializing ContextualProvider Root with: {}",
335+
Arrays.toString(values));
336+
rootContext.addToContext(values);
337+
}
338+
return rootContext;
339+
} catch (IOException e) {
340+
throw new RuntimeException("Could not read context properties", e);
341+
} catch (ClassNotFoundException e) {
342+
throw new RuntimeException("Could not find Context Class", e);
329343
}
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-
}));
344+
});
345+
} catch (Throwable t) {
346+
Log.at(Level.WARNING, "Unable to initialize Root Context", t);
347+
return null;
348+
}
349+
});
342350
}
343351

344352
@Getter protected final Set<Context> children;

0 commit comments

Comments
 (0)