Search in sources :

Example 1 with ControllerServiceInitializationContext

use of org.apache.nifi.controller.ControllerServiceInitializationContext in project kylo by Teradata.

the class SpringSecurityContextLoader method create.

/**
 * Creates a new Security Context Loader from the specified controller service context.
 *
 * @param controllerServiceContext the controller service initialization context
 * @return the Security Context Loader
 */
@Nonnull
public static SpringSecurityContextLoader create(@Nonnull final ControllerServiceInitializationContext controllerServiceContext) {
    final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.registerSingleton(ControllerServiceInitializationContext.class.getName(), controllerServiceContext);
    final GenericApplicationContext context = new GenericApplicationContext(beanFactory);
    context.refresh();
    return create(context);
}
Also used : ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Nonnull(javax.annotation.Nonnull)

Example 2 with ControllerServiceInitializationContext

use of org.apache.nifi.controller.ControllerServiceInitializationContext 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 3 with ControllerServiceInitializationContext

use of org.apache.nifi.controller.ControllerServiceInitializationContext in project nifi by apache.

the class ControllerServiceInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService)

Example 4 with ControllerServiceInitializationContext

use of org.apache.nifi.controller.ControllerServiceInitializationContext in project nifi-minifi by apache.

the class ControllerServiceInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService)

Aggregations

ControllerServiceInitializationContext (org.apache.nifi.controller.ControllerServiceInitializationContext)4 ControllerService (org.apache.nifi.controller.ControllerService)2 MockControllerServiceInitializationContext (org.apache.nifi.mock.MockControllerServiceInitializationContext)2 NarCloseable (org.apache.nifi.nar.NarCloseable)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Nonnull (javax.annotation.Nonnull)1 Invocable (javax.script.Invocable)1 ScriptException (javax.script.ScriptException)1 ValidationResult (org.apache.nifi.components.ValidationResult)1 StateManager (org.apache.nifi.components.state.StateManager)1 ControllerServiceLookup (org.apache.nifi.controller.ControllerServiceLookup)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 LookupFailureException (org.apache.nifi.lookup.LookupFailureException)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 ScriptEngineConfigurator (org.apache.nifi.processors.script.ScriptEngineConfigurator)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1