@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); }
private Integer statusCodeFor(MethodInsnNode node) { if (node.name.equals("noContent")) { return 204; } if (node.name.equals("with")) { AbstractInsnNode previous = node.getPrevious(); if (previous instanceof IntInsnNode) { return ((IntInsnNode) previous).operand; } if (previous instanceof FieldInsnNode) { String owner = ((FieldInsnNode) previous).owner.replace("/", "."); if (owner.equals(Status.class.getName())) { String statusName = ((FieldInsnNode) previous).name; return Try.apply(() -> Status.class.getDeclaredField(statusName).get(null)) .map(it -> ((Status) it).value()) .orElse(null); } } } return null; }
public static List<CrashFSDriver> parse(final ClassLoader loader, final List<CrashPredicate> paths) { List<CrashFSDriver> drivers = new ArrayList<>(); boolean loginFound = false; for (CrashPredicate path : paths) { for (URI uri : expandPath(loader, path.name)) { // classpath first CrashFSDriver driver = Try .apply(() -> FileSystems.newFileSystem(uri, Collections.emptyMap())) .map(it -> Try.apply(() -> new CrashFSDriver(uri, it, path)).get()) // file system fallback .recover(x -> Try.apply(() -> new CrashFSDriver(uri, Paths.get(uri), path)).get()) .get(); // HACK to make sure login.groovy takes precedence over default one driver.log.debug("driver created: {}", driver); if (loginFound) { driver.filter = FLOGIN.negate().and(driver.filter); } else if (driver.exists(FLOGIN)) { driver.log.debug(" login.groovy found: {}", driver); loginFound = true; } drivers.add(driver); } } return drivers; }
private Integer statusCodeFor(MethodInsnNode node) { if (node.name.equals("noContent")) { return 204; } if (node.name.equals("with")) { AbstractInsnNode previous = node.getPrevious(); if (previous instanceof IntInsnNode) { return ((IntInsnNode) previous).operand; } if (previous instanceof FieldInsnNode) { String owner = ((FieldInsnNode) previous).owner.replace("/", "."); if (owner.equals(Status.class.getName())) { String statusName = ((FieldInsnNode) previous).name; return Try.apply(() -> Status.class.getDeclaredField(statusName).get(null)) .map(it -> ((Status) it).value()) .orElse(null); } } } return null; }
private Library loadLibrary(final URI lib) { return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap())) .map(it -> Try.apply(() -> new Library(it)).get()) .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get()) .get(); }
private Config dbConfig(final String key, final Config source) { Object db = source.getAnyRef(key); if (db instanceof String) { // embedded db? return Try.apply(() -> source.getConfig("databases." + db)) .map(it -> { // Rewrite embedded db it = it.getConfig("dataSource"); Config dbtree = it.withValue("url", ConfigValueFactory.fromAnyRef( it.getString("url").replace("{mem.seed}", System.currentTimeMillis() + ""))); // write embedded with current key return ConfigFactory.empty() .withValue(key, dbtree.root()) .withFallback(source); }).orElseGet(() -> { // assume it is a just the url return ConfigFactory.empty() .withValue(key + ".url", ConfigValueFactory.fromAnyRef(db.toString())) .withFallback(source); }); } else { return source; } }
private Library loadLibrary(final URI lib) { return Try.apply(() -> FileSystems.newFileSystem(lib, Maps.newHashMap())) .map(it -> Try.apply(() -> new Library(it)).get()) .recover(x -> Try.apply(() -> new Library(Paths.get(lib))).get()) .get(); }