/** * Release nodejs and v8 resources. */ public void release() { Try.run(scope::release); node.release(); }
@Override public void writeSuccess() { Try.run(success::invoke) .onFailure(cause -> log.error("Error while invoking success callback", cause)); }
@Override public void run() { if (watcher != null) { log.debug("stopping: watcher"); Try.run(watcher::stop).onFailure(ex -> log.debug("Stop of watcher resulted in error", ex)); } commands.forEach(cmd -> { log.debug("stopping: " + cmd); Try.run(cmd::stop).onFailure(ex -> log.error("Stop of " + cmd + " resulted in error", ex)); }); } }
protected void fireCloseEvent() { Throwing.Runnable task = onclose.getAndSet(null); if (task != null) { Try.run(task).onFailure(ex -> log.error("close callback resulted in error", ex)); } }
public void close() throws Exception { reporters.forEach(r -> Try.run(r::close) .onFailure(cause -> log.error("close of {} resulted in error", r, cause))); }
public void shutdown() { drivers.forEach(it -> Try.run(it::close)); super.stop(); }
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; }
@Override public void writeResponseContent(String content) { Try.run(() -> rsp.send(content)); }
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; }
private String sha1(final File dir, final File sprite, final File css) throws IOException { try (Stream<Path> stream = Files.walk(dir.toPath())) { Hasher sha1 = Hashing.sha1().newHasher(); stream.filter(p -> !Files.isDirectory(p)) .forEach(p -> Try.run(() -> sha1.putBytes(Files.readAllBytes(p)))); if (sprite.exists()) { sha1.putBytes(Files.readAllBytes(sprite.toPath())); } if (css.exists()) { sha1.putBytes(Files.readAllBytes(css.toPath())); } return BaseEncoding.base16().encode(sha1.hash().asBytes()).toLowerCase(); } }
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))); }
public static void forceConverter(final AsyncRepository repo, final JacksonMapper converter) { Try.run(() -> FIELD.apply(repo.getClass()).set(repo, converter)) .onFailure(x -> log.warn("Set converter resulted in error", x)); } }
@Override public void writeFailed(final Throwable cause) { Try.run(() -> err.onError(cause)) .onFailure(ex -> log.error("Error while invoking err callback", ex)); } };
public void done(final Optional<Throwable> cause) { if (complete.size() > 0) { for (Route.Complete h : complete) { Try.run(() -> h.handle(req, this, cause)) .onFailure(x -> LoggerFactory.getLogger(Response.class) .error("complete listener resulted in error", x)); } complete.clear(); } end(); }
@Override protected void closeInternal() { Try.run(() -> rsp.closeOutput()) .onFailure(cause -> log.debug("error while closing connection", cause)); }
static Handler tryPage(final Handler handler, final Logger log) { return (req, rsp, ex) -> Try.run(() -> handler.handle(req, rsp, ex)).onFailure(cause -> log.debug("execution of pretty err page resulted in exception", cause)); }
@Override public void run() { String sseId = sse.id(); log.debug("running heart beat for {}", sseId); Try.run(() -> sse.send(Optional.of(sseId), HEART_BEAT).whenComplete((id, x) -> { if (x != null) { log.debug("connection lost for {}", sseId, x); sse.fireCloseEvent(); Try.run(sse::close); } else { log.debug("reschedule heart beat for {}", id); // reschedule sse.keepAlive(retry); } })); }
public void release() { Try.run(mem::release) .onComplete(() -> releaseNow(v8)); }
private void handleErr(final Throwable cause) { Try.run(() -> { if (SILENT.test(cause)) { log.debug("execution of WS" + path() + " resulted in exception", cause); } else { exceptionCallback.onError(cause); } }) .onComplete(() -> cleanup(cause)) .throwException(); }
/** * 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(); } }