@Test public void testExtractViewArchive() throws Exception { ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity(); resourceTypeEntity.setId(10); resourceTypeEntity.setName("MY_VIEW{1.0.0}"); ViewEntity viewDefinition = ViewEntityTest.getViewEntity(); viewDefinition.setResourceType(resourceTypeEntity); expect(configuration.getViewExtractionThreadPoolCoreSize()).andReturn(2).anyTimes(); expect(configuration.getViewExtractionThreadPoolMaxSize()).andReturn(3).anyTimes(); expect(configuration.getViewExtractionThreadPoolTimeout()).andReturn(10000L).anyTimes(); if (System.getProperty("os.name").contains("Windows")) { expect(viewArchive.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes(); } else { expect(viewArchive.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes(); } expect(archiveDir.exists()).andReturn(false); if (System.getProperty("os.name").contains("Windows")) { expect(archiveDir.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes(); } else { expect(archiveDir.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes(); } expect(archiveDir.mkdir()).andReturn(true); expect(archiveDir.toURI()).andReturn(new URI("file:./")); expect(metaInfDir.mkdir()).andReturn(true); expect(viewJarFile.getNextJarEntry()).andReturn(jarEntry); expect(viewJarFile.getNextJarEntry()).andReturn(null); expect(jarEntry.getName()).andReturn("view.xml"); expect(viewJarFile.read(anyObject(byte[].class))).andReturn(10); expect(viewJarFile.read(anyObject(byte[].class))).andReturn(-1); fos.write(anyObject(byte[].class), eq(0), eq(10)); fos.flush(); fos.close(); viewJarFile.closeEntry(); viewJarFile.close(); expect(classesDir.exists()).andReturn(true); expect(classesDir.toURI()).andReturn(new URI("file:./")); expect(libDir.exists()).andReturn(true); expect(libDir.listFiles()).andReturn(new File[]{fileEntry}); expect(fileEntry.toURI()).andReturn(new URI("file:./")); replay(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir, viewJarFile, jarEntry, fos, configuration, viewDir, fileEntry, viewDAO); ViewExtractor viewExtractor = getViewExtractor(viewDefinition); viewExtractor.extractViewArchive(viewDefinition, viewArchive, archiveDir); verify(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir, viewJarFile, jarEntry, fos, configuration, viewDir, fileEntry, viewDAO); }
public ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir) throws ExtractionException { String archivePath = archiveDir.getAbsolutePath(); try { if (archiveDir.exists() && viewArchive != null && viewArchive.lastModified() > archiveDir.lastModified()) { FileUtils.deleteDirectory(archiveDir); } if (!archiveDir.exists()) { String msg = "Creating archive folder " + archivePath + "."; view.setStatusDetail(msg); LOG.info(msg); if (archiveDir.mkdir()) { JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive); try { msg = "Extracting files from " + viewArchive.getName() + "."; view.setStatusDetail(msg); LOG.info(msg); File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF"); if (!metaInfDir.mkdir()) { msg = "Could not create archive META-INF directory."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry())!= null){ try { String entryPath = archivePath + File.separator + jarEntry.getName(); LOG.debug("Extracting " + entryPath); File entryFile = archiveUtility.getFile(entryPath); if (jarEntry.isDirectory()) { LOG.debug("Making directory " + entryPath); if (!entryFile.mkdir()) { msg = "Could not create archive entry directory " + entryPath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } else { FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile); try { LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath); byte[] buffer = new byte[BUFFER_SIZE]; int n; while((n = jarInputStream.read(buffer)) > -1) { fos.write(buffer, 0, n); } LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath); } finally { fos.flush(); fos.close(); } } } finally { jarInputStream.closeEntry(); } } } finally { jarInputStream.close(); } } else { msg = "Could not create archive directory " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(archivePath, false); return getArchiveClassLoader(viewConfig, archiveDir); } catch (Exception e) { String msg = "Caught exception trying to extract the view archive " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg, e); throw new ExtractionException(msg, e); } }
ViewExtractor { public ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir) throws ExtractionException { String archivePath = archiveDir.getAbsolutePath(); try { if (archiveDir.exists() && viewArchive != null && viewArchive.lastModified() > archiveDir.lastModified()) { FileUtils.deleteDirectory(archiveDir); } if (!archiveDir.exists()) { String msg = "Creating archive folder " + archivePath + "."; view.setStatusDetail(msg); LOG.info(msg); if (archiveDir.mkdir()) { JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive); try { msg = "Extracting files from " + viewArchive.getName() + "."; view.setStatusDetail(msg); LOG.info(msg); File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF"); if (!metaInfDir.mkdir()) { msg = "Could not create archive META-INF directory."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry())!= null){ try { String entryPath = archivePath + File.separator + jarEntry.getName(); LOG.debug("Extracting " + entryPath); File entryFile = archiveUtility.getFile(entryPath); if (jarEntry.isDirectory()) { LOG.debug("Making directory " + entryPath); if (!entryFile.mkdir()) { msg = "Could not create archive entry directory " + entryPath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } else { FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile); try { LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath); byte[] buffer = new byte[BUFFER_SIZE]; int n; while((n = jarInputStream.read(buffer)) > -1) { fos.write(buffer, 0, n); } LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath); } finally { fos.flush(); fos.close(); } } } finally { jarInputStream.closeEntry(); } } } finally { jarInputStream.close(); } } else { msg = "Could not create archive directory " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(archivePath, false); return getArchiveClassLoader(viewConfig, archiveDir); } catch (Exception e) { String msg = "Caught exception trying to extract the view archive " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg, e); throw new ExtractionException(msg, e); } } }
ViewExtractor { public ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir) throws ExtractionException { String archivePath = archiveDir.getAbsolutePath(); try { if (archiveDir.exists() && viewArchive != null && viewArchive.lastModified() > archiveDir.lastModified()) { FileUtils.deleteDirectory(archiveDir); } if (!archiveDir.exists()) { String msg = "Creating archive folder " + archivePath + "."; view.setStatusDetail(msg); LOG.info(msg); if (archiveDir.mkdir()) { JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive); try { msg = "Extracting files from " + viewArchive.getName() + "."; view.setStatusDetail(msg); LOG.info(msg); File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF"); if (!metaInfDir.mkdir()) { msg = "Could not create archive META-INF directory."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry())!= null){ try { String entryPath = archivePath + File.separator + jarEntry.getName(); LOG.debug("Extracting " + entryPath); File entryFile = archiveUtility.getFile(entryPath); if (jarEntry.isDirectory()) { LOG.debug("Making directory " + entryPath); if (!entryFile.mkdir()) { msg = "Could not create archive entry directory " + entryPath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } else { FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile); try { LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath); byte[] buffer = new byte[BUFFER_SIZE]; int n; while((n = jarInputStream.read(buffer)) > -1) { fos.write(buffer, 0, n); } LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath); } finally { fos.flush(); fos.close(); } } } finally { jarInputStream.closeEntry(); } } } finally { jarInputStream.close(); } } else { msg = "Could not create archive directory " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(archivePath, false); return getArchiveClassLoader(viewConfig, archiveDir); } catch (Exception e) { String msg = "Caught exception trying to extract the view archive " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg, e); throw new ExtractionException(msg, e); } }  }
ViewExtractor { public ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir) throws ExtractionException { String archivePath = archiveDir.getAbsolutePath(); try { if (archiveDir.exists() && viewArchive != null && viewArchive.lastModified() > archiveDir.lastModified()) { FileUtils.deleteDirectory(archiveDir); } if (!archiveDir.exists()) { String msg = "Creating archive folder " + archivePath + "."; view.setStatusDetail(msg); LOG.info(msg); if (archiveDir.mkdir()) { JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive); try { msg = "Extracting files from " + viewArchive.getName() + "."; view.setStatusDetail(msg); LOG.info(msg); File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF"); if (!metaInfDir.mkdir()) { msg = "Could not create archive META-INF directory."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry())!= null){ try { String entryPath = archivePath + File.separator + jarEntry.getName(); LOG.debug("Extracting " + entryPath); File entryFile = archiveUtility.getFile(entryPath); if (jarEntry.isDirectory()) { LOG.debug("Making directory " + entryPath); if (!entryFile.mkdir()) { msg = "Could not create archive entry directory " + entryPath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } else { FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile); try { LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath); byte[] buffer = new byte[BUFFER_SIZE]; int n; while((n = jarInputStream.read(buffer)) > -1) { fos.write(buffer, 0, n); } LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath); } finally { fos.flush(); fos.close(); } } } finally { jarInputStream.closeEntry(); } } } finally { jarInputStream.close(); } } else { msg = "Could not create archive directory " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(archivePath, false); return getArchiveClassLoader(viewConfig, archiveDir); } catch (Exception e) { String msg = "Caught exception trying to extract the view archive " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg, e); throw new ExtractionException(msg, e); } }  ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir); boolean ensureExtractedArchiveDirectory(String extractedArchivesPath); }
ViewExtractor { public ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir) throws ExtractionException { String archivePath = archiveDir.getAbsolutePath(); try { if (archiveDir.exists() && viewArchive != null && viewArchive.lastModified() > archiveDir.lastModified()) { FileUtils.deleteDirectory(archiveDir); } if (!archiveDir.exists()) { String msg = "Creating archive folder " + archivePath + "."; view.setStatusDetail(msg); LOG.info(msg); if (archiveDir.mkdir()) { JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive); try { msg = "Extracting files from " + viewArchive.getName() + "."; view.setStatusDetail(msg); LOG.info(msg); File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF"); if (!metaInfDir.mkdir()) { msg = "Could not create archive META-INF directory."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry())!= null){ try { String entryPath = archivePath + File.separator + jarEntry.getName(); LOG.debug("Extracting " + entryPath); File entryFile = archiveUtility.getFile(entryPath); if (jarEntry.isDirectory()) { LOG.debug("Making directory " + entryPath); if (!entryFile.mkdir()) { msg = "Could not create archive entry directory " + entryPath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } else { FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile); try { LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath); byte[] buffer = new byte[BUFFER_SIZE]; int n; while((n = jarInputStream.read(buffer)) > -1) { fos.write(buffer, 0, n); } LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath); } finally { fos.flush(); fos.close(); } } } finally { jarInputStream.closeEntry(); } } } finally { jarInputStream.close(); } } else { msg = "Could not create archive directory " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg); throw new ExtractionException(msg); } } ViewConfig viewConfig = archiveUtility.getViewConfigFromExtractedArchive(archivePath, false); return getArchiveClassLoader(viewConfig, archiveDir); } catch (Exception e) { String msg = "Caught exception trying to extract the view archive " + archivePath + "."; view.setStatusDetail(msg); LOG.error(msg, e); throw new ExtractionException(msg, e); } }  ClassLoader extractViewArchive(ViewEntity view, File viewArchive, File archiveDir); boolean ensureExtractedArchiveDirectory(String extractedArchivesPath);  }