/** * A generic main method that auto-loads available modules and runs Bootique stack. Useful for apps that don't * care to customize their "main()". * * @param args app arguments passed by the shell. * @since 0.17 */ public static void main(String[] args) { Bootique.app(args).autoLoadModules().exec().exit(); }
/** * Creates and returns an instance of {@link BQRuntime} that contains all Bootique services, commands, etc. * This method is only needed if you are managing Bootique execution sequence manually. Normally you'd be using * {@link #exec()} method instead of this method. * * @return a new {@link BQRuntime} instance that contains all Bootique services, commands, etc. * @since 0.13 */ public BQRuntime createRuntime() { Injector injector = createInjector(); return createRuntime(injector); }
protected BQTestRuntimeBuilder(String[] args) { //exclude system variables and properties by setting the properties final HashMap<String, String> properties = new HashMap<>(); properties.put(EXCLUDE_SYSTEM_PROPERTIES, "true"); properties.put(EXCLUDE_SYSTEM_VARIABLES, "true"); this.properties = properties; this.bootique = Bootique.app(args).module(createPropertiesProvider()); }
Thread shutdownThread = createJVMShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownThread); try { o = createRuntime().run(); shutdown(shutdownManager, bootLogger); Runtime.getRuntime().removeShutdownHook(shutdownThread); o = processExceptions(ce.getCause(), ce); } catch (ProvisionException pe) { .map(Message::getCause) .orElse(null); o = processExceptions(cause, pe); } catch (Throwable th) { o = processExceptions(th, th); bootLogger.stderr(String.format("Error running command '%s': %s", getArgsAsString(), o.getMessage()), o.getException()); } else { bootLogger.stderr(String.format("Error running command '%s'", getArgsAsString()), o.getException());
/** Create the compiler runtime. * * @param args the command line arguments. * @return the runtime. */ @SuppressWarnings("static-method") protected BQRuntime createRuntime(String... args) { SARLStandaloneSetup.doPreSetup(); final BQRuntime runtime = Bootique.app(args).autoLoadModules().createRuntime(); SARLStandaloneSetup.doPostSetup(runtime.getInstance(Injector.class)); return runtime; }
/** * Adds a Module generated by the provider. Provider may optionally specify * that the Module overrides services in some other Module. * * @param moduleProvider a provider of Module and override spec. * @return this instance of test runtime builder. */ public T module(BQModuleProvider moduleProvider) { bootique.module(moduleProvider); return (T) this; }
/** * Starts a builder of Bootique runtime, initializing it with provided command-line arguments. * * @param args command-line arguments. * @return Bootique object that can be customized and then executed as an app via {@link #exec()} method. * @since 0.17 */ public static Bootique app(Collection<String> args) { if (args == null) { args = Collections.emptyList(); } return app(BootiqueUtils.toArray(Objects.requireNonNull(args))); }
/** * The main build method that creates and returns a {@link BQRuntime}. * * @return a new instance of {@link BQRuntime} configured in this builder. */ public BQRuntime createRuntime() { BQRuntime runtime = bootique.createRuntime(); runtimes.add(runtime); return runtime; }
Injector createInjector() { DeferredModulesSource modulesSource = new DeferredModulesSource(); Collection<BQModule> bqModules = new ArrayList<>(); // note that 'moduleMetadata' is invalid at this point; it will be initialized later in this method, which // is safe to do, as it won't be used until the Injector is created by the method caller. bqModules.add(coreModuleProvider(modulesSource).moduleBuilder().build()); BootiqueUtils.moduleProviderDependencies(builderProviders()) .forEach(p -> bqModules.add(p.moduleBuilder().build())); if (autoLoadModules) { autoLoadedProviders().forEach(p -> bqModules.add(p.moduleBuilder().build())); } // now that all modules are collected, finish 'moduleMetadata' initialization modulesSource.init(bqModules); // convert to Guice modules respecting overrides, etc. Collection<Module> modules = new RuntimeModuleMerger(bootLogger).toGuiceModules(bqModules); return Guice.createInjector(modules); }
/** * Instructs Bootique to load any modules available on classpath that expose {@link io.bootique.BQModuleProvider} * provider. Auto-loaded modules will be used in default configuration. Factories within modules will of course be * configured dynamically from YAML. * * @return this instance of test runtime builder. */ public T autoLoadModules() { bootique.autoLoadModules(); return (T) this; }
/** * Appends extra values to the test CLI arguments. * * @param args extra args to pass to Bootique. * @return this instance of test runtime builder. */ public T args(String... args) { bootique.args(args); return (T) this; }
/** * Starts a builder of Bootique runtime. * * @param args command-line arguments. * @return Bootique object that can be customized and then executed as an app via {@link #exec()} method. */ public static Bootique app(String... args) { if (args == null) { args = new String[0]; } return new Bootique(args); }
/** * @param bootLogger custom BootLogger to use for a given runtime. * @return this instance of test runtime builder. * @since 0.23 */ public T bootLogger(BootLogger bootLogger) { bootique.bootLogger(bootLogger); return (T) this; }
Thread shutdownThread = createJVMShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownThread); try { o = createRuntime().run(); shutdown(shutdownManager, bootLogger); Runtime.getRuntime().removeShutdownHook(shutdownThread); o = processExceptions(ce.getCause(), ce); } catch (ProvisionException pe) { o = processExceptions(pe.getCause(), pe); } catch (Throwable th) { o = processExceptions(th, th); bootLogger.stderr(String.format("Error running command '%s': %s", getArgsAsString(), o.getMessage()), o.getException()); } else { bootLogger.stderr(String.format("Error running command '%s'", getArgsAsString()), o.getException());
/** * @param moduleType custom Module class to add to Bootique DI runtime. * @return this instance of test runtime builder. * @see #autoLoadModules() */ public T module(Class<? extends Module> moduleType) { bootique.module(moduleType); return (T) this; }
/** * Starts a builder of Bootique runtime, initializing it with provided * command-line arguments. * * @param args command-line arguments. * @return Bootique object that can be customized and then executed as an * app via {@link #exec()} method. * @since 0.17 */ public static Bootique app(Collection<String> args) { if (args == null) { args = Collections.emptyList(); } return app(BootiqueUtils.toArray(Objects.requireNonNull(args))); }
/** * Creates runtime without starting it. Can be started via {@link BQDaemonTestFactory#start(BQRuntime)}. * * @return newly created managed runtime. * @since 0.23 */ public BQRuntime createRuntime() { BQRuntime runtime = bootique.createRuntime(); // wrap in BQRuntimeDaemon to handle thread pool shutdown and startup checks. BQRuntimeDaemon testRuntime = new BQRuntimeDaemon(runtime, startupCheck, startupTimeout, startupTimeoutTimeUnit); runtimes.put(runtime, testRuntime); return runtime; }
Injector createInjector() { DeferredModulesSource modulesSource = new DeferredModulesSource(); Collection<BQModule> bqModules = new ArrayList<>(); // note that 'moduleMetadata' is invalid at this point; it will be initialized later in this method, which // is safe to do, as it won't be used until the Injector is created by the method caller. bqModules.add(coreModuleProvider(modulesSource).moduleBuilder().build()); BootiqueUtils.moduleProviderDependencies(builderProviders()) .forEach(p -> bqModules.add(p.moduleBuilder().build())); if (autoLoadModules) { autoLoadedProviders().forEach(p -> bqModules.add(p.moduleBuilder().build())); } // now that all modules are collected, finish 'moduleMetadata' initialization modulesSource.init(bqModules); // convert to Guice modules respecting overrides, etc. Collection<Module> modules = new RuntimeModuleMerger(bootLogger).toGuiceModules(bqModules); return Guice.createInjector(modules); }
/** * Appends extra values to the test CLI arguments. * * @param args extra args to pass to Bootique. * @return this instance of test runtime builder. */ public T args(Collection<String> args) { bootique.args(args); return (T) this; }
/** * Starts a builder of Bootique runtime. * * @param args command-line arguments. * @return Bootique object that can be customized and then executed as an * app via {@link #exec()} method. */ public static Bootique app(String... args) { if (args == null) { args = new String[0]; } return new Bootique(args); }