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 void copy(final InputStream in, final OutputStream out) { Try.of(in, out) .run(ByteStreams::copy) .throwException(); }
method.invoke(owner); }).unwrap(InvocationTargetException.class) .throwException(); });
private void tryOption(final Object source, final Config config, final Method option) { Try.run(() -> { String optionName = option.getName().replace("set", ""); Object optionValue = config.getAnyRef(optionName); Class<?> optionType = Primitives.wrap(option.getParameterTypes()[0]); if (Number.class.isAssignableFrom(optionType) && optionValue instanceof String) { // either a byte or time unit try { optionValue = config.getBytes(optionName); } catch (ConfigException.BadValue ex) { optionValue = config.getDuration(optionName, TimeUnit.MILLISECONDS); } if (optionType == Integer.class) { // to int optionValue = ((Number) optionValue).intValue(); } } log.debug("{}.{}({})", source.getClass().getSimpleName(), option.getName(), optionValue); option.invoke(source, optionValue); }).unwrap(InvocationTargetException.class) .throwException(); }
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(); }
@Override public void handle(final Request req, final Response rsp, final Optional<Throwable> cause) { if (handle.isClosed()) { logger.warn("closed handle: {}", handle); } else { logger.debug("closing handling: {}", handle); Try.of(handle) .run(h -> { if (h.isInTransaction()) { if (cause.isPresent()) { logger.debug("rollback transaction: {}", handle, cause.get()); h.rollback(); } else { logger.debug("commit transaction {}", handle); h.commit(); } } }).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(); } }
public MockUnit run(final Block... blocks) throws Exception { for (Block block : this.blocks) { Try.run(() -> block.run(this)) .throwException(); } mockClasses.forEach(PowerMock::replay); partialMocks.forEach(PowerMock::replay); mocks.forEach(EasyMock::replay); for (Block main : blocks) { Try.run(() -> main.run(this)).throwException(); } mocks.forEach(EasyMock::verify); partialMocks.forEach(PowerMock::verify); mockClasses.forEach(PowerMock::verify); return this; }
Runtime.getRuntime().addShutdownHook(new Thread(() -> Try.run(watcher::stop) .onComplete(() -> connection.close()) .throwException())); watcher.start();
caseSensitiveRouting) .accept(it)) .throwException();
private Executor executor() { if (executor == null) { if (this.host.startsWith("https://")) { Try.run(() -> { SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(null, (chain, authType) -> true) .build(); builder.setSSLContext(sslContext); builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); }).throwException(); } client = builder.build(); executor = Executor.newInstance(client); if (creds != null) { executor.auth(creds); } } return executor; }
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 void copy(final InputStream in, final OutputStream out) { Try.of(in, out) .run(ByteStreams::copy) .throwException(); }
method.invoke(owner); }).unwrap(InvocationTargetException.class) .throwException(); });
private void tryOption(final Object source, final Config config, final Method option) { Try.run(() -> { String optionName = option.getName().replace("set", ""); Object optionValue = config.getAnyRef(optionName); Class<?> optionType = Primitives.wrap(option.getParameterTypes()[0]); if (Number.class.isAssignableFrom(optionType) && optionValue instanceof String) { // either a byte or time unit try { optionValue = config.getBytes(optionName); } catch (ConfigException.BadValue ex) { optionValue = config.getDuration(optionName, TimeUnit.MILLISECONDS); } if (optionType == Integer.class) { // to int optionValue = ((Number) optionValue).intValue(); } } log.debug("{}.{}({})", source.getClass().getSimpleName(), option.getName(), optionValue); option.invoke(source, optionValue); }).unwrap(InvocationTargetException.class) .throwException(); }
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(); } }