OpearFile implements Opear { static Result<File> cacheJar(final @NotNull JarFile jarFile, final @NotNull File cacheDir) { return jarFile.stream() .map(entry -> { final File cacheFile = new File(cacheDir, entry.getName()); if (entry.isDirectory()) { if (cacheFile.isDirectory() || cacheFile.mkdirs()) { return Result.success(cacheFile); } } else { if (cacheFile.getParentFile().isDirectory() || cacheFile.getParentFile().mkdirs()) { try (InputStream input = jarFile.getInputStream(entry)) { FileIOUtils.copyInputStreamToFile(input, cacheFile); return Result.success(cacheFile); } catch (IOException e) { return Result.<File>failure("failed to cache entry " + entry.getName(), e); } } } return Result.<File>failure("failed to cache entry " + entry.getName()); }) .filter(Result::isFailure).findFirst() .map(failed -> failed.getError().map(Result::<File>failure) .orElseGet(() -> Result.<File>failure("failed to cache jarFile" + jarFile.getName()))) .orElse(Result.success(cacheDir)); } OpearFile(final File cacheDir, final OpearMetadata metadata); @Override URL getDefaultPlan(); @Override Result<URL> getSpecificPlan(final @NotNull String planName); @Override ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent); static Result<OpearFile> fromDirectory(final @NotNull File directory); static Result<OpearFile> fromJar(final @NotNull JarFile jarFile, final @NotNull File cacheBaseDir); static final Attributes.Name NAME_BUNDLE_SYMBOLICNAME; static final Attributes.Name NAME_BUNDLE_VERSION; static final Attributes.Name NAME_CLASS_PATH; static final Attributes.Name NAME_OAKPAL_VERSION; static final Attributes.Name NAME_OAKPAL_PLAN; }
@Test public void testCacheJar_fail() throws Exception { buildDeepTestJar(); final File cacheDir = new File("target/test-output/OpearFileTest/testCacheJar_fail/cache"); if (cacheDir.exists()) { FileUtils.deleteDirectory(cacheDir); } new File(cacheDir, "deep-plan.json").mkdirs(); assertTrue("fail to cache entry", OpearFile.cacheJar(new JarFile(deepTestTarget), cacheDir).isFailure()); FileUtils.deleteDirectory(cacheDir); FileUtils.touch(new File(cacheDir, "META-INF")); assertTrue("fail to cache directory entry", OpearFile.cacheJar(new JarFile(deepTestTarget), cacheDir).isFailure()); }