@Override public ExplodedPlugin explode(PluginInfo info) { try { File dir = unzipFile(info.getNonNullJarFile()); return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), dir); } catch (Exception e) { throw new IllegalStateException(String.format("Fail to open plugin [%s]: %s", info.getKey(), info.getNonNullJarFile().getAbsolutePath()), e); } }
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = pluginFiles.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }
@Before public void setUp() throws IOException { tempDir = temp.newFolder(); PluginFiles pluginFiles = mock(PluginFiles.class); when(pluginFiles.createTempDir()).thenReturn(tempDir); underTest = new ScannerPluginJarExploder(pluginFiles); }
@Test public void extract_only_libs() throws IOException { File jar = loadFile("sonar-checkstyle-plugin-2.8.jar"); underTest.explode(PluginInfo.create(jar)); assertThat(new File(jar.getParent(), "sonar-checkstyle-plugin-2.8.jar")).exists(); assertThat(new File(jar.getParent(), "sonar-checkstyle-plugin-2.8.jar_unzip/META-INF/MANIFEST.MF")).doesNotExist(); assertThat(new File(jar.getParent(), "sonar-checkstyle-plugin-2.8.jar_unzip/org/sonar/plugins/checkstyle/CheckstyleVersion.class")).doesNotExist(); }
@Test public void copy_and_extract_libs() throws IOException { File jar = loadFile("sonar-checkstyle-plugin-2.8.jar"); ExplodedPlugin exploded = underTest.explode(PluginInfo.create(jar)); assertThat(exploded.getKey()).isEqualTo("checkstyle"); assertThat(exploded.getMain()).isFile().exists(); assertThat(exploded.getLibs()).extracting(File::getName).containsExactlyInAnyOrder("antlr-2.7.6.jar", "checkstyle-5.1.jar", "commons-cli-1.0.jar"); assertThat(new File(jar.getParent(), "sonar-checkstyle-plugin-2.8.jar")).exists(); assertThat(new File(jar.getParent(), "sonar-checkstyle-plugin-2.8.jar_unzip/META-INF/lib/checkstyle-5.1.jar")).exists(); }
@Override public ExplodedPlugin explode(PluginInfo info) { try { File dir = unzipFile(info.getNonNullJarFile()); return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), dir); } catch (Exception e) { throw new IllegalStateException(String.format("Fail to open plugin [%s]: %s", info.getKey(), info.getNonNullJarFile().getAbsolutePath()), e); } }
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = pluginFiles.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }