Search in sources :

Example 31 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class ScriptedLookupService method reloadScript.

/**
 * Reloads the script RecordReaderFactory. This must be called within the lock.
 *
 * @param scriptBody An input stream associated with the script content
 * @return Whether the script was successfully reloaded
 */
@Override
protected boolean reloadScript(final String scriptBody) {
    // note we are starting here with a fresh listing of validation
    // results since we are (re)loading a new/updated script. any
    // existing validation results are not relevant
    final Collection<ValidationResult> results = new HashSet<>();
    try {
        // get the engine and ensure its invocable
        if (scriptEngine instanceof Invocable) {
            final Invocable invocable = (Invocable) scriptEngine;
            // Find a custom configurator and invoke their eval() method
            ScriptEngineConfigurator configurator = scriptingComponentHelper.scriptEngineConfiguratorMap.get(scriptingComponentHelper.getScriptEngineName().toLowerCase());
            if (configurator != null) {
                configurator.eval(scriptEngine, scriptBody, scriptingComponentHelper.getModules());
            } else {
                // evaluate the script
                scriptEngine.eval(scriptBody);
            }
            // get configured LookupService from the script (if it exists)
            final Object obj = scriptEngine.get("lookupService");
            if (obj != null) {
                final ComponentLog logger = getLogger();
                try {
                    // set the logger if the processor wants it
                    invocable.invokeMethod(obj, "setLogger", logger);
                } catch (final NoSuchMethodException nsme) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Scripted LookupService does not contain a setLogger method.");
                    }
                }
                // record the processor for use later
                final LookupService<Object> scriptedLookupService = invocable.getInterface(obj, LookupService.class);
                lookupService.set(scriptedLookupService);
                if (scriptedLookupService != null) {
                    try {
                        scriptedLookupService.initialize(new ControllerServiceInitializationContext() {

                            @Override
                            public String getIdentifier() {
                                return ScriptedLookupService.this.getIdentifier();
                            }

                            @Override
                            public ComponentLog getLogger() {
                                return logger;
                            }

                            @Override
                            public StateManager getStateManager() {
                                return ScriptedLookupService.this.getStateManager();
                            }

                            @Override
                            public ControllerServiceLookup getControllerServiceLookup() {
                                return ScriptedLookupService.super.getControllerServiceLookup();
                            }

                            @Override
                            public String getKerberosServicePrincipal() {
                                return ScriptedLookupService.this.kerberosServicePrincipal;
                            }

                            @Override
                            public File getKerberosServiceKeytab() {
                                return ScriptedLookupService.this.kerberosServiceKeytab;
                            }

                            @Override
                            public File getKerberosConfigurationFile() {
                                return ScriptedLookupService.this.kerberosConfigFile;
                            }
                        });
                    } catch (final Exception e) {
                        logger.error("Unable to initialize scripted LookupService: " + e.getLocalizedMessage(), e);
                        throw new ProcessException(e);
                    }
                }
            } else {
                throw new ScriptException("No LookupService was defined by the script.");
            }
        } else {
            throw new ScriptException("Script engine is not Invocable, cannot be used for ScriptedLookupService");
        }
    } catch (final Exception ex) {
        final ComponentLog logger = getLogger();
        final String message = "Unable to load script: " + ex.getLocalizedMessage();
        logger.error(message, ex);
        results.add(new ValidationResult.Builder().subject("ScriptedLookupServiceValidation").valid(false).explanation("Unable to load script due to " + ex.getLocalizedMessage()).input(scriptingComponentHelper.getScriptPath()).build());
    }
    // store the updated validation results
    validationResults.set(results);
    // return whether there was any issues loading the configured script
    return results.isEmpty();
}
Also used : ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) ScriptEngineConfigurator(org.apache.nifi.processors.script.ScriptEngineConfigurator) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ProcessException(org.apache.nifi.processor.exception.ProcessException) StateManager(org.apache.nifi.components.state.StateManager) File(java.io.File) ControllerServiceLookup(org.apache.nifi.controller.ControllerServiceLookup) HashSet(java.util.HashSet)

Example 32 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class InvokeScriptedProcessor method reloadScript.

/**
 * Reloads the script Processor. This must be called within the lock.
 *
 * @param scriptBody An input stream associated with the script content
 * @return Whether the script was successfully reloaded
 */
private boolean reloadScript(final String scriptBody) {
    // note we are starting here with a fresh listing of validation
    // results since we are (re)loading a new/updated script. any
    // existing validation results are not relevant
    final Collection<ValidationResult> results = new HashSet<>();
    try {
        // get the engine and ensure its invocable
        if (scriptEngine instanceof Invocable) {
            final Invocable invocable = (Invocable) scriptEngine;
            // Find a custom configurator and invoke their eval() method
            ScriptEngineConfigurator configurator = scriptingComponentHelper.scriptEngineConfiguratorMap.get(scriptingComponentHelper.getScriptEngineName().toLowerCase());
            if (configurator != null) {
                configurator.eval(scriptEngine, scriptBody, scriptingComponentHelper.getModules());
            } else {
                // evaluate the script
                scriptEngine.eval(scriptBody);
            }
            // get configured processor from the script (if it exists)
            final Object obj = scriptEngine.get("processor");
            if (obj != null) {
                final ComponentLog logger = getLogger();
                try {
                    // set the logger if the processor wants it
                    invocable.invokeMethod(obj, "setLogger", logger);
                } catch (final NoSuchMethodException nsme) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Configured script Processor does not contain a setLogger method.");
                    }
                }
                // record the processor for use later
                final Processor scriptProcessor = invocable.getInterface(obj, Processor.class);
                processor.set(scriptProcessor);
                if (scriptProcessor != null) {
                    try {
                        scriptProcessor.initialize(new ProcessorInitializationContext() {

                            @Override
                            public String getIdentifier() {
                                return InvokeScriptedProcessor.this.getIdentifier();
                            }

                            @Override
                            public ComponentLog getLogger() {
                                return logger;
                            }

                            @Override
                            public ControllerServiceLookup getControllerServiceLookup() {
                                return InvokeScriptedProcessor.super.getControllerServiceLookup();
                            }

                            @Override
                            public NodeTypeProvider getNodeTypeProvider() {
                                return InvokeScriptedProcessor.super.getNodeTypeProvider();
                            }

                            @Override
                            public String getKerberosServicePrincipal() {
                                return InvokeScriptedProcessor.this.kerberosServicePrincipal;
                            }

                            @Override
                            public File getKerberosServiceKeytab() {
                                return InvokeScriptedProcessor.this.kerberosServiceKeytab;
                            }

                            @Override
                            public File getKerberosConfigurationFile() {
                                return InvokeScriptedProcessor.this.kerberosConfigFile;
                            }
                        });
                    } catch (final Exception e) {
                        logger.error("Unable to initialize scripted Processor: " + e.getLocalizedMessage(), e);
                        throw new ProcessException(e);
                    }
                }
            } else {
                throw new ScriptException("No processor was defined by the script.");
            }
        }
    } catch (final Exception ex) {
        final ComponentLog logger = getLogger();
        final String message = "Unable to load script: " + ex.getLocalizedMessage();
        logger.error(message, ex);
        results.add(new ValidationResult.Builder().subject("ScriptValidation").valid(false).explanation("Unable to load script due to " + ex.getLocalizedMessage()).input(scriptingComponentHelper.getScriptPath()).build());
    }
    // store the updated validation results
    validationResults.set(results);
    // return whether there was any issues loading the configured script
    return results.isEmpty();
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) NodeTypeProvider(org.apache.nifi.controller.NodeTypeProvider) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ProcessException(org.apache.nifi.processor.exception.ProcessException) File(java.io.File) ControllerServiceLookup(org.apache.nifi.controller.ControllerServiceLookup) HashSet(java.util.HashSet)

Example 33 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class InvokeScriptedProcessor method customValidate.

/**
 * Invokes the validate() routine provided by the script, allowing for
 * custom validation code. This method assumes there is a valid Processor
 * defined in the script and it has been loaded by the
 * InvokeScriptedProcessor processor
 *
 * @param context The validation context to be passed into the custom
 * validate method
 * @return A collection of ValidationResults returned by the custom validate
 * method
 */
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    Collection<ValidationResult> commonValidationResults = super.customValidate(context);
    if (!commonValidationResults.isEmpty()) {
        return commonValidationResults;
    }
    // and don't print anything into log.
    if (validationResults.get() != null) {
        return validationResults.get();
    }
    scriptingComponentHelper.setScriptEngineName(context.getProperty(scriptingComponentHelper.SCRIPT_ENGINE).getValue());
    scriptingComponentHelper.setScriptPath(context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).evaluateAttributeExpressions().getValue());
    scriptingComponentHelper.setScriptBody(context.getProperty(ScriptingComponentUtils.SCRIPT_BODY).getValue());
    String modulePath = context.getProperty(ScriptingComponentUtils.MODULES).evaluateAttributeExpressions().getValue();
    if (!StringUtils.isEmpty(modulePath)) {
        scriptingComponentHelper.setModules(modulePath.split(","));
    } else {
        scriptingComponentHelper.setModules(new String[0]);
    }
    setup();
    // Now that InvokeScriptedProcessor is validated, we can call validate on the scripted processor
    final Processor instance = processor.get();
    final Collection<ValidationResult> currentValidationResults = validationResults.get();
    // if there was existing validation errors and the processor loaded successfully
    if (currentValidationResults.isEmpty() && instance != null) {
        try {
            // defer to the underlying processor for validation
            final Collection<ValidationResult> instanceResults = instance.validate(context);
            if (instanceResults != null && instanceResults.size() > 0) {
                // return the validation results from the underlying instance
                return instanceResults;
            }
        } catch (final Exception e) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to validate the script Processor: " + e;
            logger.error(message, e);
            // return a new validation message
            final Collection<ValidationResult> results = new HashSet<>();
            results.add(new ValidationResult.Builder().subject("Validation").valid(false).explanation("An error occurred calling validate in the configured script Processor.").input(context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue()).build());
            return results;
        }
    }
    return currentValidationResults;
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) Collection(java.util.Collection) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException)

Example 34 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class InvokeScriptedProcessor method reloadScriptFile.

/**
 * Reloads the script located at the given path
 *
 * @param scriptPath the path to the script file to be loaded
 * @return true if the script was loaded successfully; false otherwise
 */
private boolean reloadScriptFile(final String scriptPath) {
    final Collection<ValidationResult> results = new HashSet<>();
    try (final FileInputStream scriptStream = new FileInputStream(scriptPath)) {
        return reloadScript(IOUtils.toString(scriptStream, Charset.defaultCharset()));
    } catch (final Exception e) {
        final ComponentLog logger = getLogger();
        final String message = "Unable to load script: " + e;
        logger.error(message, e);
        results.add(new ValidationResult.Builder().subject("ScriptValidation").valid(false).explanation("Unable to load script due to " + e).input(scriptPath).build());
    }
    // store the updated validation results
    validationResults.set(results);
    // return whether there was any issues loading the configured script
    return results.isEmpty();
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException) HashSet(java.util.HashSet)

Example 35 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class InvokeScriptedProcessor method onTrigger.

/**
 * Invokes the onTrigger() method of the scripted processor. If the script
 * failed to reload, the processor yields until the script can be reloaded
 * successfully. If the scripted processor's onTrigger() method throws an
 * exception, a ProcessException will be thrown. If no processor is defined
 * by the script, an error is logged with the system.
 *
 * @param context provides access to convenience methods for obtaining
 * property values, delaying the scheduling of the processor, provides
 * access to Controller Services, etc.
 * @param sessionFactory provides access to a {@link ProcessSessionFactory},
 * which can be used for accessing FlowFiles, etc.
 * @throws ProcessException if the scripted processor's onTrigger() method
 * throws an exception
 */
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    // Initialize the rest of the processor resources if we have not already done so
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    ComponentLog log = getLogger();
    // ensure the processor (if it exists) is loaded
    final Processor instance = processor.get();
    // ensure the processor did not fail to reload at some point
    final Collection<ValidationResult> results = validationResults.get();
    if (!results.isEmpty()) {
        log.error(String.format("Unable to run because the Processor is not valid: [%s]", StringUtils.join(results, ", ")));
        context.yield();
        return;
    }
    if (instance != null) {
        try {
            // run the processor
            instance.onTrigger(context, sessionFactory);
        } catch (final ProcessException e) {
            final String message = String.format("An error occurred executing the configured Processor [%s]: %s", context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue(), e);
            log.error(message);
            throw e;
        }
    } else {
        log.error("There is no processor defined by the script");
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)153 ArrayList (java.util.ArrayList)63 Test (org.junit.Test)56 ValidationContext (org.apache.nifi.components.ValidationContext)36 MockProcessContext (org.apache.nifi.util.MockProcessContext)26 TestRunner (org.apache.nifi.util.TestRunner)21 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)20 Validator (org.apache.nifi.components.Validator)20 HashSet (java.util.HashSet)18 ProcessContext (org.apache.nifi.processor.ProcessContext)15 HashMap (java.util.HashMap)12 File (java.io.File)11 Collection (java.util.Collection)11 SSLContextService (org.apache.nifi.ssl.SSLContextService)11 Map (java.util.Map)10 List (java.util.List)9 ComponentLog (org.apache.nifi.logging.ComponentLog)9 IOException (java.io.IOException)8 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)8 KerberosCredentialsService (org.apache.nifi.kerberos.KerberosCredentialsService)8