OpearFile implements Opear { @Override public ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent) { final URL[] urls = Stream.of(metadata.getPlanClassPath()) .map(name -> new File(cacheDir, name)).flatMap(file -> { if (file.isDirectory() || file.getName().endsWith(".jar")) { return compose1(File::toURI, result1(URI::toURL)).apply(file).stream(); } else { return Stream.empty(); } }) .toArray(URL[]::new); if (urls.length > 0) { return new URLClassLoader(urls, parent); } else { return parent; } } 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 testGetPlanClassLoader_empty() throws Exception { final File cacheDir = new File("target/test-output/OpearFileTest/testGetPlanClassLoader_empty/cache"); if (cacheDir.exists()) { FileUtils.deleteDirectory(cacheDir); } OpearFile opearFile = new OpearFile(cacheDir, new OpearFile.OpearMetadata(new String[0], new String[0], true)); final ClassLoader parent = new URLClassLoader(new URL[0], null); assertSame("same classloader with empty classpath", parent, opearFile.getPlanClassLoader(parent)); }
@Test public void testGetPlanClassLoader() throws Exception { final File cacheDir = new File("target/test-output/OpearFileTest/testGetPlanClassLoader/cache"); if (cacheDir.exists()) { FileUtils.deleteDirectory(cacheDir); } buildDeepTestJar(); final Result<OpearFile> opearResult = OpearFile.fromJar(new JarFile(deepTestTarget), cacheDir); assertTrue("is successful", opearResult.isSuccess()); OpearFile opearFile = opearResult.getOrDefault(null); assertNotNull("not null", opearFile); final String checklistName = "OAKPAL-INF/checklists/embedded-checklist.json"; final ClassLoader controlCl = new URLClassLoader(new URL[]{embedModuleTarget.toURI().toURL()}, null); assertNotNull("control checklist URL not null", controlCl.getResource(checklistName)); final ClassLoader classLoader = opearFile.getPlanClassLoader(new URLClassLoader(new URL[0], null)); final URL embeddedChecklistUrl = classLoader.getResource(checklistName); assertNotNull("checklist URL not null: " + printClassLoader(classLoader), embeddedChecklistUrl); }