|
10 | 10 | import org.comroid.api.func.exc.ThrowingSupplier; |
11 | 11 | import org.comroid.api.func.util.Debug; |
12 | 12 | import org.comroid.api.func.util.RootContextSource; |
| 13 | +import org.comroid.api.info.Log; |
13 | 14 | import org.comroid.api.java.ReflectionHelper; |
14 | 15 | import org.jetbrains.annotations.ApiStatus.Experimental; |
15 | 16 | import org.jetbrains.annotations.ApiStatus.Internal; |
@@ -307,38 +308,45 @@ class Base implements Context { |
307 | 308 | @SuppressWarnings("ConstantConditions") public static final Supplier<Context> ROOT; |
308 | 309 |
|
309 | 310 | 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); |
329 | 343 | } |
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 | + }); |
342 | 350 | } |
343 | 351 |
|
344 | 352 | @Getter protected final Set<Context> children; |
|
0 commit comments