/** Creates a jar with a Class-Path entry to workaround the windows classpath limitation. */ private static File toJarWithClasspath(Iterable<File> files) { return Errors.rethrow().get(() -> { File jarFile = File.createTempFile(LONG_CLASSPATH_JAR_PREFIX, ".jar"); try (ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile)))) { zip.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(zip, StandardCharsets.UTF_8)))) { pw.println("Manifest-Version: 1.0"); String classPath = CollectionUtils.join(" ", CollectionUtils.collect(files, File::toURI)); String classPathEntry = "Class-Path: " + classPath; pw.println(CollectionUtils.join("\n ", classPathEntry.split(MATCH_CHUNKS_OF_70_CHARACTERS))); } } return jarFile; }); }
/** Returns true if the given directory exists, and waits up to 500ms for the directory to exist. */ public static boolean dirExists(File dir) { long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < MS_RETRY) { File refreshed = Errors.rethrow().get(dir::getCanonicalFile); if (refreshed.exists() && refreshed.isDirectory()) { return true; } else { Errors.rethrow().run(() -> Thread.sleep(1)); } } return false; }
/** Returns the content of template.build.properties as a String. */ static String rawFile() { return Errors.rethrow().get(() -> { URL url = PdeBuildProperties.class.getResource("template.build.properties"); System.out.println("url=" + url); byte[] content = Resources.toByteArray(url); return new String(content, StandardCharsets.UTF_8); }); }
Food thrown = rethrowing.get(() -> cook("spaghetti")); // no default needed
/** Returns a map from every bundle-id to its corresponding 3-part version (the qualifier is dropped). */ public static Map<String, String> bundleToVersion(EclipseRelease release) { // warn if the user is asking for a too-old version of eclipse, but go ahead and try anyway just in case if (release.version().compareTo(FIRST_ON_CENTRAL.version()) < 0) { System.err.println(FIRST_ON_CENTRAL.version() + " was the first eclipse release that was published on MavenCentral."); } File versionFolder = new File(GoomphCacheLocations.eclipseReleaseMetadata(), release.version().toString()); FileMisc.mkdirs(versionFolder); File artifactsJar = new File(versionFolder, ARTIFACTS_JAR); if (artifactsJar.exists() && artifactsJar.length() > 0) { try { return parseFromFile(artifactsJar); } catch (Exception e) { e.printStackTrace(); System.err.println("Retrying download..."); FileMisc.forceDelete(artifactsJar); } } return Errors.rethrow().get(() -> { byte[] content = Resources.toByteArray(new URL(release.updateSite() + "artifacts.jar")); Files.write(content, artifactsJar); return parseFromFile(artifactsJar); }); }
void run() { Set<File> files = new HashSet<>(); File p2asmaven = project.file(destination); groups.forEach((group, action) -> { // populate the def AsMavenGroup def = new AsMavenGroup(group); action.execute(def); // run it AsMavenGroupImpl impl = Errors.rethrow().get(() -> def.run(project, p2asmaven)); // keep track of what is clean files.add(impl.dirP2()); files.add(impl.dirP2Runnable()); files.add(impl.dirMavenGroup()); files.add(impl.tokenFile()); }); // delete the other files deleteStragglers(p2asmaven, files, AsMavenGroupImpl.SUBDIR_P2, AsMavenGroupImpl.SUBDIR_P2_RUNNABLE, AsMavenGroupImpl.SUBDIR_MAVEN); }
@Override public void finalize() { // If we're taking the "default value" route, Errors has you covered Food logged = Errors.log().getWithDefault(() -> cook("spaghetti"), CEREAL); // log to twitter Food suppressed = Errors.suppress().getWithDefault(() -> cook("spaghetti"), CEREAL); // suppress to insecurity buffer // If we're taking the "rethrow RuntimeException" route, then specifying a // default value would be nonsensical, so we don't do it Food rethrow = Errors.rethrow().get(() -> cook("spaghetti")); // I'm stressed, don't judge me Errors.suppress().run(() -> { eat(logged); eat(suppressed); eat(rethrow); }); }
/** Creates a PrintStream of the given charset, which passes its content to this StringPrinter. */ public PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }
/** * Creates a `PrintStream` of the given charset, which passes its content to this `StringPrinter`. * * Note that `PrintStream.println` uses the system newline, while `StringPrinter.println` always uses `\n`. */ public PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }
/** Loads an instance of the colors. */ public static XkcdColors load() { URL url = XkcdColors.class.getResource("/xkcd_colors.txt"); return Errors.rethrow().get(() -> new XkcdColors(url)); }
private static Map<String, String> parseFromFile(File artifactsJar) throws IOException { Box.Nullable<Map<String, String>> value = Box.Nullable.ofNull(); ZipMisc.read(artifactsJar, "artifacts.xml", input -> { value.set(Errors.rethrow().get(() -> parse(input))); }); return Objects.requireNonNull(value.get()); } }
/** Creates a PrintStream of the given charset, which passes its content to this StringPrinter. */ public PrintStream toPrintStream(Charset charset) { return Errors.rethrow().get(() -> { return new PrintStream(toOutputStream(charset), true, charset.name()); }); }