@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(); } } }
@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); }
/** * Functional try-with-resources: * * <pre>{@code * byte[] content = Try.with(() -> newInputStream()) * .apply(in -> read(in)) * .get(); * * }</pre> * * Jdbc example: * * <pre>{@code * Try.with(() -> newConnection()) * .map(c -> c.preparedStatement("...")) * .map(stt -> stt.executeQuery()) * .apply(rs-> { * return res.getString("column"); * }) * .get(); * * }</pre> * * @param r1 Input resource. * @param <R> Resource type. * @return A resource handler. */ public final static <R extends AutoCloseable> ResourceHandler<R> with(Throwing.Supplier<R> r1) { return new ResourceHandler<>(r1); }
@Override public void send(final ByteBuffer buffer) throws Exception { Try.of(Channels.newChannel(rsp.getOutputStream())) .run(channel -> channel.write(buffer)) .onSuccess(() -> committed = true); }
static String readString(final ClassLoader loader, final String path) { return Try.with(() -> loader.getResourceAsStream("whoops/" + path)) .apply(stream -> new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8)) .get(); }
/** * Map the resource to a new closeable resource. * * @param fn Mapper. * @param <V> New resource type. * @return A new resource handler. */ public <V extends AutoCloseable> ResourceHandler<V> map(Throwing.Function<R, V> fn) { return new ResourceHandler<>(new ProxyCloseable<>(this.r, fn)); }
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("")); } }
@Override public void send(final ByteBuffer buffer) throws Exception { Try.of(Channels.newChannel(rsp.getOutputStream())) .run(channel -> channel.write(buffer)) .onSuccess(() -> committed = true); }
static String readString(final ClassLoader loader, final String path) { return Try.with(() -> loader.getResourceAsStream("whoops/" + path)) .apply(stream -> new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8)) .get(); }