MainDispatch { public static void dispatch(Class<?> main, String[] args, InstanceType instance, InstanceFlavor flavor) { if (MainDispatch.main != null) throw new IllegalStateException("Dispatch cannot be called more than once"); MainDispatch.main = Objects.requireNonNull(main); MainDispatch.instance = Objects.requireNonNull(instance); MainDispatch.flavor = Objects.requireNonNull(flavor); final long timestamp = System.currentTimeMillis(); final Logger log = LoggerFactory.getLogger(MainDispatch.class); Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { log.error("An unexpected exception has occurred", throwable); }); Runtime.getRuntime().addShutdownHook(new Thread(() -> { shutdown.forEach(task -> { try { task.execute(new TaskOutcome(task.toString())); } catch (Exception e) { log.error("Failed to execute shutdown task", e); } }); })); try { main.getDeclaredMethod("main", String[].class).invoke(null, (Object) args); } catch (Exception e) { throw new RuntimeException("Failed to invoke main method in class: " + main.getName(), e); } for (Task task : tasks) { if (task.initMetadata == null) throw new RuntimeException("Unregistered initialization task class"); TaskOutcome outcome = new TaskOutcome(task.initMetadata.name()); try { task.outcome = task.execute(outcome); } catch (Exception e) { task.outcome = outcome.failure(e); } if (!task.outcome.isSkipped() && !outcome.getOutcome().getResult() && task.initMetadata.fatal()) { logTaskSummary(log); System.exit(1); } } if (tasks.stream().filter(t -> !t.outcome.getOutcome().getResult()).count() != 0) logTaskSummary(log); else if (Config.STARTUP_SUMMARY.value().orElse(true)) logTaskSummary(log); if (idle != null) idle.start(); tasks = null; log.info("Initialization completed in {} ms", System.currentTimeMillis() - timestamp); } private  MainDispatch(); static Class<?> getMain(); static InstanceType getInstance(); static InstanceFlavor getInstanceFlavor(); static IdleLoop getIdleLoop(); static void dispatch(Class<?> main, String[] args, InstanceType instance, InstanceFlavor flavor); static void register(IdleLoop idle); static void registerIdle(Supplier<Boolean> task); static void register(Task task); static void register(Class<?> c);  }
@Test @DisplayName("Dispatch a class that registers nothing") void dispatch_1() { MainDispatch.dispatch(EmptyTest.class, new String[] {}, InstanceType.CHARCOAL, InstanceFlavor.NONE); }
@Test @DisplayName("Dispatch a class that is successful") void dispatch_2() { MainDispatch.dispatch(SuccessTest.class, new String[] { "37434" }, InstanceType.CHARCOAL, InstanceFlavor.NONE); }
@Test @DisplayName("Dispatch a class that encounters a non-fatal error") void dispatch_3() { MainDispatch.dispatch(NonfatalTest.class, new String[] { "37434" }, InstanceType.CHARCOAL, InstanceFlavor.NONE); }