@Override protected URL resolve(String path) throws Exception { return sources.stream() .map(source -> source.resolve(path)) .filter(Files::exists) .findFirst() .map(throwingFunction(it -> it.toUri().toURL())) .orElse(null); } }
private static <T> T newObject(final Class<T> klass) { return throwingSupplier(klass::newInstance).get(); }
/** * @return Generate a weak Etag using the {@link #path()}, {@link #lastModified()} and * {@link #length()}. */ @Nonnull default String etag() { try { StringBuilder b = new StringBuilder(32); b.append("W/\""); BaseEncoding b64 = BaseEncoding.base64(); int lhash = resource().toURI().hashCode(); b.append(b64.encode(Longs.toByteArray(lastModified() ^ lhash))); b.append(b64.encode(Longs.toByteArray(length() ^ lhash))); b.append('"'); return b.toString(); } catch (URISyntaxException x) { throw Throwing.sneakyThrow(x); } }
private static <V> V fnWrap(Supplier<V> fn, java.util.function.Function<Throwable, Exception> wrapper) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } }
private void onSyncPackageJson(Config conf, Path workDirectory, Throwing.Consumer<String> action) throws IOException { Path tmp = Paths.get(conf.getString("application.tmpdir"), "package.json"); Files.createDirectories(tmp); String sha1 = Hashing.sha256() .hashBytes(Files.readAllBytes(workDirectory.resolve("package.json"))) .toString(); Path lastSha1 = tmp.resolve(sha1); if (!Files.exists(lastSha1) || !Files.exists(workDirectory.resolve("node_modules"))) { action.accept("install"); Try.of(Files.walk(tmp)) .run(files -> files.filter(f -> !f.equals(tmp)).forEach(throwingConsumer(Files::deleteIfExists))); Files.write(tmp.resolve(lastSha1), Arrays.asList("")); } }
private static void runWrap(Runnable action, java.util.function.Function<Throwable, Exception> wrapper) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } }
@Override public void close() throws Exception { try { Optional.ofNullable(resource) .ifPresent(Throwing.throwingConsumer(AutoCloseable::close)); } finally { if (parent instanceof ProxyCloseable) { ((ProxyCloseable) parent).close(); } else { Optional.ofNullable(parentResource) .ifPresent(Throwing.throwingConsumer(AutoCloseable::close)); } } }
@SuppressWarnings("rawtypes") static Optional<Class> findClass(final ClassLoader loader, final String name) { return Stream.of(loader, Thread.currentThread().getContextClassLoader()) // we don't care about exception .map(Throwing.<ClassLoader, Class>throwingFunction(cl -> cl.loadClass(name)) .orElse((Class) null)) .filter(Objects::nonNull) .findFirst(); }
@Override public void contextInitialized(final ServletContextEvent sce) { ServletContext ctx = sce.getServletContext(); String appClass = ctx.getInitParameter("application.class"); requireNonNull(appClass, "Context param NOT found: application.class"); Jooby.run(Throwing.throwingSupplier(() -> { Jooby app = (Jooby) ctx.getClassLoader().loadClass(appClass).newInstance(); ctx.setAttribute(Jooby.class.getName(), app); return app; }), "application.path=" + ctx.getContextPath(), "server.module=" + ServletModule.class.getName()); }
throw Throwing.sneakyThrow(ex.getCause()); } catch (Exception ex) { throw Throwing.sneakyThrow(ex);
private static <R, X extends Throwable> R fnRecover(Supplier<R> fn, Class<? extends X> type, java.util.function.Function<X, R> recover) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } if (type.isInstance(x)) { return recover.apply(type.cast(x)); } throw sneakyThrow(x); } }
public URLClassLoader newClassLoader() { return toClassLoader( classpath().stream() .map(throwingFunction(f -> f.toURI().toURL())) .collect(Collectors.toList()), getClass().getClassLoader()); }
@Override public MemcachedClient get() { client = throwingSupplier(() -> { ConnectionFactory connectionFactory = builder.build(); this.builder = null; return new MemcachedClient(connectionFactory, servers); }).get(); return client; }
@Override public V get() { throw Throwing.sneakyThrow(x); }
private static <V, X extends Throwable> V fnUnwrap(Supplier<V> fn, Class<? extends X> type) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } }
public ExecSchedulerHook(final Map<String, Executor> executors) { // we don't want eager initialization of Schedulers schedulers = Throwing.<String, Scheduler>throwingFunction( name -> Schedulers.from(executors.get(name))) .memoized(); }
binder.bind(PluginContext.class).toProvider(throwingSupplier(ctx::get)::get);
private static <X extends Throwable> void runOnFailure(Runnable action, Class<? extends X> type, java.util.function.Consumer<X> consumer) { try { action.tryRun(); } catch (Throwable x) { if (type.isInstance(x)) { consumer.accept(type.cast(x)); } throw sneakyThrow(x); } }
private static <X extends Throwable> void runUnwrap(Runnable action, Class<? extends X> type) { try { action.tryRun(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } }
@Override public Collection<Import> apply(final String url, final Import previous) { String fname = nameWithExtension(url); List<String> segments = Splitter.on('/').trimResults().omitEmptyStrings() .splitToList(previous.getAbsoluteUri().toString()); String relative = segments.subList(0, segments.size() - 1).stream() .collect(Collectors.joining("/", "/", "")); return Arrays.asList(relative + fname, fname) .stream() .map(resolver::apply) .filter(it -> it != null) .findFirst() .map(throwingFunction(it -> Try.with(it.toURL()::openStream) .apply(in -> new String(ByteStreams.toByteArray(in), StandardCharsets.UTF_8)) .map(content -> Arrays.asList(new Import(it, it, content))) .get() )) .orElse(null); }