private void files(Path dir, Throwing.Consumer<Path> consumer) throws Exception { try (Stream<Path> stream = Files.walk(dir)) { Iterator<Path> files = stream.filter(Files::isRegularFile) .filter(it -> it.toString().endsWith(".svg")) .sorted() .iterator(); while (files.hasNext()) { consumer.accept(files.next()); } } }
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("")); } }
@SuppressWarnings("rawtypes") private void list(final Config conf, final String name, final Throwing.Consumer<Object> callback) { if (conf.hasPath(name)) { Object value = conf.getAnyRef(name); List values = value instanceof List ? (List) value : ImmutableList.of(value); for (Object it : values) { callback.accept(it); } } }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
/** * Run an operation over the resource. * * @param fn Function to apply. * @return A new try result. */ public Try run(Throwing.Consumer<R> fn) { return Try.run(() -> { try (R r1 = this.r.get()) { fn.accept(r1); } }); } }
/** * Always run the given action, works like a finally clause. Exception will be null in case of success. * * @param action Finally action. * @return This try result. */ public Try onComplete(Throwing.Consumer<Throwable> action) { try { action.accept(getCause().orElse(null)); return this; } catch (Throwable x) { return Try.failure(x); } }
/** * 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 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))); }
/** * Get access to a {@link Session}/{@link EntityManager} and do some work. * * @param callback Callback to run. * @throws Throwable If something goes wrong. */ default void accept(final Throwing.Consumer<Session> callback) throws Throwable { apply(session -> { callback.accept(session); return null; }); }
@SuppressWarnings("rawtypes") private void list(final Config conf, final String name, final Throwing.Consumer<Object> callback) { if (conf.hasPath(name)) { Object value = conf.getAnyRef(name); List values = value instanceof List ? (List) value : ImmutableList.of(value); for (Object it : values) { callback.accept(it); } } }
routeClasses, caseSensitiveRouting) .accept(it)) .throwException();
parsers, renderers, routeClasses, caseSensitiveRouting).accept(e);
@Override public void configure(final Env env, final Config conf, final Binder binder) throws Throwable { Path userdir = Paths.get(System.getProperty("user.dir")); Path workDirectory = Optional.ofNullable(this.workDirectory) .orElse(userdir); Path installDirectory = Optional.ofNullable(this.installDirectory).orElse(workDirectory); ProxyConfig proxy = new ProxyConfig(proxies(conf)); CacheResolver cache = Files.exists(userdir.resolve("pom.xml")) ? new MavenCacheResolver() : null; FrontendPluginFactory factory = new FrontendPluginFactory(workDirectory.toFile(), installDirectory.toFile(), cache); installNode(conf, nodeVersion, proxy, factory); Map<String, String> environment = environment(conf); NodeTask task = newTask(factory, conf, proxy, environment, nodeVersion); onSyncPackageJson(conf, workDirectory, task::executeSync); if (onStart == null && onStarted == null) { onStart = cmd -> cmd.execute("run", "build"); } if (onStart != null) { env.onStart(() -> onStart.accept(task)); } if (onStarted != null) { env.onStarted(() -> onStarted.accept(task)); } }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
/** * 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 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))); }
/** * Get access to a {@link Session}/{@link EntityManager} and do some work. * * @param callback Callback to run. * @throws Throwable If something goes wrong. */ default void accept(final Throwing.Consumer<Session> callback) throws Throwable { apply(session -> { callback.accept(session); return null; }); }