routeClasses, caseSensitiveRouting) .accept(it)) .throwException();
routeClasses, caseSensitiveRouting) .accept(it)) .throwException();
parsers, renderers, routeClasses, caseSensitiveRouting).accept(e);
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
/** * Run an operation over the resource. * * @param fn Function to apply. * @return A new try result. */ public Try run(Throwing.Consumer<R> fn) { return Try.run(() -> { try (R r1 = this.r.get()) { fn.accept(r1); } }); } }
/** * Always run the given action, works like a finally clause. Exception will be null in case of success. * * @param action Finally action. * @return This try result. */ public Try onComplete(Throwing.Consumer<Throwable> action) { try { action.accept(getCause().orElse(null)); return this; } catch (Throwable x) { return Try.failure(x); } }
/** * Execute the given nodejs callback and automatically releaseNow v8 and nodejs resources. * * @param basedir Base dir where to deploy a library. * @param callback Nodejs callback. */ public static void run(final File basedir, final Throwing.Consumer<Nodejs> callback) { Nodejs node = new Nodejs(basedir); Try.run(() -> callback.accept(node)) .onComplete(node::release) .throwException(); } }
@Override default void accept(V v) { runAction(() -> tryAccept(v)); }
/** * Execute the given action before throwing the exception. * * @param type Exception type filter. * @param action Action to execute. * @param <X> Exception type. * @param <V1> Input type. * @return A new consumer with a listener action. */ default <V1 extends V, X extends Throwable> Consumer<V1> onFailure(Class<? extends X> type, java.util.function.Consumer<X> action) { return value -> runOnFailure(() -> tryAccept(value), type, action); }
/** * Unwrap an exception and rethrow. Useful to produce clean/shorter stacktraces. * * @param type Type to unwrap. * @param <V1> Input type * @param <X> Exception type. * @return A new consumer. */ default <V1 extends V, X extends Throwable> Consumer<V1> unwrap(Class<? extends X> type) { return value -> runUnwrap(() -> tryAccept(value), type); } }
private static void fireStop(final Jooby app, final Logger log, final List<Throwing.Consumer<Registry>> onStop) { // stop services onStop.forEach(c -> Try.run(() -> c.accept(app)) .onFailure(x -> log.error("shutdown of {} resulted in error", c, x))); }
/** * Get access to a {@link Session}/{@link EntityManager} and do some work. * * @param callback Callback to run. * @throws Throwable If something goes wrong. */ default void accept(final Throwing.Consumer<Session> callback) throws Throwable { apply(session -> { callback.accept(session); return null; }); }
/** * Execute the given action before throwing the exception. * * @param action Action to execute. * @param <V1> Input type. * @return A new consumer with a listener action. */ default <V1 extends V> Consumer<V1> onFailure(java.util.function.Consumer<Throwable> action) { return onFailure(Throwable.class, action); }
@SuppressWarnings("rawtypes") private void list(final Config conf, final String name, final Throwing.Consumer<Object> callback) { if (conf.hasPath(name)) { Object value = conf.getAnyRef(name); List values = value instanceof List ? (List) value : ImmutableList.of(value); for (Object it : values) { callback.accept(it); } } }
/** * Wrap an exception as new exception provided by the given wrap function. * * @param wrapper Wrap function. * @param <V1> Input type * @return A new consumer. */ default <V1 extends V> Consumer<V1> wrap( java.util.function.Function<Throwable, Exception> wrapper) { return value -> runWrap(() -> tryAccept(value), wrapper); }
parsers, renderers, routeClasses, caseSensitiveRouting).accept(e);