NIFI-6758: allow pre-unpacked nar's to be recorded in ExtensionMapping

- fixing resource leak

This closes #3806.

Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
Endre Zoltan Kovacs 2019-10-09 17:57:46 +02:00 committed by Bryan Bende
parent ed1e843609
commit 5238dc20b1
No known key found for this signature in database
GPG Key ID: A0DDA9ED50711C39
1 changed files with 37 additions and 12 deletions

View File

@ -24,6 +24,8 @@ import org.apache.nifi.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.String.format;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
@ -34,6 +36,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@ -105,35 +108,26 @@ public final class NarUnpacker {
// get the manifest for this nar
try (final JarFile nar = new JarFile(narFile)) {
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());
BundleCoordinate bundleCoordinate = createBundleCoordinate(nar.getManifest());
// determine if this is the framework
if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(narId)) {
if (NarClassLoaders.FRAMEWORK_NAR_ID.equals(bundleCoordinate.getId())) {
if (unpackedFramework != null) {
throw new IllegalStateException("Multiple framework NARs discovered. Only one framework is permitted.");
}
// unpack the framework nar
unpackedFramework = unpackNar(narFile, frameworkWorkingDir);
} else if (NarClassLoaders.JETTY_NAR_ID.equals(narId)) {
} else if (NarClassLoaders.JETTY_NAR_ID.equals(bundleCoordinate.getId())) {
if (unpackedJetty != null) {
throw new IllegalStateException("Multiple Jetty NARs discovered. Only one Jetty NAR is permitted.");
}
// unpack and record the Jetty nar
unpackedJetty = unpackNar(narFile, extensionsWorkingDir);
unpackedNars.put(unpackedJetty, new BundleCoordinate(groupId, narId, version));
unpackedExtensions.add(unpackedJetty);
} else {
// unpack and record the extension nar
final File unpackedExtension = unpackNar(narFile, extensionsWorkingDir);
unpackedNars.put(unpackedExtension, new BundleCoordinate(groupId, narId, version));
unpackedExtensions.add(unpackedExtension);
}
}
@ -188,6 +182,7 @@ public final class NarUnpacker {
}
}
unpackedNars.putAll(createUnpackedNarBundleCoordinateMap(extensionsWorkingDir));
final ExtensionMapping extensionMapping = new ExtensionMapping();
mapExtensions(unpackedNars, docsWorkingDir, extensionMapping);
@ -205,6 +200,36 @@ public final class NarUnpacker {
return null;
}
/**
* Creates a map containing the nar directory mapped to it's bundle-coordinate.
* @param extensionsWorkingDir
* @return
*/
private static Map<File, BundleCoordinate> createUnpackedNarBundleCoordinateMap(File extensionsWorkingDir) {
Map<File, BundleCoordinate> result = new HashMap<>();
File[] unpackedDirs = extensionsWorkingDir.listFiles(file -> file.isDirectory() && file.getName().endsWith("nar-unpacked"));
for (File unpackedDir : unpackedDirs) {
Path mf = Paths.get(unpackedDir.getAbsolutePath(), "META-INF", "MANIFEST.MF");
try(InputStream is = Files.newInputStream(mf)) {
Manifest manifest = new Manifest(is);
BundleCoordinate bundleCoordinate = createBundleCoordinate(manifest);
result.put(unpackedDir, bundleCoordinate);
} catch (IOException e) {
logger.error(format("Unable to parse NAR information from unpacked nar directory [%s].", unpackedDir.getAbsoluteFile()), e);
}
}
return result;
}
private static BundleCoordinate createBundleCoordinate(Manifest manifest) {
Attributes mainAttributes = manifest.getMainAttributes();
String groupId = mainAttributes.getValue(NarManifestEntry.NAR_GROUP.getManifestName());
String narId = mainAttributes.getValue(NarManifestEntry.NAR_ID.getManifestName());
String version = mainAttributes.getValue(NarManifestEntry.NAR_VERSION.getManifestName());
BundleCoordinate bundleCoordinate = new BundleCoordinate(groupId, narId, version);
return bundleCoordinate;
}
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();