Search in sources :

Example 1 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project intellij-plugins by JetBrains.

the class DartServerCompletionContributor method createLookupElement.

private static LookupElement createLookupElement(@NotNull final Project project, @NotNull final CompletionSuggestion suggestion) {
    final Element element = suggestion.getElement();
    final Location location = element == null ? null : element.getLocation();
    final DartLookupObject lookupObject = new DartLookupObject(project, location);
    final String lookupString = suggestion.getCompletion();
    LookupElementBuilder lookup = LookupElementBuilder.create(lookupObject, lookupString);
    // keywords are bold
    if (suggestion.getKind().equals(CompletionSuggestionKind.KEYWORD)) {
        lookup = lookup.bold();
    }
    final int dotIndex = lookupString.indexOf('.');
    if (dotIndex > 0 && dotIndex < lookupString.length() - 1 && StringUtil.isJavaIdentifier(lookupString.substring(0, dotIndex)) && StringUtil.isJavaIdentifier(lookupString.substring(dotIndex + 1))) {
        // 'path.Context' should match 'Conte' prefix
        lookup = lookup.withLookupString(lookupString.substring(dotIndex + 1));
    }
    boolean shouldSetSelection = true;
    if (element != null) {
        // @deprecated
        if (element.isDeprecated()) {
            lookup = lookup.strikeout();
        }
        // append type parameters
        final String typeParameters = element.getTypeParameters();
        if (typeParameters != null) {
            lookup = lookup.appendTailText(typeParameters, false);
        }
        // append parameters
        final String parameters = element.getParameters();
        if (parameters != null) {
            lookup = lookup.appendTailText(parameters, false);
        }
        // append return type
        final String returnType = element.getReturnType();
        if (!StringUtils.isEmpty(returnType)) {
            lookup = lookup.withTypeText(returnType, true);
        }
        // icon
        Icon icon = getBaseImage(element);
        if (icon != null) {
            icon = applyVisibility(icon, element.isPrivate());
            icon = applyOverlay(icon, element.isFinal(), AllIcons.Nodes.FinalMark);
            icon = applyOverlay(icon, element.isConst(), AllIcons.Nodes.FinalMark);
            lookup = lookup.withIcon(icon);
        }
        // Prepare for typing arguments, if any.
        if (CompletionSuggestionKind.INVOCATION.equals(suggestion.getKind())) {
            shouldSetSelection = false;
            final List<String> parameterNames = suggestion.getParameterNames();
            if (parameterNames != null) {
                lookup = lookup.withInsertHandler((context, item) -> {
                    // like in JavaCompletionUtil.insertParentheses()
                    final boolean needRightParenth = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET || parameterNames.isEmpty() && context.getCompletionChar() != '(';
                    if (parameterNames.isEmpty()) {
                        final ParenthesesInsertHandler<LookupElement> handler = ParenthesesInsertHandler.getInstance(false, false, false, needRightParenth, false);
                        handler.handleInsert(context, item);
                    } else {
                        final ParenthesesInsertHandler<LookupElement> handler = ParenthesesInsertHandler.getInstance(true, false, false, needRightParenth, false);
                        handler.handleInsert(context, item);
                        // Show parameters popup.
                        final Editor editor = context.getEditor();
                        final PsiElement psiElement = lookupObject.getElement();
                        if (DartCodeInsightSettings.getInstance().INSERT_DEFAULT_ARG_VALUES) {
                            // Insert argument defaults if provided.
                            final String argumentListString = suggestion.getDefaultArgumentListString();
                            if (argumentListString != null) {
                                final Document document = editor.getDocument();
                                int offset = editor.getCaretModel().getOffset();
                                // At this point caret is expected to be right after the opening paren.
                                // But if user was completing using Tab over the existing method call with arguments then old arguments are still there,
                                // if so, skip inserting argumentListString
                                final CharSequence text = document.getCharsSequence();
                                if (text.charAt(offset - 1) == '(' && text.charAt(offset) == ')') {
                                    document.insertString(offset, argumentListString);
                                    PsiDocumentManager.getInstance(project).commitDocument(document);
                                    final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(context.getFile());
                                    final int[] ranges = suggestion.getDefaultArgumentListTextRanges();
                                    // Only proceed if ranges are provided and well-formed.
                                    if (ranges != null && (ranges.length & 1) == 0) {
                                        int index = 0;
                                        while (index < ranges.length) {
                                            final int start = ranges[index];
                                            final int length = ranges[index + 1];
                                            final String arg = argumentListString.substring(start, start + length);
                                            final TextExpression expression = new TextExpression(arg);
                                            final TextRange range = new TextRange(offset + start, offset + start + length);
                                            index += 2;
                                            builder.replaceRange(range, "group_" + (index - 1), expression, true);
                                        }
                                        builder.run(editor, true);
                                    }
                                }
                            }
                        }
                        AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, psiElement);
                    }
                });
            }
        }
    }
    // Use selection offset / length.
    if (shouldSetSelection) {
        lookup = lookup.withInsertHandler((context, item) -> {
            final Editor editor = context.getEditor();
            final int startOffset = context.getStartOffset() + suggestion.getSelectionOffset();
            final int endOffset = startOffset + suggestion.getSelectionLength();
            editor.getCaretModel().moveToOffset(startOffset);
            if (endOffset > startOffset) {
                editor.getSelectionModel().setSelection(startOffset, endOffset);
            }
        });
    }
    return PrioritizedLookupElement.withPriority(lookup, suggestion.getRelevance());
}
Also used : Language(com.intellij.lang.Language) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) DartResolveUtil(com.jetbrains.lang.dart.util.DartResolveUtil) DartSdk(com.jetbrains.lang.dart.sdk.DartSdk) AllIcons(com.intellij.icons.AllIcons) DartUriElement(com.jetbrains.lang.dart.psi.DartUriElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) XMLLanguage(com.intellij.lang.xml.XMLLanguage) StringUtils(org.apache.commons.lang3.StringUtils) RowIcon(com.intellij.ui.RowIcon) AutoPopupController(com.intellij.codeInsight.AutoPopupController) org.dartlang.analysis.server.protocol(org.dartlang.analysis.server.protocol) ProcessingContext(com.intellij.util.ProcessingContext) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) DartYamlFileTypeFactory(com.jetbrains.lang.dart.DartYamlFileTypeFactory) CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) PsiReference(com.intellij.psi.PsiReference) PlatformPatterns.psiElement(com.intellij.patterns.PlatformPatterns.psiElement) TextRange(com.intellij.openapi.util.TextRange) HtmlFileType(com.intellij.ide.highlighter.HtmlFileType) DartNewExpression(com.jetbrains.lang.dart.psi.DartNewExpression) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) DartLanguage(com.jetbrains.lang.dart.DartLanguage) NotNull(org.jetbrains.annotations.NotNull) DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ParenthesesInsertHandler(com.intellij.codeInsight.completion.util.ParenthesesInsertHandler) DartCodeInsightSettings(com.jetbrains.lang.dart.ide.codeInsight.DartCodeInsightSettings) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) TemplateBuilderFactory(com.intellij.codeInsight.template.TemplateBuilderFactory) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) PlatformIcons(com.intellij.util.PlatformIcons) StandardPatterns.or(com.intellij.patterns.StandardPatterns.or) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) Editor(com.intellij.openapi.editor.Editor) PlatformPatterns.psiFile(com.intellij.patterns.PlatformPatterns.psiFile) PubspecYamlUtil(com.jetbrains.lang.dart.util.PubspecYamlUtil) Pair(com.intellij.openapi.util.Pair) LayeredIcon(com.intellij.ui.LayeredIcon) javax.swing(javax.swing) DartUriElement(com.jetbrains.lang.dart.psi.DartUriElement) PlatformPatterns.psiElement(com.intellij.patterns.PlatformPatterns.psiElement) PsiElement(com.intellij.psi.PsiElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) TextRange(com.intellij.openapi.util.TextRange) ParenthesesInsertHandler(com.intellij.codeInsight.completion.util.ParenthesesInsertHandler) Document(com.intellij.openapi.editor.Document) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) RowIcon(com.intellij.ui.RowIcon) LayeredIcon(com.intellij.ui.LayeredIcon) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 2 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project nifi by apache.

the class QueryDatabaseTable method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    // Fetch the column/table info once
    if (!setupComplete.get()) {
        super.setup(context);
    }
    ProcessSession session = sessionFactory.createSession();
    final List<FlowFile> resultSetFlowFiles = new ArrayList<>();
    final ComponentLog logger = getLogger();
    final DBCPService dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
    final DatabaseAdapter dbAdapter = dbAdapters.get(context.getProperty(DB_TYPE).getValue());
    final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions().getValue();
    final String columnNames = context.getProperty(COLUMN_NAMES).evaluateAttributeExpressions().getValue();
    final String maxValueColumnNames = context.getProperty(MAX_VALUE_COLUMN_NAMES).evaluateAttributeExpressions().getValue();
    final String customWhereClause = context.getProperty(WHERE_CLAUSE).evaluateAttributeExpressions().getValue();
    final Integer fetchSize = context.getProperty(FETCH_SIZE).evaluateAttributeExpressions().asInteger();
    final Integer maxRowsPerFlowFile = context.getProperty(MAX_ROWS_PER_FLOW_FILE).evaluateAttributeExpressions().asInteger();
    final Integer outputBatchSizeField = context.getProperty(OUTPUT_BATCH_SIZE).evaluateAttributeExpressions().asInteger();
    final int outputBatchSize = outputBatchSizeField == null ? 0 : outputBatchSizeField;
    final Integer maxFragments = context.getProperty(MAX_FRAGMENTS).isSet() ? context.getProperty(MAX_FRAGMENTS).evaluateAttributeExpressions().asInteger() : 0;
    final JdbcCommon.AvroConversionOptions options = JdbcCommon.AvroConversionOptions.builder().recordName(tableName).maxRows(maxRowsPerFlowFile).convertNames(context.getProperty(NORMALIZE_NAMES_FOR_AVRO).asBoolean()).useLogicalTypes(context.getProperty(USE_AVRO_LOGICAL_TYPES).asBoolean()).defaultPrecision(context.getProperty(DEFAULT_PRECISION).evaluateAttributeExpressions().asInteger()).defaultScale(context.getProperty(DEFAULT_SCALE).evaluateAttributeExpressions().asInteger()).build();
    final StateManager stateManager = context.getStateManager();
    final StateMap stateMap;
    try {
        stateMap = stateManager.getState(Scope.CLUSTER);
    } catch (final IOException ioe) {
        getLogger().error("Failed to retrieve observed maximum values from the State Manager. Will not perform " + "query until this is accomplished.", ioe);
        context.yield();
        return;
    }
    // Make a mutable copy of the current state property map. This will be updated by the result row callback, and eventually
    // set as the current state map (after the session has been committed)
    final Map<String, String> statePropertyMap = new HashMap<>(stateMap.toMap());
    // If an initial max value for column(s) has been specified using properties, and this column is not in the state manager, sync them to the state property map
    for (final Map.Entry<String, String> maxProp : maxValueProperties.entrySet()) {
        String maxPropKey = maxProp.getKey().toLowerCase();
        String fullyQualifiedMaxPropKey = getStateKey(tableName, maxPropKey);
        if (!statePropertyMap.containsKey(fullyQualifiedMaxPropKey)) {
            String newMaxPropValue;
            // but store the new initial max value under the fully-qualified key.
            if (statePropertyMap.containsKey(maxPropKey)) {
                newMaxPropValue = statePropertyMap.get(maxPropKey);
            } else {
                newMaxPropValue = maxProp.getValue();
            }
            statePropertyMap.put(fullyQualifiedMaxPropKey, newMaxPropValue);
        }
    }
    List<String> maxValueColumnNameList = StringUtils.isEmpty(maxValueColumnNames) ? null : Arrays.asList(maxValueColumnNames.split("\\s*,\\s*"));
    final String selectQuery = getQuery(dbAdapter, tableName, columnNames, maxValueColumnNameList, customWhereClause, statePropertyMap);
    final StopWatch stopWatch = new StopWatch(true);
    final String fragmentIdentifier = UUID.randomUUID().toString();
    try (final Connection con = dbcpService.getConnection();
        final Statement st = con.createStatement()) {
        if (fetchSize != null && fetchSize > 0) {
            try {
                st.setFetchSize(fetchSize);
            } catch (SQLException se) {
                // Not all drivers support this, just log the error (at debug level) and move on
                logger.debug("Cannot set fetch size to {} due to {}", new Object[] { fetchSize, se.getLocalizedMessage() }, se);
            }
        }
        String jdbcURL = "DBCPService";
        try {
            DatabaseMetaData databaseMetaData = con.getMetaData();
            if (databaseMetaData != null) {
                jdbcURL = databaseMetaData.getURL();
            }
        } catch (SQLException se) {
        // Ignore and use default JDBC URL. This shouldn't happen unless the driver doesn't implement getMetaData() properly
        }
        final Integer queryTimeout = context.getProperty(QUERY_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.SECONDS).intValue();
        // timeout in seconds
        st.setQueryTimeout(queryTimeout);
        try {
            logger.debug("Executing query {}", new Object[] { selectQuery });
            final ResultSet resultSet = st.executeQuery(selectQuery);
            int fragmentIndex = 0;
            while (true) {
                final AtomicLong nrOfRows = new AtomicLong(0L);
                FlowFile fileToProcess = session.create();
                try {
                    fileToProcess = session.write(fileToProcess, out -> {
                        // Max values will be updated in the state property map by the callback
                        final MaxValueResultSetRowCollector maxValCollector = new MaxValueResultSetRowCollector(tableName, statePropertyMap, dbAdapter);
                        try {
                            nrOfRows.set(JdbcCommon.convertToAvroStream(resultSet, out, options, maxValCollector));
                        } catch (SQLException | RuntimeException e) {
                            throw new ProcessException("Error during database query or conversion of records to Avro.", e);
                        }
                    });
                } catch (ProcessException e) {
                    // Add flowfile to results before rethrowing so it will be removed from session in outer catch
                    resultSetFlowFiles.add(fileToProcess);
                    throw e;
                }
                if (nrOfRows.get() > 0) {
                    // set attribute how many rows were selected
                    fileToProcess = session.putAttribute(fileToProcess, RESULT_ROW_COUNT, String.valueOf(nrOfRows.get()));
                    fileToProcess = session.putAttribute(fileToProcess, RESULT_TABLENAME, tableName);
                    fileToProcess = session.putAttribute(fileToProcess, CoreAttributes.MIME_TYPE.key(), JdbcCommon.MIME_TYPE_AVRO_BINARY);
                    if (maxRowsPerFlowFile > 0) {
                        fileToProcess = session.putAttribute(fileToProcess, "fragment.identifier", fragmentIdentifier);
                        fileToProcess = session.putAttribute(fileToProcess, "fragment.index", String.valueOf(fragmentIndex));
                    }
                    logger.info("{} contains {} Avro records; transferring to 'success'", new Object[] { fileToProcess, nrOfRows.get() });
                    session.getProvenanceReporter().receive(fileToProcess, jdbcURL, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
                    resultSetFlowFiles.add(fileToProcess);
                    // If we've reached the batch size, send out the flow files
                    if (outputBatchSize > 0 && resultSetFlowFiles.size() >= outputBatchSize) {
                        session.transfer(resultSetFlowFiles, REL_SUCCESS);
                        session.commit();
                        resultSetFlowFiles.clear();
                    }
                } else {
                    // If there were no rows returned, don't send the flowfile
                    session.remove(fileToProcess);
                    context.yield();
                    break;
                }
                fragmentIndex++;
                if (maxFragments > 0 && fragmentIndex >= maxFragments) {
                    break;
                }
            }
            // Even though the maximum value and total count are known at this point, to maintain consistent behavior if Output Batch Size is set, do not store the attributes
            if (outputBatchSize == 0) {
                for (int i = 0; i < resultSetFlowFiles.size(); i++) {
                    // Add maximum values as attributes
                    for (Map.Entry<String, String> entry : statePropertyMap.entrySet()) {
                        // Get just the column name from the key
                        String key = entry.getKey();
                        String colName = key.substring(key.lastIndexOf(NAMESPACE_DELIMITER) + NAMESPACE_DELIMITER.length());
                        resultSetFlowFiles.set(i, session.putAttribute(resultSetFlowFiles.get(i), "maxvalue." + colName, entry.getValue()));
                    }
                    // set count on all FlowFiles
                    if (maxRowsPerFlowFile > 0) {
                        resultSetFlowFiles.set(i, session.putAttribute(resultSetFlowFiles.get(i), "fragment.count", Integer.toString(fragmentIndex)));
                    }
                }
            }
        } catch (final SQLException e) {
            throw e;
        }
        session.transfer(resultSetFlowFiles, REL_SUCCESS);
    } catch (final ProcessException | SQLException e) {
        logger.error("Unable to execute SQL select query {} due to {}", new Object[] { selectQuery, e });
        if (!resultSetFlowFiles.isEmpty()) {
            session.remove(resultSetFlowFiles);
        }
        context.yield();
    } finally {
        session.commit();
        try {
            // Update the state
            stateManager.setState(statePropertyMap, Scope.CLUSTER);
        } catch (IOException ioe) {
            getLogger().error("{} failed to update State Manager, maximum observed values will not be recorded", new Object[] { this, ioe });
        }
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) StandardValidators(org.apache.nifi.processor.util.StandardValidators) Arrays(java.util.Arrays) Connection(java.sql.Connection) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Scope(org.apache.nifi.components.state.Scope) ResultSet(java.sql.ResultSet) Map(java.util.Map) ParseException(java.text.ParseException) TriggerSerially(org.apache.nifi.annotation.behavior.TriggerSerially) FlowFile(org.apache.nifi.flowfile.FlowFile) NORMALIZE_NAMES_FOR_AVRO(org.apache.nifi.processors.standard.util.JdbcCommon.NORMALIZE_NAMES_FOR_AVRO) Set(java.util.Set) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) UUID(java.util.UUID) StateMap(org.apache.nifi.components.state.StateMap) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) Stateful(org.apache.nifi.annotation.behavior.Stateful) List(java.util.List) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) JdbcCommon(org.apache.nifi.processors.standard.util.JdbcCommon) StopWatch(org.apache.nifi.util.StopWatch) Tags(org.apache.nifi.annotation.documentation.Tags) DBCPService(org.apache.nifi.dbcp.DBCPService) ResultSetMetaData(java.sql.ResultSetMetaData) IntStream(java.util.stream.IntStream) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) USE_AVRO_LOGICAL_TYPES(org.apache.nifi.processors.standard.util.JdbcCommon.USE_AVRO_LOGICAL_TYPES) DatabaseMetaData(java.sql.DatabaseMetaData) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) DEFAULT_SCALE(org.apache.nifi.processors.standard.util.JdbcCommon.DEFAULT_SCALE) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) Relationship(org.apache.nifi.processor.Relationship) DEFAULT_PRECISION(org.apache.nifi.processors.standard.util.JdbcCommon.DEFAULT_PRECISION) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) DatabaseAdapter(org.apache.nifi.processors.standard.db.DatabaseAdapter) StateManager(org.apache.nifi.components.state.StateManager) ProcessContext(org.apache.nifi.processor.ProcessContext) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) Statement(java.sql.Statement) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) Collections(java.util.Collections) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) StateMap(org.apache.nifi.components.state.StateMap) ArrayList(java.util.ArrayList) StateManager(org.apache.nifi.components.state.StateManager) ResultSet(java.sql.ResultSet) FlowFile(org.apache.nifi.flowfile.FlowFile) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) DatabaseMetaData(java.sql.DatabaseMetaData) ComponentLog(org.apache.nifi.logging.ComponentLog) DatabaseAdapter(org.apache.nifi.processors.standard.db.DatabaseAdapter) StopWatch(org.apache.nifi.util.StopWatch) JdbcCommon(org.apache.nifi.processors.standard.util.JdbcCommon) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcessException(org.apache.nifi.processor.exception.ProcessException) DBCPService(org.apache.nifi.dbcp.DBCPService) Map(java.util.Map) StateMap(org.apache.nifi.components.state.StateMap) HashMap(java.util.HashMap)

Example 3 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project nifi by apache.

the class Notify method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    final PropertyValue signalIdProperty = context.getProperty(RELEASE_SIGNAL_IDENTIFIER);
    final PropertyValue counterNameProperty = context.getProperty(SIGNAL_COUNTER_NAME);
    final PropertyValue deltaProperty = context.getProperty(SIGNAL_COUNTER_DELTA);
    final String attributeCacheRegex = context.getProperty(ATTRIBUTE_CACHE_REGEX).getValue();
    final Integer bufferCount = context.getProperty(SIGNAL_BUFFER_COUNT).asInteger();
    // the cache client used to interact with the distributed cache.
    final AtomicDistributedMapCacheClient cache = context.getProperty(DISTRIBUTED_CACHE_SERVICE).asControllerService(AtomicDistributedMapCacheClient.class);
    final WaitNotifyProtocol protocol = new WaitNotifyProtocol(cache);
    final Map<String, SignalBuffer> signalBuffers = new HashMap<>();
    for (int i = 0; i < bufferCount; i++) {
        final FlowFile flowFile = session.get();
        if (flowFile == null) {
            break;
        }
        // Signal id is computed from attribute 'RELEASE_SIGNAL_IDENTIFIER' with expression language support
        final String signalId = signalIdProperty.evaluateAttributeExpressions(flowFile).getValue();
        // if the computed value is null, or empty, we transfer the flow file to failure relationship
        if (StringUtils.isBlank(signalId)) {
            logger.error("FlowFile {} has no attribute for given Release Signal Identifier", new Object[] { flowFile });
            // set 'notified' attribute
            session.transfer(session.putAttribute(flowFile, NOTIFIED_ATTRIBUTE_NAME, String.valueOf(false)), REL_FAILURE);
            continue;
        }
        String counterName = counterNameProperty.evaluateAttributeExpressions(flowFile).getValue();
        if (StringUtils.isEmpty(counterName)) {
            counterName = WaitNotifyProtocol.DEFAULT_COUNT_NAME;
        }
        int delta = 1;
        if (deltaProperty.isSet()) {
            final String deltaStr = deltaProperty.evaluateAttributeExpressions(flowFile).getValue();
            try {
                delta = Integer.parseInt(deltaStr);
            } catch (final NumberFormatException e) {
                logger.error("Failed to calculate delta for FlowFile {} due to {}", new Object[] { flowFile, e }, e);
                session.transfer(session.putAttribute(flowFile, NOTIFIED_ATTRIBUTE_NAME, String.valueOf(false)), REL_FAILURE);
                continue;
            }
        }
        if (!signalBuffers.containsKey(signalId)) {
            signalBuffers.put(signalId, new SignalBuffer());
        }
        final SignalBuffer signalBuffer = signalBuffers.get(signalId);
        if (StringUtils.isNotEmpty(attributeCacheRegex)) {
            flowFile.getAttributes().entrySet().stream().filter(e -> (!e.getKey().equals("uuid") && e.getKey().matches(attributeCacheRegex))).forEach(e -> signalBuffer.attributesToCache.put(e.getKey(), e.getValue()));
        }
        signalBuffer.incrementDelta(counterName, delta);
        signalBuffer.flowFiles.add(flowFile);
        if (logger.isDebugEnabled()) {
            logger.debug("Cached release signal identifier {} counterName {} from FlowFile {}", new Object[] { signalId, counterName, flowFile });
        }
    }
    signalBuffers.forEach((signalId, signalBuffer) -> {
        // retry after yielding for a while.
        try {
            protocol.notify(signalId, signalBuffer.deltas, signalBuffer.attributesToCache);
            signalBuffer.flowFiles.forEach(flowFile -> session.transfer(session.putAttribute(flowFile, NOTIFIED_ATTRIBUTE_NAME, String.valueOf(true)), REL_SUCCESS));
        } catch (IOException e) {
            throw new RuntimeException(String.format("Unable to communicate with cache when processing %s due to %s", signalId, e), e);
        }
    });
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) HashMap(java.util.HashMap) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) PropertyValue(org.apache.nifi.components.PropertyValue) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) AtomicDistributedMapCacheClient(org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) AtomicDistributedMapCacheClient(org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient)

Example 4 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project nifi by apache.

the class ExecuteSparkInteractive method onTrigger.

@Override
public void onTrigger(ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog log = getLogger();
    final LivySessionService livySessionService = context.getProperty(LIVY_CONTROLLER_SERVICE).asControllerService(LivySessionService.class);
    final Map<String, String> livyController = livySessionService.getSession();
    if (livyController == null || livyController.isEmpty()) {
        log.debug("No Spark session available (yet), routing flowfile to wait");
        session.transfer(flowFile, REL_WAIT);
        return;
    }
    final long statusCheckInterval = context.getProperty(STATUS_CHECK_INTERVAL).evaluateAttributeExpressions(flowFile).asTimePeriod(TimeUnit.MILLISECONDS);
    Charset charset;
    try {
        charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());
    } catch (Exception e) {
        log.warn("Illegal character set name specified, defaulting to UTF-8");
        charset = StandardCharsets.UTF_8;
    }
    String sessionId = livyController.get("sessionId");
    String livyUrl = livyController.get("livyUrl");
    String code = context.getProperty(CODE).evaluateAttributeExpressions(flowFile).getValue();
    if (StringUtils.isEmpty(code)) {
        try (InputStream inputStream = session.read(flowFile)) {
            // If no code was provided, assume it is in the content of the incoming flow file
            code = IOUtils.toString(inputStream, charset);
        } catch (IOException ioe) {
            log.error("Error reading input flowfile, penalizing and routing to failure", new Object[] { flowFile, ioe.getMessage() }, ioe);
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
            return;
        }
    }
    code = StringEscapeUtils.escapeJavaScript(code);
    String payload = "{\"code\":\"" + code + "\"}";
    try {
        final JSONObject result = submitAndHandleJob(livyUrl, livySessionService, sessionId, payload, statusCheckInterval);
        log.debug("ExecuteSparkInteractive Result of Job Submit: " + result);
        if (result == null) {
            session.transfer(flowFile, REL_FAILURE);
        } else {
            try {
                final JSONObject output = result.getJSONObject("data");
                flowFile = session.write(flowFile, out -> out.write(output.toString().getBytes()));
                flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), LivySessionService.APPLICATION_JSON);
                session.transfer(flowFile, REL_SUCCESS);
            } catch (JSONException je) {
                // The result doesn't contain the data, just send the output object as the flow file content to failure (after penalizing)
                log.error("Spark Session returned an error, sending the output JSON object as the flow file content to failure (after penalizing)");
                flowFile = session.write(flowFile, out -> out.write(result.toString().getBytes()));
                flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), LivySessionService.APPLICATION_JSON);
                flowFile = session.penalize(flowFile);
                session.transfer(flowFile, REL_FAILURE);
            }
        }
    } catch (IOException ioe) {
        log.error("Failure processing flowfile {} due to {}, penalizing and routing to failure", new Object[] { flowFile, ioe.getMessage() }, ioe);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) OutputStream(java.io.OutputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) InputStreamReader(java.io.InputStreamReader) LivySessionService(org.apache.nifi.controller.api.livy.LivySessionService) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) JSONException(org.codehaus.jettison.json.JSONException) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) BufferedReader(java.io.BufferedReader) Tags(org.apache.nifi.annotation.documentation.Tags) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Collections(java.util.Collections) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) InputStream(java.io.InputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) LivySessionService(org.apache.nifi.controller.api.livy.LivySessionService) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 5 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project CzechIdMng by bcvsolutions.

the class SchedulerController method find.

/**
 * Finds scheduled tasks
 *
 * @return all tasks
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCHEDULER_READ + "')")
@ApiOperation(value = "Search scheduled tasks", nickname = "searchSchedulerTasks", tags = { SchedulerController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.SCHEDULER_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.SCHEDULER_READ, description = "") }) })
@ApiImplicitParams({ @ApiImplicitParam(name = "page", dataType = "string", paramType = "query", value = "Results page you want to retrieve (0..N)"), @ApiImplicitParam(name = "size", dataType = "string", paramType = "query", value = "Number of records per page."), @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "Sorting criteria in the format: property(,asc|desc). " + "Default sort order is ascending. " + "Multiple sort criteria are supported.") })
public Resources<Task> find(@RequestParam(required = false) MultiValueMap<String, Object> parameters, @PageableDefault Pageable pageable) {
    String text = getParameterConverter().toString(parameters, DataFilter.PARAMETER_TEXT);
    List<Task> tasks = schedulerService.getAllTasks().stream().filter(task -> {
        // filter - like name or description only
        return StringUtils.isEmpty(text) || task.getTaskType().getSimpleName().toLowerCase().contains(text.toLowerCase()) || (task.getDescription() != null && task.getDescription().toLowerCase().contains(text.toLowerCase()));
    }).sorted((taskOne, taskTwo) -> {
        Sort sort = pageable.getSort();
        if (pageable.getSort() == null) {
            return 0;
        }
        int compareAscValue = 0;
        boolean asc = true;
        // "naive" sort implementation
        if (sort.getOrderFor(PROPERTY_TASK_TYPE) != null) {
            asc = sort.getOrderFor(PROPERTY_TASK_TYPE).isAscending();
            compareAscValue = taskOne.getTaskType().getSimpleName().compareTo(taskTwo.getTaskType().getSimpleName());
        }
        if (sort.getOrderFor(PROPERTY_DESCRIPTION) != null) {
            asc = sort.getOrderFor(PROPERTY_DESCRIPTION).isAscending();
            compareAscValue = taskOne.getDescription().compareTo(taskTwo.getDescription());
        }
        if (sort.getOrderFor(PROPERTY_INSTANCE_ID) != null) {
            asc = sort.getOrderFor(PROPERTY_INSTANCE_ID).isAscending();
            compareAscValue = taskOne.getInstanceId().compareTo(taskTwo.getInstanceId());
        }
        return asc ? compareAscValue : compareAscValue * -1;
    }).collect(Collectors.toList());
    // "naive" pagination
    int first = pageable.getPageNumber() * pageable.getPageSize();
    int last = pageable.getPageSize() + first;
    List<Task> taskPage = tasks.subList(first < tasks.size() ? first : tasks.size() > 0 ? tasks.size() - 1 : 0, last < tasks.size() ? last : tasks.size());
    // 
    return pageToResources(new PageImpl(taskPage, pageable, tasks.size()), Task.class);
}
Also used : PagedResourcesAssembler(org.springframework.data.web.PagedResourcesAssembler) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) ApiOperation(io.swagger.annotations.ApiOperation) DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) SwaggerConfig(eu.bcvsolutions.idm.core.api.config.swagger.SwaggerConfig) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AuthorizationScope(io.swagger.annotations.AuthorizationScope) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) Api(io.swagger.annotations.Api) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) MultiValueMap(org.springframework.util.MultiValueMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) SchedulerManager(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulerManager) Page(org.springframework.data.domain.Page) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ParameterConverter(eu.bcvsolutions.idm.core.api.utils.ParameterConverter) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) BaseController(eu.bcvsolutions.idm.core.api.rest.BaseController) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) PageableDefault(org.springframework.data.web.PageableDefault) Resources(org.springframework.hateoas.Resources) ResponseEntity(org.springframework.http.ResponseEntity) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) PageImpl(org.springframework.data.domain.PageImpl) Authorization(io.swagger.annotations.Authorization) PageImpl(org.springframework.data.domain.PageImpl) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) Sort(org.springframework.data.domain.Sort) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)62 List (java.util.List)46 ArrayList (java.util.ArrayList)35 Map (java.util.Map)33 Collectors (java.util.stream.Collectors)33 HashMap (java.util.HashMap)25 Set (java.util.Set)23 Collections (java.util.Collections)21 IOException (java.io.IOException)20 Arrays (java.util.Arrays)20 HashSet (java.util.HashSet)16 Optional (java.util.Optional)16 Logger (org.slf4j.Logger)16 LoggerFactory (org.slf4j.LoggerFactory)15 Collection (java.util.Collection)14 File (java.io.File)9 URI (java.net.URI)8 Comparator (java.util.Comparator)8 UUID (java.util.UUID)8 TimeUnit (java.util.concurrent.TimeUnit)7