OpearFile implements Opear { public static Result<OpearFile> fromJar(final @NotNull JarFile jarFile, final @NotNull File cacheBaseDir) { return getHashCacheKey(jarFile.getName()).flatMap(cacheKey -> { return result1(JarFile::getManifest).apply(jarFile) .flatMap(OpearFile::validateOpearManifest) .flatMap(metadata -> { final File cacheDir = new File(cacheBaseDir, cacheKey); if (cacheDir.isDirectory()) { return fromDirectory(cacheDir); } else { if (!cacheDir.mkdirs()) { return Result.failure(format("failed to create cache dir %s for specified opear file %s", cacheDir.getPath(), jarFile.getName())); } else { return cacheJar(jarFile, cacheDir).flatMap(OpearFile::fromDirectory); } } }); }); } 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 testEmptyPlanJar() throws Exception { final File targetDir = new File("target/test-temp"); targetDir.mkdirs(); final File mfJar = new File(targetDir, "emptyplan.jar"); final File mfCache = new File(targetDir, "emptyplan.cache"); if (mfJar.exists()) { mfJar.delete(); } final File mfDir = new File("src/test/resources/plans/xEmpty"); final File mfFile = new File(mfDir, JarFile.MANIFEST_NAME); try (InputStream mfStream = new FileInputStream(mfFile)) { Manifest manifest = new Manifest(mfStream); try (JarOutputStream mfJarOut = new JarOutputStream(new FileOutputStream(mfJar), manifest)) { mfJarOut.putNextEntry(new JarEntry("other-plan.json")); mfJarOut.write("{}".getBytes(StandardCharsets.UTF_8)); } } Result<OpearFile> opearResult = result1((File file) -> new JarFile(file, true)) .apply(mfJar) .flatMap(jar -> OpearFile.fromJar(jar, mfCache)); opearResult.throwCause(Exception.class); assertTrue("opear result should be success ", opearResult.isSuccess()); Result<OakpalPlan> plan = opearResult.map(OpearFile::getDefaultPlan).flatMap(OakpalPlan::fromJson); plan.throwCause(Exception.class); assertTrue("opear plan should be empty", plan.map(OakpalPlan::getChecklists).getOrDefault(Arrays.asList("not a checklist")).isEmpty()); }
@Test public void testFromJar_useCacheDirectory() throws Exception { buildDeepTestJar(); final File cacheDir = new File("target/test-output/OpearFileTest/testFromJar_useCacheDirectory/cache"); if (cacheDir.exists()) { FileUtils.deleteDirectory(cacheDir); } assertTrue("succeed with jar when cache is fresh", OpearFile.fromJar(new JarFile(deepTestTarget), cacheDir).isSuccess()); assertTrue("succeed with cache dir when cache is present", OpearFile.fromJar(new JarFile(deepTestTarget), cacheDir).isSuccess()); }