PluginLoader implements IPluginLoader<Collection<IPath>> { @Override public void loadPlugin(final Collection<IPath> files) throws PluginLoaderException { try { for (IPath file : files) { URL url = new URL("jar:file:" + file.getPath() + "!/"); this.classLoader.addUrl(url); } } catch (Throwable e) { throw new PluginLoaderException("Malformed file name.", e); } JarFile jarFile = null; String pathToJar = null; for (IPath file : files) { try { pathToJar = file.getPath(); jarFile = new JarFile(pathToJar); Enumeration<JarEntry> iterator = jarFile.entries(); while (iterator.hasMoreElements()) { JarEntry je = iterator.nextElement(); if (je.isDirectory() || !je.getName().endsWith(CLASS_EXTENSION)) { continue; } String className = je.getName().substring(0, je.getName().length() - CLASS_EXTENSION.length()); className = className.replace('/', '.'); Class clazz; try { clazz = classLoader.loadClass(className); } catch (Throwable e) { continue; } if (IPlugin.class.isAssignableFrom(clazz) && clazz != IPlugin.class) { creator.execute(clazz); } } } catch (Throwable e) { visitor.pluginLoadingFail(pathToJar, e); throw new PluginLoaderException("Plugin loading failed: " + pathToJar, e); } finally { if (null != jarFile) { try { jarFile.close(); } catch (IOException e) { throw new PluginLoaderException("Error on close instance of JarFile .", e); } } } } } PluginLoader(final ClassLoader classLoader, final IAction<Class> action, final IPluginLoaderVisitor<String> visitor); @Override void loadPlugin(final Collection<IPath> files);  }
@Test public void checkPluginLoaderCreation() throws Exception { Checker checker = new Checker(); ExpansibleURLClassLoader cl = new ExpansibleURLClassLoader(new URL[]{}); IPluginLoaderVisitor<String> visitor = mock(IPluginLoaderVisitor.class); IPluginLoader<Collection<IPath>> pl = new PluginLoader( cl, (t) -> { try { checker.wasCalled = true; } catch (Exception e) { throw new RuntimeException("Could not create instance of IPlugin"); } }, visitor); URL url = this.getClass().getClassLoader().getResource("test_jar_package.jar"); if (null == url) { fail(); } Collection<IPath> files = new ArrayList<IPath>(){{add(new Path(url.getPath()));}}; pl.loadPlugin(files); assertTrue(checker.wasCalled); }
@Test (expected = PluginLoaderException.class) public void checkPluginLoaderException() throws Exception { ExpansibleURLClassLoader cl = new ExpansibleURLClassLoader(new URL[]{}); IPluginLoaderVisitor<String> visitor = mock(IPluginLoaderVisitor.class); IPluginLoader<Collection<IPath>> pl = new PluginLoader( cl, (t) -> { throw new RuntimeException("Could not create instance of IPlugin"); }, visitor); pl.loadPlugin(null); fail(); }
@Test (expected = PluginLoaderException.class) public void checkPluginLoaderOnLoadBrokenJarFile() throws Exception { Checker checker = new Checker(); ExpansibleURLClassLoader cl = new ExpansibleURLClassLoader(new URL[]{}); IPluginLoaderVisitor<String> visitor = mock(IPluginLoaderVisitor.class); IPluginLoader<Collection<IPath>> pl = new PluginLoader( cl, (t) -> { try { checker.wasCalled = true; } catch (Exception e) { throw new RuntimeException("Could not create instance of IPlugin"); } }, visitor); URL url = this.getClass().getClassLoader().getResource("broken.jar"); if (null == url) { fail(); } Collection<IPath> files = new ArrayList<IPath>(){{add(new Path(url.getPath()));}}; pl.loadPlugin(files); assertTrue(checker.wasCalled); fail(); }