phrocker commented on a change in pull request #489: MINIFICPP-740: Add ability 
to run NiFi processors from Java and Python
URL: https://github.com/apache/nifi-minifi-cpp/pull/489#discussion_r261014684
 
 

 ##########
 File path: 
extensions/jni/nifi-framework-jni/src/main/java/org/apache/nifi/nar/JniUnpacker.java
 ##########
 @@ -0,0 +1,398 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.nar;
+
+import org.apache.nifi.bundle.BundleCoordinate;
+import org.apache.nifi.util.FileUtils;
+import org.apache.nifi.util.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+/**
+ *  Should change NarPacker so that we can depend on it a little more for our 
purposes.
+ */
+public class JniUnpacker {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(JniUnpacker.class);
+    private static String HASH_FILENAME = "nar-md5sum";
+
+    private static final Map<String,File> narMaps = new HashMap<>();
+    private static final Map<String,String> narDependencies = new HashMap<>();
+
+    private static final FileFilter NAR_FILTER = new FileFilter() {
+        @Override
+        public boolean accept(File pathname) {
+            final String nameToTest = pathname.getName().toLowerCase();
+            return nameToTest.endsWith(".nar") && pathname.isFile();
+        }
+    };
+
+
+    public static Optional<String> getDependency(final String narId){
+        return Optional.of( narDependencies.get(narId) );
+    }
+
+    public static Optional<File> getDirectory(final String narId){
+        return Optional.of( narMaps.get(narId) );
+    }
+
+    public static ExtensionMapping unpackNars(final File narFilesDir, final 
File unpackDirectory, final File docsWorkingDir, List<File> paths ) throws 
IOException {
+        final List<Path> narLibraryDirs = new ArrayList<>();
+        final Map<File, BundleCoordinate> unpackedNars = new HashMap<>();
+
+        final Path narFilesPath = narFilesDir.toPath();
+        narLibraryDirs.add(narFilesPath);
+
+        try {
+            File unpackedFramework = null;
+            final Set<File> unpackedExtensions = new HashSet<>();
+            final List<File> narFiles = new ArrayList<>();
+
+            // make sure the nar directories are there and accessible
+            FileUtils.ensureDirectoryExistAndCanReadAndWrite(unpackDirectory);
+
+            for (Path narLibraryDir : narLibraryDirs) {
+
+                File narDir = narLibraryDir.toFile();
+
+                // Test if the source NARs can be read
+                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() + " to " + unpackDirectory);
+
+                    // get the manifest for this nar
+                    try (final JarFile nar = new JarFile(narFile)) {
+                        logger.debug("Expanding NAR file: " + nar.getName());
+                        final Manifest manifest = nar.getManifest();
+
+                        // lookup the nar id
+                        final Attributes attributes = 
manifest.getMainAttributes();
+                        final String groupId = 
attributes.getValue(NarManifestEntry.NAR_GROUP.getManifestName());
+                        final String narId = 
attributes.getValue(NarManifestEntry.NAR_ID.getManifestName());
+                        final String version = 
attributes.getValue(NarManifestEntry.NAR_VERSION.getManifestName());
+                        final String dependencyId = 
attributes.getValue(NarManifestEntry.NAR_DEPENDENCY_ID.getManifestName());
+                        logger.debug("Expanding NAR file: " + nar.getName() + 
" has dependency " + dependencyId);
+                        narMaps.put(narId,narFilesDir);
+                        narDependencies.put(narId,dependencyId);
+                        // determine if this is the framework
+                        if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) {
+                            if (unpackedFramework != null) {
+                                throw new IllegalStateException("Multiple 
framework NARs discovered. Only one framework is permitted.");
+                            }
+
+                            // unpack the framework nar
+                            unpackedFramework = unpackNar(narFile,paths, 
unpackDirectory);
+                        } else {
+                            final File unpackedExtension = 
unpackNar(narFile,paths, unpackDirectory);
+
+                            // record the current bundle
+                            unpackedNars.put(unpackedExtension, new 
BundleCoordinate(groupId, narId, version));
+
+                            // unpack the extension nar
+                            unpackedExtensions.add(unpackedExtension);
+                        }
+                    }
+                }
+
+                final long duration = System.nanoTime() - startTime;
+                logger.info("NAR loading process took " + duration + " 
nanoseconds "
+                        + "(" + (int) TimeUnit.SECONDS.convert(duration, 
TimeUnit.NANOSECONDS) + " seconds).");
+            }
+
+            // attempt to delete any docs files that exist so that any 
components that have been removed
+            // will no longer have entries in the docs folder
+            final File[] docsFiles = docsWorkingDir.listFiles();
+            if (docsFiles != null) {
+                for (final File file : docsFiles) {
+                    FileUtils.deleteFile(file, true);
+                }
+            }
+
+            final ExtensionMapping extensionMapping = new ExtensionMapping();
+            mapExtensions(unpackedNars, 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 static void mapExtensions(final Map<File, BundleCoordinate> 
unpackedNars, final File docsDirectory, final ExtensionMapping mapping) throws 
IOException {
+        for (final Map.Entry<File, BundleCoordinate> entry : 
unpackedNars.entrySet()) {
+            final File unpackedNar = entry.getKey();
+            final BundleCoordinate bundleCoordinate = entry.getValue();
+
+            final File bundledDependencies = new File(unpackedNar, 
"NAR-INF/bundled-dependencies");
+
+            unpackBundleDocs(docsDirectory, mapping, bundleCoordinate, 
bundledDependencies);
 
 Review comment:
   nm i don't think i could really get all the information I need without 
loading processors anyway. good point re the docs. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to