use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.
the class StandardProcessorDAO method verifyUpdate.
private void verifyUpdate(ProcessorNode processor, ProcessorDTO processorDTO) {
// ensure the state, if specified, is valid
if (isNotNull(processorDTO.getState())) {
try {
final ScheduledState purposedScheduledState = ScheduledState.valueOf(processorDTO.getState());
// only attempt an action if it is changing
if (!purposedScheduledState.equals(processor.getScheduledState())) {
// perform the appropriate action
switch(purposedScheduledState) {
case RUNNING:
processor.verifyCanStart();
break;
case STOPPED:
switch(processor.getScheduledState()) {
case RUNNING:
processor.verifyCanStop();
break;
case DISABLED:
processor.verifyCanEnable();
break;
}
break;
case DISABLED:
processor.verifyCanDisable();
break;
}
}
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException(String.format("The specified processor state (%s) is not valid. Valid options are 'RUNNING', 'STOPPED', and 'DISABLED'.", processorDTO.getState()));
}
}
boolean modificationRequest = false;
if (isAnyNotNull(processorDTO.getName(), processorDTO.getBundle())) {
modificationRequest = true;
}
final BundleDTO bundleDTO = processorDTO.getBundle();
if (bundleDTO != null) {
// ensures all nodes in a cluster have the bundle, throws exception if bundle not found for the given type
final BundleCoordinate bundleCoordinate = BundleUtils.getBundle(processor.getCanonicalClassName(), bundleDTO);
// ensure we are only changing to a bundle with the same group and id, but different version
processor.verifyCanUpdateBundle(bundleCoordinate);
}
final ProcessorConfigDTO configDTO = processorDTO.getConfig();
if (configDTO != null) {
if (isAnyNotNull(configDTO.getAnnotationData(), configDTO.getAutoTerminatedRelationships(), configDTO.getBulletinLevel(), configDTO.getComments(), configDTO.getConcurrentlySchedulableTaskCount(), configDTO.getPenaltyDuration(), configDTO.getProperties(), configDTO.getSchedulingPeriod(), configDTO.getSchedulingStrategy(), configDTO.getExecutionNode(), configDTO.getYieldDuration())) {
modificationRequest = true;
}
// validate the request
final List<String> requestValidation = validateProposedConfiguration(processor, configDTO);
// ensure there was no validation errors
if (!requestValidation.isEmpty()) {
throw new ValidationException(requestValidation);
}
}
if (modificationRequest) {
processor.verifyCanUpdate();
}
}
use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.
the class NarUnpacker method unpackNars.
public static ExtensionMapping unpackNars(final NiFiProperties props, final Bundle systemBundle) {
final List<Path> narLibraryDirs = props.getNarLibraryDirectories();
final File frameworkWorkingDir = props.getFrameworkWorkingDirectory();
final File extensionsWorkingDir = props.getExtensionsWorkingDirectory();
final File docsWorkingDir = props.getComponentDocumentationWorkingDirectory();
final Map<File, BundleCoordinate> unpackedNars = new HashMap<>();
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(frameworkWorkingDir);
FileUtils.ensureDirectoryExistAndCanReadAndWrite(extensionsWorkingDir);
FileUtils.ensureDirectoryExistAndCanReadAndWrite(docsWorkingDir);
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());
// 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());
// 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, frameworkWorkingDir);
} else {
final File unpackedExtension = unpackNar(narFile, extensionsWorkingDir);
// record the current bundle
unpackedNars.put(unpackedExtension, new BundleCoordinate(groupId, narId, version));
// unpack the extension nar
unpackedExtensions.add(unpackedExtension);
}
}
}
// ensure we've found the framework nar
if (unpackedFramework == null) {
throw new IllegalStateException("No framework NAR found.");
} else if (!unpackedFramework.canRead()) {
throw new IllegalStateException("Framework NAR cannot be read.");
}
// Determine if any nars no longer exist and delete their working directories. This happens
// if a new version of a nar is dropped into the lib dir. ensure no old framework are present
final File[] frameworkWorkingDirContents = frameworkWorkingDir.listFiles();
if (frameworkWorkingDirContents != null) {
for (final File unpackedNar : frameworkWorkingDirContents) {
if (!unpackedFramework.equals(unpackedNar)) {
FileUtils.deleteFile(unpackedNar, true);
}
}
}
// ensure no old extensions are present
final File[] extensionsWorkingDirContents = extensionsWorkingDir.listFiles();
if (extensionsWorkingDirContents != null) {
for (final File unpackedNar : extensionsWorkingDirContents) {
if (!unpackedExtensions.contains(unpackedNar)) {
FileUtils.deleteFile(unpackedNar, true);
}
}
}
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);
// unpack docs for the system bundle which will catch any JARs directly in the lib directory that might have docs
unpackBundleDocs(docsWorkingDir, extensionMapping, systemBundle.getBundleDetails().getCoordinate(), systemBundle.getBundleDetails().getWorkingDirectory());
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;
}
use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.
the class NarUnpacker method mapExtensions.
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, "META-INF/bundled-dependencies");
unpackBundleDocs(docsDirectory, mapping, bundleCoordinate, bundledDependencies);
}
}
use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.
the class StandardAuthorizableLookup method createTemporaryProcessorsAndControllerServices.
/**
* Creates temporary instances of all processors and controller services found in the specified snippet.
*
* @param snippet snippet
* @param processors processors
* @param controllerServices controller services
*/
private void createTemporaryProcessorsAndControllerServices(final FlowSnippetDTO snippet, final Set<ComponentAuthorizable> processors, final Set<ComponentAuthorizable> controllerServices) {
if (snippet == null) {
return;
}
if (snippet.getProcessors() != null) {
snippet.getProcessors().forEach(processor -> {
try {
final BundleCoordinate bundle = BundleUtils.getCompatibleBundle(processor.getType(), processor.getBundle());
processors.add(getConfigurableComponent(processor.getType(), new BundleDTO(bundle.getGroup(), bundle.getId(), bundle.getVersion())));
} catch (final IllegalStateException e) {
// no compatible bundles... no additional auth checks necessary... if created, will be ghosted
}
});
}
if (snippet.getControllerServices() != null) {
snippet.getControllerServices().forEach(controllerService -> {
try {
final BundleCoordinate bundle = BundleUtils.getCompatibleBundle(controllerService.getType(), controllerService.getBundle());
controllerServices.add(getConfigurableComponent(controllerService.getType(), new BundleDTO(bundle.getGroup(), bundle.getId(), bundle.getVersion())));
} catch (final IllegalStateException e) {
// no compatible bundles... no additional auth checks necessary... if created, will be ghosted
}
});
}
if (snippet.getProcessGroups() != null) {
snippet.getProcessGroups().stream().forEach(group -> createTemporaryProcessorsAndControllerServices(group.getContents(), processors, controllerServices));
}
}
use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.
the class FlowRegistryUtils method getRestrictedComponents.
public static Set<ConfigurableComponent> getRestrictedComponents(final VersionedProcessGroup group) {
final Set<ConfigurableComponent> restrictedComponents = new HashSet<>();
final Set<Tuple<String, BundleCoordinate>> componentTypes = new HashSet<>();
populateComponentTypes(group, componentTypes);
for (final Tuple<String, BundleCoordinate> tuple : componentTypes) {
final ConfigurableComponent component = ExtensionManager.getTempComponent(tuple.getKey(), tuple.getValue());
if (component == null) {
throw new NiFiCoreException("Could not create an instance of component " + tuple.getKey() + " using bundle coordinates " + tuple.getValue());
}
final boolean isRestricted = component.getClass().isAnnotationPresent(Restricted.class);
if (isRestricted) {
restrictedComponents.add(component);
}
}
return restrictedComponents;
}
Aggregations