public boolean exists(final Predicate<Path> filter) { try (Stream<Path> walk = Try.apply(() -> Files.walk(root)).orElse(Stream.empty())) { return walk .skip(1) .anyMatch(filter); } }
/** * 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(); } }
private static void copy(final InputStream in, final OutputStream out) { Try.of(in, out) .run(ByteStreams::copy) .throwException(); }
public void release() { Try.run(mem::release) .onComplete(() -> releaseNow(v8)); }
log.debug("{}", uri); Path outdir = this.basedir.toPath().resolve("node_modules").resolve(library.replace("/", ".")); Optional<Path> basedir = Try.apply(() -> Paths.get(uri)).toOptional(); String libroot = Route.normalize("/" + library); try (Library lib = loadLibrary(uri)) { .toArray(new StandardCopyOption[this.coptions.size()]); Try.run(() -> Files.copy(it, output, coptions)) .onFailure(x -> log.error("can't copy {}", it, x));
public void end() { try { if (rollbackOnly) { rollback(); } else { commit(); } } finally { if (readOnly) { setConnectionReadOnly(false); } String sessionId = oid(session); log.debug("closing session: {}", sessionId); Try.run(session::close) .onFailure(x -> log.error("session.close() resulted in exception: {}", sessionId, x)) .onSuccess(() -> log.debug("session closed: {}", sessionId)); unbind(session.getSessionFactory()); } }
Try.run(() -> { method.setAccessible(true); method.invoke(owner); }).unwrap(InvocationTargetException.class) .throwException(); });
File workdir = new File(Try.apply(() -> conf.getString("application.tmpdir")) .orElse(System.getProperty("java.io.tmpdir"))); Float h = new Float(params.getDouble(3)); V8Function callback = (V8Function) params.get(4); Try.run(() -> { try (FileReader in = new FileReader(svgPath); OutputStream out = new FileOutputStream(pngPath)) { .onSuccess(() -> callback.call(null, null)) .onFailure(x -> { log.debug("png-fallback resulted in exception", x); callback.call(null, toV8Array(v8, Arrays.asList(x.getMessage()))); sha1files.forEach(it -> Try.run(() -> Files.delete(it)));
private Jooby hackInjector(final Jooby app) { Try.run(() -> { Field field = Jooby.class.getDeclaredField("injector"); field.setAccessible(true); Injector injector = proxyInjector(getClass().getClassLoader(), registry); field.set(app, injector); registry.put(Key.get(Injector.class), injector); }).throwException(); return app; }
@Override public void tryRun() throws Throwable { if (dep == null) { return; } Try.run(() -> { Optional<Method> shutdown = Arrays.stream(dep.getClass().getMethods()) .filter(m -> m.getName().startsWith("shutdown") && m.getParameterCount() == 0 && Modifier.isPublic(m.getModifiers())) .findFirst(); if (shutdown.isPresent()) { log.debug("stopping {}", dep); shutdown.get().invoke(dep); } else { log.debug("no shutdown method found for: {}", dep); } }).unwrap(InvocationTargetException.class) .onComplete(() -> this.dep = null) .throwException(); }
private static List<URI> expandPath(final ClassLoader loader, final String pattern) { List<URI> result = new ArrayList<>(); File file = new File(pattern); if (file.exists()) { result.add(file.toURI()); } Try.run(() -> Collections.list(loader.getResources(pattern)) .stream() .map(it -> Try.apply(it::toURI).get()) .forEach(result::add)); return result; }
/** * Release nodejs and v8 resources. */ public void release() { Try.run(scope::release); node.release(); }
/** * Run the given action if and only if this is a failure. * * @param action Failure action/listener. * @return This try. */ @Override public Value<V> onFailure(final Consumer<? super Throwable> action) { super.onFailure(action); return this; }
/** * Always run the given action, works like a finally clause. * * @param action Finally action. * @return This try result. */ @Override public Value<V> onComplete(Throwing.Runnable action) { return (Value<V>) super.onComplete(action); }
@Override public <X extends Throwable> Value<V> unwrap(Class<? extends X> type) { return (Value<V>) super.unwrap(type); }
/** * Run the given action if and only if this is a success. * * @param action Success listener. * @return This try. */ @Override public Value<V> onSuccess(final Runnable action) { super.onSuccess(action); return this; }
buckets.forEach(n -> Try.apply(() -> r.require(n, Bucket.class).close()) .onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x)) .orElse(false)); Try.run(cluster::disconnect) .onFailure(x -> log.debug("disconnect operation resulted in exception", x)); Try.run(cenv::shutdown) .onFailure(x -> log.debug("environment shutdown resulted in exception", x)); });
@Override default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable { Sse sse = req.require(Sse.class); String path = req.path(); rsp.send(new Deferred(deferred -> { try { sse.handshake(req, () -> { Try.run(() -> handle(req, sse)) .onSuccess(() -> deferred.resolve(null)) .onFailure(ex -> { deferred.reject(ex); Logger log = LoggerFactory.getLogger(Sse.class); log.error("execution of {} resulted in error", path, ex); }); }); } catch (Exception ex) { deferred.reject(ex); } })); }