@Test public void testUnpackNarsFromNonExistantDir() { final File nonExistantDir = new File("./target/this/dir/should/not/exist/"); nonExistantDir.delete(); nonExistantDir.deleteOnExit(); final Map<String, String> others = new HashMap<>(); others.put("nifi.nar.library.directory.alt", nonExistantDir.toString()); NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others); final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties); assertTrue(extensionMapping.getAllExtensionNames().contains( "org.apache.nifi.processors.dummy.one")); assertEquals(1, extensionMapping.getAllExtensionNames().size()); final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory(); File[] extensionFiles = extensionsWorkingDir.listFiles(); assertEquals(1, extensionFiles.length); assertEquals("dummy-one.nar-unpacked", extensionFiles[0].getName()); }
public static ExtensionMapping unpackNars(final NiFiProperties props) { final List<Path> narLibraryDirs = props.getNarLibraryDirectories(); final File frameworkWorkingDir = props.getFrameworkWorkingDirectory(); final File extensionsWorkingDir = props.getExtensionsWorkingDirectory(); final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory(); try { File unpackedFramework = null; final Set<File> unpackedExtensions = new HashSet<>(); final List<File> narFiles = new ArrayList<>(); FileUtils.ensureDirectoryExistAndCanReadAndWrite(frameworkWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir); for (Path narLibraryDir : narLibraryDirs) { File narDir = narLibraryDir.toFile(); FileUtils.ensureDirectoryExistAndCanRead(narDir); File[] dirFiles = narDir.listFiles(NAR_FILTER); if (dirFiles != null) { List<File> fileList = Arrays.asList(dirFiles); narFiles.addAll(fileList); } } if (!narFiles.isEmpty()) { final long startTime = System.nanoTime(); logger.info("Expanding " + narFiles.size() + " NAR files with all processors..."); for (File narFile : narFiles) { logger.debug("Expanding NAR file: " + narFile.getAbsolutePath()); try (final JarFile nar = new JarFile(narFile)) { final Manifest manifest = nar.getManifest(); final Attributes attributes = manifest.getMainAttributes(); final String narId = attributes.getValue("Nar-Id"); if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { if (unpackedFramework != null) { throw new IllegalStateException( "Multiple framework NARs discovered. Only one framework is permitted."); } unpackedFramework = unpackNar(narFile, frameworkWorkingDir); } else { unpackedExtensions.add(unpackNar(narFile, extensionsWorkingDir)); } } } if (unpackedFramework == null) { throw new IllegalStateException("No framework NAR found."); } else if (!unpackedFramework.canRead()) { throw new IllegalStateException("Framework NAR cannot be read."); } final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles(); if (frameworkWorkingDirContents != null) { for (final File unpackedNar : frameworkWorkingDirContents) { if (!unpackedFramework.equals(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles(); if (extensionsWorkingDirContents != null) { for (final File unpackedNar : extensionsWorkingDirContents) { if (!unpackedExtensions.contains(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final long endTime = System.nanoTime(); logger.info("NAR loading process took " + (endTime - startTime) + " nanoseconds."); } final File[] docsFiles = docsWorkingDir.listFiles(); if (docsFiles != null) { for (final File file : docsFiles) { FileUtils.deleteFile(file, true); } } final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(extensionsWorkingDir, docsWorkingDir, extensionMapping); return extensionMapping; } catch (IOException e) { logger.warn("Unable to load NAR library bundles due to " + e + " Will proceed without loading any further Nar bundles"); if (logger.isDebugEnabled()) { logger.warn("", e); } } return null; }
NarUnpacker { public static ExtensionMapping unpackNars(final NiFiProperties props) { final List<Path> narLibraryDirs = props.getNarLibraryDirectories(); final File frameworkWorkingDir = props.getFrameworkWorkingDirectory(); final File extensionsWorkingDir = props.getExtensionsWorkingDirectory(); final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory(); try { File unpackedFramework = null; final Set<File> unpackedExtensions = new HashSet<>(); final List<File> narFiles = new ArrayList<>(); FileUtils.ensureDirectoryExistAndCanReadAndWrite(frameworkWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir); for (Path narLibraryDir : narLibraryDirs) { File narDir = narLibraryDir.toFile(); FileUtils.ensureDirectoryExistAndCanRead(narDir); File[] dirFiles = narDir.listFiles(NAR_FILTER); if (dirFiles != null) { List<File> fileList = Arrays.asList(dirFiles); narFiles.addAll(fileList); } } if (!narFiles.isEmpty()) { final long startTime = System.nanoTime(); logger.info("Expanding " + narFiles.size() + " NAR files with all processors..."); for (File narFile : narFiles) { logger.debug("Expanding NAR file: " + narFile.getAbsolutePath()); try (final JarFile nar = new JarFile(narFile)) { final Manifest manifest = nar.getManifest(); final Attributes attributes = manifest.getMainAttributes(); final String narId = attributes.getValue("Nar-Id"); if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { if (unpackedFramework != null) { throw new IllegalStateException( "Multiple framework NARs discovered. Only one framework is permitted."); } unpackedFramework = unpackNar(narFile, frameworkWorkingDir); } else { unpackedExtensions.add(unpackNar(narFile, extensionsWorkingDir)); } } } if (unpackedFramework == null) { throw new IllegalStateException("No framework NAR found."); } else if (!unpackedFramework.canRead()) { throw new IllegalStateException("Framework NAR cannot be read."); } final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles(); if (frameworkWorkingDirContents != null) { for (final File unpackedNar : frameworkWorkingDirContents) { if (!unpackedFramework.equals(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles(); if (extensionsWorkingDirContents != null) { for (final File unpackedNar : extensionsWorkingDirContents) { if (!unpackedExtensions.contains(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final long endTime = System.nanoTime(); logger.info("NAR loading process took " + (endTime - startTime) + " nanoseconds."); } final File[] docsFiles = docsWorkingDir.listFiles(); if (docsFiles != null) { for (final File file : docsFiles) { FileUtils.deleteFile(file, true); } } final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(extensionsWorkingDir, docsWorkingDir, extensionMapping); return extensionMapping; } catch (IOException e) { logger.warn("Unable to load NAR library bundles due to " + e + " Will proceed without loading any further Nar bundles"); if (logger.isDebugEnabled()) { logger.warn("", e); } } return null; } }
NarUnpacker { public static ExtensionMapping unpackNars(final NiFiProperties props) { final List<Path> narLibraryDirs = props.getNarLibraryDirectories(); final File frameworkWorkingDir = props.getFrameworkWorkingDirectory(); final File extensionsWorkingDir = props.getExtensionsWorkingDirectory(); final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory(); try { File unpackedFramework = null; final Set<File> unpackedExtensions = new HashSet<>(); final List<File> narFiles = new ArrayList<>(); FileUtils.ensureDirectoryExistAndCanReadAndWrite(frameworkWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir); for (Path narLibraryDir : narLibraryDirs) { File narDir = narLibraryDir.toFile(); FileUtils.ensureDirectoryExistAndCanRead(narDir); File[] dirFiles = narDir.listFiles(NAR_FILTER); if (dirFiles != null) { List<File> fileList = Arrays.asList(dirFiles); narFiles.addAll(fileList); } } if (!narFiles.isEmpty()) { final long startTime = System.nanoTime(); logger.info("Expanding " + narFiles.size() + " NAR files with all processors..."); for (File narFile : narFiles) { logger.debug("Expanding NAR file: " + narFile.getAbsolutePath()); try (final JarFile nar = new JarFile(narFile)) { final Manifest manifest = nar.getManifest(); final Attributes attributes = manifest.getMainAttributes(); final String narId = attributes.getValue("Nar-Id"); if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { if (unpackedFramework != null) { throw new IllegalStateException( "Multiple framework NARs discovered. Only one framework is permitted."); } unpackedFramework = unpackNar(narFile, frameworkWorkingDir); } else { unpackedExtensions.add(unpackNar(narFile, extensionsWorkingDir)); } } } if (unpackedFramework == null) { throw new IllegalStateException("No framework NAR found."); } else if (!unpackedFramework.canRead()) { throw new IllegalStateException("Framework NAR cannot be read."); } final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles(); if (frameworkWorkingDirContents != null) { for (final File unpackedNar : frameworkWorkingDirContents) { if (!unpackedFramework.equals(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles(); if (extensionsWorkingDirContents != null) { for (final File unpackedNar : extensionsWorkingDirContents) { if (!unpackedExtensions.contains(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final long endTime = System.nanoTime(); logger.info("NAR loading process took " + (endTime - startTime) + " nanoseconds."); } final File[] docsFiles = docsWorkingDir.listFiles(); if (docsFiles != null) { for (final File file : docsFiles) { FileUtils.deleteFile(file, true); } } final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(extensionsWorkingDir, docsWorkingDir, extensionMapping); return extensionMapping; } catch (IOException e) { logger.warn("Unable to load NAR library bundles due to " + e + " Will proceed without loading any further Nar bundles"); if (logger.isDebugEnabled()) { logger.warn("", e); } } return null; } private  NarUnpacker(); }
NarUnpacker { public static ExtensionMapping unpackNars(final NiFiProperties props) { final List<Path> narLibraryDirs = props.getNarLibraryDirectories(); final File frameworkWorkingDir = props.getFrameworkWorkingDirectory(); final File extensionsWorkingDir = props.getExtensionsWorkingDirectory(); final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory(); try { File unpackedFramework = null; final Set<File> unpackedExtensions = new HashSet<>(); final List<File> narFiles = new ArrayList<>(); FileUtils.ensureDirectoryExistAndCanReadAndWrite(frameworkWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir); for (Path narLibraryDir : narLibraryDirs) { File narDir = narLibraryDir.toFile(); FileUtils.ensureDirectoryExistAndCanRead(narDir); File[] dirFiles = narDir.listFiles(NAR_FILTER); if (dirFiles != null) { List<File> fileList = Arrays.asList(dirFiles); narFiles.addAll(fileList); } } if (!narFiles.isEmpty()) { final long startTime = System.nanoTime(); logger.info("Expanding " + narFiles.size() + " NAR files with all processors..."); for (File narFile : narFiles) { logger.debug("Expanding NAR file: " + narFile.getAbsolutePath()); try (final JarFile nar = new JarFile(narFile)) { final Manifest manifest = nar.getManifest(); final Attributes attributes = manifest.getMainAttributes(); final String narId = attributes.getValue("Nar-Id"); if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { if (unpackedFramework != null) { throw new IllegalStateException( "Multiple framework NARs discovered. Only one framework is permitted."); } unpackedFramework = unpackNar(narFile, frameworkWorkingDir); } else { unpackedExtensions.add(unpackNar(narFile, extensionsWorkingDir)); } } } if (unpackedFramework == null) { throw new IllegalStateException("No framework NAR found."); } else if (!unpackedFramework.canRead()) { throw new IllegalStateException("Framework NAR cannot be read."); } final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles(); if (frameworkWorkingDirContents != null) { for (final File unpackedNar : frameworkWorkingDirContents) { if (!unpackedFramework.equals(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles(); if (extensionsWorkingDirContents != null) { for (final File unpackedNar : extensionsWorkingDirContents) { if (!unpackedExtensions.contains(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final long endTime = System.nanoTime(); logger.info("NAR loading process took " + (endTime - startTime) + " nanoseconds."); } final File[] docsFiles = docsWorkingDir.listFiles(); if (docsFiles != null) { for (final File file : docsFiles) { FileUtils.deleteFile(file, true); } } final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(extensionsWorkingDir, docsWorkingDir, extensionMapping); return extensionMapping; } catch (IOException e) { logger.warn("Unable to load NAR library bundles due to " + e + " Will proceed without loading any further Nar bundles"); if (logger.isDebugEnabled()) { logger.warn("", e); } } return null; } private  NarUnpacker(); static ExtensionMapping unpackNars(final NiFiProperties props); }
NarUnpacker { public static ExtensionMapping unpackNars(final NiFiProperties props) { final List<Path> narLibraryDirs = props.getNarLibraryDirectories(); final File frameworkWorkingDir = props.getFrameworkWorkingDirectory(); final File extensionsWorkingDir = props.getExtensionsWorkingDirectory(); final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory(); try { File unpackedFramework = null; final Set<File> unpackedExtensions = new HashSet<>(); final List<File> narFiles = new ArrayList<>(); FileUtils.ensureDirectoryExistAndCanReadAndWrite(frameworkWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir); FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir); for (Path narLibraryDir : narLibraryDirs) { File narDir = narLibraryDir.toFile(); FileUtils.ensureDirectoryExistAndCanRead(narDir); File[] dirFiles = narDir.listFiles(NAR_FILTER); if (dirFiles != null) { List<File> fileList = Arrays.asList(dirFiles); narFiles.addAll(fileList); } } if (!narFiles.isEmpty()) { final long startTime = System.nanoTime(); logger.info("Expanding " + narFiles.size() + " NAR files with all processors..."); for (File narFile : narFiles) { logger.debug("Expanding NAR file: " + narFile.getAbsolutePath()); try (final JarFile nar = new JarFile(narFile)) { final Manifest manifest = nar.getManifest(); final Attributes attributes = manifest.getMainAttributes(); final String narId = attributes.getValue("Nar-Id"); if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) { if (unpackedFramework != null) { throw new IllegalStateException( "Multiple framework NARs discovered. Only one framework is permitted."); } unpackedFramework = unpackNar(narFile, frameworkWorkingDir); } else { unpackedExtensions.add(unpackNar(narFile, extensionsWorkingDir)); } } } if (unpackedFramework == null) { throw new IllegalStateException("No framework NAR found."); } else if (!unpackedFramework.canRead()) { throw new IllegalStateException("Framework NAR cannot be read."); } final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles(); if (frameworkWorkingDirContents != null) { for (final File unpackedNar : frameworkWorkingDirContents) { if (!unpackedFramework.equals(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles(); if (extensionsWorkingDirContents != null) { for (final File unpackedNar : extensionsWorkingDirContents) { if (!unpackedExtensions.contains(unpackedNar)) { FileUtils.deleteFile(unpackedNar, true); } } } final long endTime = System.nanoTime(); logger.info("NAR loading process took " + (endTime - startTime) + " nanoseconds."); } final File[] docsFiles = docsWorkingDir.listFiles(); if (docsFiles != null) { for (final File file : docsFiles) { FileUtils.deleteFile(file, true); } } final ExtensionMapping extensionMapping = new ExtensionMapping(); mapExtensions(extensionsWorkingDir, docsWorkingDir, extensionMapping); return extensionMapping; } catch (IOException e) { logger.warn("Unable to load NAR library bundles due to " + e + " Will proceed without loading any further Nar bundles"); if (logger.isDebugEnabled()) { logger.warn("", e); } } return null; } private  NarUnpacker(); static ExtensionMapping unpackNars(final NiFiProperties props);  }