Java Code Examples for org.apache.nifi.logging.ComponentLog#isDebugEnabled()

The following examples show how to use org.apache.nifi.logging.ComponentLog#isDebugEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvokeScriptedProcessor.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the valid relationships for this processor as supplied by the
 * script itself.
 *
 * @return a Set of Relationships supported by this processor
 */
@Override
public Set<Relationship> getRelationships() {
    final Set<Relationship> relationships = new HashSet<>();
    final Processor instance = processor.get();
    if (instance != null) {
        try {
            final Set<Relationship> rels = instance.getRelationships();
            if (rels != null && !rels.isEmpty()) {
                relationships.addAll(rels);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get relationships from scripted Processor: " + t;

            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }
    return Collections.unmodifiableSet(relationships);
}
 
Example 2
Source File: InvokeScriptedProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void invokeScriptedProcessorMethod(String methodName, Object... params) {
    // Run the scripted processor's method here, if it exists
    if (scriptEngine instanceof Invocable) {
        final Invocable invocable = (Invocable) scriptEngine;
        final Object obj = scriptEngine.get("processor");
        if (obj != null) {

            ComponentLog logger = getLogger();
            try {
                invocable.invokeMethod(obj, methodName, params);
            } catch (final NoSuchMethodException nsme) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Configured script Processor does not contain the method " + methodName);
                }
            } catch (final Exception e) {
                // An error occurred during onScheduled, propagate it up
                logger.error("Error while executing the scripted processor's method " + methodName, e);
                if (e instanceof ProcessException) {
                    throw (ProcessException) e;
                }
                throw new ProcessException(e);
            }
        }
    }
}
 
Example 3
Source File: ReportingTaskWrapper.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void run() {
    lifecycleState.incrementActiveThreadCount(null);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
        taskNode.getReportingTask().onTrigger(taskNode.getReportingContext());
    } catch (final Throwable t) {
        final ComponentLog componentLog = new SimpleProcessLogger(taskNode.getIdentifier(), taskNode.getReportingTask());
        componentLog.error("Error running task {} due to {}", new Object[]{taskNode.getReportingTask(), t.toString()});
        if (componentLog.isDebugEnabled()) {
            componentLog.error("", t);
        }
    } finally {
        try {
            // if the reporting task is no longer scheduled to run and this is the last thread,
            // invoke the OnStopped methods
            if (!lifecycleState.isScheduled() && lifecycleState.getActiveThreadCount() == 1 && lifecycleState.mustCallOnStoppedMethods()) {
                try (final NarCloseable x = NarCloseable.withComponentNarLoader(extensionManager, taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, taskNode.getReportingTask(), taskNode.getConfigurationContext());
                }
            }
        } finally {
            lifecycleState.decrementActiveThreadCount(null);
        }
    }
}
 
Example 4
Source File: InvokeScriptedProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the valid relationships for this processor as supplied by the
 * script itself.
 *
 * @return a Set of Relationships supported by this processor
 */
@Override
public Set<Relationship> getRelationships() {
    final Set<Relationship> relationships = new HashSet<>();
    final Processor instance = processor.get();
    if (instance != null) {
        try {
            final Set<Relationship> rels = instance.getRelationships();
            if (rels != null && !rels.isEmpty()) {
                relationships.addAll(rels);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get relationships from scripted Processor: " + t;

            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }
    return Collections.unmodifiableSet(relationships);
}
 
Example 5
Source File: ReportingTaskWrapper.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void run() {
    scheduleState.incrementActiveThreadCount();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
        taskNode.getReportingTask().onTrigger(taskNode.getReportingContext());
    } catch (final Throwable t) {
        final ComponentLog componentLog = new SimpleProcessLogger(taskNode.getIdentifier(), taskNode.getReportingTask());
        componentLog.error("Error running task {} due to {}", new Object[]{taskNode.getReportingTask(), t.toString()});
        if (componentLog.isDebugEnabled()) {
            componentLog.error("", t);
        }
    } finally {
        try {
            // if the reporting task is no longer scheduled to run and this is the last thread,
            // invoke the OnStopped methods
            if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
                try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, taskNode.getReportingTask(), taskNode.getConfigurationContext());
                }
            }
        } finally {
            scheduleState.decrementActiveThreadCount();
        }
    }
}
 
Example 6
Source File: AbstractCouchbaseProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the thrown CouchbaseException accordingly.
 *
 * @param context a process context
 * @param session a process session
 * @param logger a logger
 * @param inFile an input FlowFile
 * @param e the thrown CouchbaseException
 * @param errMsg a message to be logged
 */
protected void handleCouchbaseException(final ProcessContext context, final ProcessSession session,
    final ComponentLog logger, FlowFile inFile, CouchbaseException e,
    String errMsg) {
    logger.error(errMsg, e);
    if (inFile != null) {
        ErrorHandlingStrategy strategy = CouchbaseExceptionMappings.getStrategy(e);
        switch (strategy.penalty()) {
            case Penalize:
                if (logger.isDebugEnabled()) {
                    logger.debug("Penalized: {}", new Object[] {inFile});
                }
                inFile = session.penalize(inFile);
                break;
            case Yield:
                if (logger.isDebugEnabled()) {
                    logger.debug("Yielded context: {}", new Object[] {inFile});
                }
                context.yield();
                break;
            case None:
                break;
        }

        switch (strategy.result()) {
            case ProcessException:
                throw new ProcessException(errMsg, e);
            case Failure:
                inFile = session.putAttribute(inFile, CouchbaseAttributes.Exception.key(), e.getClass().getName());
                session.transfer(inFile, REL_FAILURE);
                break;
            case Retry:
                inFile = session.putAttribute(inFile, CouchbaseAttributes.Exception.key(), e.getClass().getName());
                session.transfer(inFile, REL_RETRY);
                break;
        }
    }
}
 
Example 7
Source File: InvokeScriptedProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of property descriptors supported by this processor. The
 * list always includes properties such as script engine name, script file
 * name, script body name, script arguments, and an external module path. If
 * the scripted processor also defines supported properties, those are added
 * to the list as well.
 *
 * @return a List of PropertyDescriptor objects supported by this processor
 */
@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {

    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    List<PropertyDescriptor> supportedPropertyDescriptors = new ArrayList<>();
    supportedPropertyDescriptors.addAll(scriptingComponentHelper.getDescriptors());

    final Processor instance = processor.get();
    if (instance != null) {
        try {
            final List<PropertyDescriptor> instanceDescriptors = instance.getPropertyDescriptors();
            if (instanceDescriptors != null) {
                supportedPropertyDescriptors.addAll(instanceDescriptors);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get property descriptors from Processor: " + t;

            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }

    return Collections.unmodifiableList(supportedPropertyDescriptors);
}
 
Example 8
Source File: SimpleCsvFileLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void loadCache() throws IllegalStateException, IOException {
    if (lock.tryLock()) {
        try {
            final ComponentLog logger = getLogger();
            if (logger.isDebugEnabled()) {
                logger.debug("Loading lookup table from file: " + csvFile);
            }

            final Map<String, String> properties = new HashMap<>();
            try (final InputStream is = new FileInputStream(csvFile)) {
                try (final InputStreamReader reader = new InputStreamReader(is, charset)) {
                    final Iterable<CSVRecord> records = csvFormat.withFirstRecordAsHeader().parse(reader);
                    for (final CSVRecord record : records) {
                        final String key = record.get(lookupKeyColumn);
                        final String value = record.get(lookupValueColumn);
                        if (StringUtils.isBlank(key)) {
                            throw new IllegalStateException("Empty lookup key encountered in: " + csvFile);
                        } else if (!ignoreDuplicates && properties.containsKey(key)) {
                            throw new IllegalStateException("Duplicate lookup key encountered: " + key + " in " + csvFile);
                        } else if (ignoreDuplicates && properties.containsKey(key)) {
                            logger.warn("Duplicate lookup key encountered: {} in {}", new Object[]{key, csvFile});
                        }
                        properties.put(key, value);
                    }
                }
            }

            this.cache = new ConcurrentHashMap<>(properties);

            if (cache.isEmpty()) {
                logger.warn("Lookup table is empty after reading file: " + csvFile);
            }
        } finally {
            lock.unlock();
        }
    }
}
 
Example 9
Source File: JettyWebSocketClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
void maintainSessions() throws Exception {
    if (client == null) {
        return;
    }

    connectionLock.lock();

    final ComponentLog logger = getLogger();
    try {
        // Loop through existing sessions and reconnect.
        for (String clientId : activeSessions.keySet()) {
            final WebSocketMessageRouter router;
            try {
                router = routers.getRouterOrFail(clientId);
            } catch (final WebSocketConfigurationException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("The clientId {} is no longer active. Discarding the clientId.", new Object[]{clientId});
                }
                activeSessions.remove(clientId);
                continue;
            }

            final String sessionId = activeSessions.get(clientId);
            // If this session is still alive, do nothing.
            if (!router.containsSession(sessionId)) {
                // This session is no longer active, reconnect it.
                // If it fails, the sessionId will remain in activeSessions, and retries later.
                // This reconnect attempt is continued until user explicitly stops a processor or this controller service.
                connect(clientId, sessionId);
            }
        }
    } finally {
        connectionLock.unlock();
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Session maintenance completed. activeSessions={}", new Object[]{activeSessions});
    }
}
 
Example 10
Source File: ListFile.java    From nifi with Apache License 2.0 5 votes vote down vote up
public void logPerformance() {
    final ComponentLog logger = getLogger();
    if (!logger.isDebugEnabled()) {
        return;
    }

    final long earliestTimestamp = performanceTracker.getEarliestTimestamp();
    final long millis = System.currentTimeMillis() - earliestTimestamp;
    final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    for (final DiskOperation operation : DiskOperation.values()) {
        final OperationStatistics stats = performanceTracker.getOperationStatistics(operation);

        final StringBuilder sb = new StringBuilder();
        if (stats.getCount() == 0) {
            sb.append("Over the past ").append(seconds).append(" seconds, for Operation '").append(operation).append("' there were no operations performed");
        } else {
            sb.append("Over the past ").append(seconds).append(" seconds, For Operation '").append(operation).append("' there were ")
                .append(stats.getCount()).append(" operations performed with an average time of ")
                .append(stats.getAverage()).append(" milliseconds; Standard Deviation = ").append(stats.getStandardDeviation()).append(" millis; Min Time = ")
                .append(stats.getMin()).append(" millis, Max Time = ").append(stats.getMax()).append(" millis");

            if (logger.isDebugEnabled()) {
                final Map<String, Long> outliers = stats.getOutliers();

                sb.append("; ").append(stats.getOutliers().size()).append(" significant outliers: ");
                sb.append(outliers);
            }
        }

        logger.debug(sb.toString());
    }

    performanceLoggingTimestamp = System.currentTimeMillis();
}
 
Example 11
Source File: AbstractCouchbaseProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the thrown CouchbaseException accordingly.
 *
 * @param context a process context
 * @param session a process session
 * @param logger a logger
 * @param inFile an input FlowFile
 * @param e the thrown CouchbaseException
 * @param errMsg a message to be logged
 */
protected void handleCouchbaseException(final ProcessContext context, final ProcessSession session,
    final ComponentLog logger, FlowFile inFile, CouchbaseException e,
    String errMsg) {
    logger.error(errMsg, e);
    if (inFile != null) {
        ErrorHandlingStrategy strategy = CouchbaseExceptionMappings.getStrategy(e);
        switch (strategy.penalty()) {
            case Penalize:
                if (logger.isDebugEnabled()) {
                    logger.debug("Penalized: {}", new Object[] {inFile});
                }
                inFile = session.penalize(inFile);
                break;
            case Yield:
                if (logger.isDebugEnabled()) {
                    logger.debug("Yielded context: {}", new Object[] {inFile});
                }
                context.yield();
                break;
            case None:
                break;
        }

        switch (strategy.result()) {
            case ProcessException:
                throw new ProcessException(errMsg, e);
            case Failure:
                inFile = session.putAttribute(inFile, CouchbaseAttributes.Exception.key(), e.getClass().getName());
                session.transfer(inFile, REL_FAILURE);
                break;
            case Retry:
                inFile = session.putAttribute(inFile, CouchbaseAttributes.Exception.key(), e.getClass().getName());
                session.transfer(inFile, REL_RETRY);
                break;
        }
    }
}
 
Example 12
Source File: ScriptingComponentHelper.java    From nifi-script-tester with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see nifi.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
 
Example 13
Source File: ScriptingComponentHelper.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see org.apache.nifi.processors.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
 
Example 14
Source File: LogAttribute.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final String logLevelValue = context.getProperty(LOG_LEVEL).getValue().toLowerCase();

    final DebugLevels logLevel;
    try {
        logLevel = DebugLevels.valueOf(logLevelValue);
    } catch (Exception e) {
        throw new ProcessException(e);
    }

    final ComponentLog LOG = getLogger();
    boolean isLogLevelEnabled = false;
    switch (logLevel) {
        case trace:
            isLogLevelEnabled = LOG.isTraceEnabled();
            break;
        case debug:
            isLogLevelEnabled = LOG.isDebugEnabled();
            break;
        case info:
            isLogLevelEnabled = LOG.isInfoEnabled();
            break;
        case warn:
            isLogLevelEnabled = LOG.isWarnEnabled();
            break;
        case error:
            isLogLevelEnabled = LOG.isErrorEnabled();
            break;
    }

    if (!isLogLevelEnabled) {
        transferChunk(session);
        return;
    }

    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    processFlowFile(LOG, logLevel, flowFile, session, context);
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example 15
Source File: ScriptingComponentHelper.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see org.apache.nifi.processors.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
 
Example 16
Source File: BaseScriptedLookupService.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of property descriptors supported by this processor. The
 * list always includes properties such as script engine name, script file
 * name, script body name, script arguments, and an external module path. If
 * the scripted processor also defines supported properties, those are added
 * to the list as well.
 *
 * @return a List of PropertyDescriptor objects supported by this processor
 */
@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {

    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    List<PropertyDescriptor> supportedPropertyDescriptors = new ArrayList<>();
    List<PropertyDescriptor> _temp = new ArrayList<>();
    _temp.addAll(scriptingComponentHelper.getDescriptors());
    _temp.remove(scriptingComponentHelper.SCRIPT_ENGINE);

    PropertyDescriptor.Builder jythonLessEngineProp = new PropertyDescriptor
            .Builder().fromPropertyDescriptor(scriptingComponentHelper.SCRIPT_ENGINE);
    List<AllowableValue> filtered = scriptingComponentHelper.getScriptEngineAllowableValues()
            .stream().filter(allowableValue -> !allowableValue.getValue().contains("ython"))
            .collect(Collectors.toList());
    jythonLessEngineProp.allowableValues(filtered.toArray(new AllowableValue[filtered.size()]));

    supportedPropertyDescriptors.add(jythonLessEngineProp.build());
    supportedPropertyDescriptors.addAll(_temp);

    final ConfigurableComponent instance = lookupService.get();
    if (instance != null) {
        try {
            final List<PropertyDescriptor> instanceDescriptors = instance.getPropertyDescriptors();
            if (instanceDescriptors != null) {
                supportedPropertyDescriptors.addAll(instanceDescriptors);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get property descriptors from Processor: " + t;

            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }

    return Collections.unmodifiableList(supportedPropertyDescriptors);
}
 
Example 17
Source File: CSVRecordLookupService.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void loadCache() throws IllegalStateException, IOException {
    if (lock.tryLock()) {
        try {
            final ComponentLog logger = getLogger();
            if (logger.isDebugEnabled()) {
                logger.debug("Loading lookup table from file: " + csvFile);
            }

            ConcurrentHashMap<String, Record> cache = new ConcurrentHashMap<>();
            try (final InputStream is = new FileInputStream(csvFile)) {
                try (final InputStreamReader reader = new InputStreamReader(is, charset)) {
                    final CSVParser records = csvFormat.withFirstRecordAsHeader().parse(reader);
                    RecordSchema lookupRecordSchema = null;
                    for (final CSVRecord record : records) {
                        final String key = record.get(lookupKeyColumn);

                        if (StringUtils.isBlank(key)) {
                            throw new IllegalStateException("Empty lookup key encountered in: " + csvFile);
                        } else if (!ignoreDuplicates && cache.containsKey(key)) {
                            throw new IllegalStateException("Duplicate lookup key encountered: " + key + " in " + csvFile);
                        } else if (ignoreDuplicates && cache.containsKey(key)) {
                            logger.warn("Duplicate lookup key encountered: {} in {}", new Object[]{key, csvFile});
                        }

                        // Put each key/value pair (except the lookup) into the properties
                        final Map<String, Object> properties = new HashMap<>();
                        record.toMap().forEach((k, v) -> {
                            if (!lookupKeyColumn.equals(k)) {
                                properties.put(k, v);
                            }
                        });

                        if (lookupRecordSchema == null) {
                            List<RecordField> recordFields = new ArrayList<>(properties.size());
                            properties.forEach((k, v) -> recordFields.add(new RecordField(k, RecordFieldType.STRING.getDataType())));
                            lookupRecordSchema = new SimpleRecordSchema(recordFields);
                        }

                        cache.put(key, new MapRecord(lookupRecordSchema, properties));
                    }
                }
            }

            this.cache = cache;

            if (cache.isEmpty()) {
                logger.warn("Lookup table is empty after reading file: " + csvFile);
            }
        } finally {
            lock.unlock();
        }
    }
}
 
Example 18
Source File: RollbackOnFailure.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Create a function to use with {@link ExceptionHandler} that adjust error type based on functional context.
 */
public static <FCT extends RollbackOnFailure> BiFunction<FCT, ErrorTypes, ErrorTypes.Result> createAdjustError(final ComponentLog logger) {
    return (c, t) -> {

        ErrorTypes.Result adjusted = null;
        switch (t.destination()) {

            case ProcessException:
                // If this process can rollback, then rollback it.
                if (!c.canRollback()) {
                    // If an exception is thrown but the processor is not transactional and processed count > 0, adjust it to self,
                    // in order to stop any further processing until this input is processed successfully.
                    // If we throw an Exception in this state, the already succeeded FlowFiles will be rolled back, too.
                    // In case the progress was made by other preceding inputs,
                    // those successful inputs should be sent to 'success' and this input stays in incoming queue.
                    // In case this input made some progress to external system, the partial update will be replayed again,
                    // can cause duplicated data.
                    c.discontinue();
                    // We should not penalize a FlowFile, if we did, other FlowFiles can be fetched first.
                    // We need to block others to be processed until this one finishes.
                    adjusted = new ErrorTypes.Result(ErrorTypes.Destination.Self, ErrorTypes.Penalty.Yield);
                }
                break;

            case Failure:
            case Retry:
                if (c.isRollbackOnFailure()) {
                    c.discontinue();
                    if (c.canRollback()) {
                        // If this process can rollback, then throw ProcessException instead, in order to rollback.
                        adjusted = new ErrorTypes.Result(ErrorTypes.Destination.ProcessException, ErrorTypes.Penalty.Yield);
                    } else {
                        // If not,
                        adjusted = new ErrorTypes.Result(ErrorTypes.Destination.Self, ErrorTypes.Penalty.Yield);
                    }
                }
                break;
        }

        if (adjusted != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Adjusted {} to {} based on context rollbackOnFailure={}, processedCount={}, transactional={}",
                        new Object[]{t, adjusted, c.isRollbackOnFailure(), c.getProcessedCount(), c.isTransactional()});
            }
            return adjusted;
        }

        return t.result();
    };
}
 
Example 19
Source File: LogMessage.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {

    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final String logLevelValue = context.getProperty(LOG_LEVEL).evaluateAttributeExpressions(flowFile).getValue().toLowerCase();

    final MessageLogLevel logLevel;
    try {
        logLevel = MessageLogLevel.valueOf(logLevelValue);
    } catch (Exception e) {
        throw new ProcessException(e);
    }

    final ComponentLog logger = getLogger();
    boolean isLogLevelEnabled = false;
    switch (logLevel) {
        case trace:
            isLogLevelEnabled = logger.isTraceEnabled();
            break;
        case debug:
            isLogLevelEnabled = logger.isDebugEnabled();
            break;
        case info:
            isLogLevelEnabled = logger.isInfoEnabled();
            break;
        case warn:
            isLogLevelEnabled = logger.isWarnEnabled();
            break;
        case error:
            isLogLevelEnabled = logger.isErrorEnabled();
            break;
    }

    if (isLogLevelEnabled) {
        processFlowFile(logger, logLevel, flowFile, context);
    }
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example 20
Source File: LogAttribute.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final String logLevelValue = context.getProperty(LOG_LEVEL).getValue().toLowerCase();

    final DebugLevels logLevel;
    try {
        logLevel = DebugLevels.valueOf(logLevelValue);
    } catch (Exception e) {
        throw new ProcessException(e);
    }

    final ComponentLog LOG = getLogger();
    boolean isLogLevelEnabled = false;
    switch (logLevel) {
        case trace:
            isLogLevelEnabled = LOG.isTraceEnabled();
            break;
        case debug:
            isLogLevelEnabled = LOG.isDebugEnabled();
            break;
        case info:
            isLogLevelEnabled = LOG.isInfoEnabled();
            break;
        case warn:
            isLogLevelEnabled = LOG.isWarnEnabled();
            break;
        case error:
            isLogLevelEnabled = LOG.isErrorEnabled();
            break;
    }

    if (!isLogLevelEnabled) {
        transferChunk(session);
        return;
    }

    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    processFlowFile(LOG, logLevel, flowFile, session, context);
    session.transfer(flowFile, REL_SUCCESS);
}