Search in sources :

Example 81 with EMPTY

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

the class EvaluateJsonPath method onTrigger.

@Override
public void onTrigger(final ProcessContext processContext, final ProcessSession processSession) throws ProcessException {
    FlowFile flowFile = processSession.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    DocumentContext documentContext;
    try {
        documentContext = validateAndEstablishJsonContext(processSession, flowFile);
    } catch (InvalidJsonException e) {
        logger.error("FlowFile {} did not have valid JSON content.", new Object[] { flowFile });
        processSession.transfer(flowFile, REL_FAILURE);
        return;
    }
    Set<Map.Entry<String, JsonPath>> attributeJsonPathEntries = attributeToJsonPathEntrySetQueue.poll();
    if (attributeJsonPathEntries == null) {
        attributeJsonPathEntries = processContext.getProperties().entrySet().stream().filter(e -> e.getKey().isDynamic()).collect(Collectors.toMap(e -> e.getKey().getName(), e -> JsonPath.compile(e.getValue()))).entrySet();
    }
    try {
        // We'll only be using this map if destinationIsAttribute == true
        final Map<String, String> jsonPathResults = destinationIsAttribute ? new HashMap<>(attributeJsonPathEntries.size()) : Collections.EMPTY_MAP;
        for (final Map.Entry<String, JsonPath> attributeJsonPathEntry : attributeJsonPathEntries) {
            final String jsonPathAttrKey = attributeJsonPathEntry.getKey();
            final JsonPath jsonPathExp = attributeJsonPathEntry.getValue();
            Object result;
            try {
                Object potentialResult = documentContext.read(jsonPathExp);
                if (returnType.equals(RETURN_TYPE_SCALAR) && !isJsonScalar(potentialResult)) {
                    logger.error("Unable to return a scalar value for the expression {} for FlowFile {}. Evaluated value was {}. Transferring to {}.", new Object[] { jsonPathExp.getPath(), flowFile.getId(), potentialResult.toString(), REL_FAILURE.getName() });
                    processSession.transfer(flowFile, REL_FAILURE);
                    return;
                }
                result = potentialResult;
            } catch (PathNotFoundException e) {
                if (pathNotFound.equals(PATH_NOT_FOUND_WARN)) {
                    logger.warn("FlowFile {} could not find path {} for attribute key {}.", new Object[] { flowFile.getId(), jsonPathExp.getPath(), jsonPathAttrKey }, e);
                }
                if (destinationIsAttribute) {
                    jsonPathResults.put(jsonPathAttrKey, StringUtils.EMPTY);
                    continue;
                } else {
                    processSession.transfer(flowFile, REL_NO_MATCH);
                    return;
                }
            }
            final String resultRepresentation = getResultRepresentation(result, nullDefaultValue);
            if (destinationIsAttribute) {
                jsonPathResults.put(jsonPathAttrKey, resultRepresentation);
            } else {
                flowFile = processSession.write(flowFile, out -> {
                    try (OutputStream outputStream = new BufferedOutputStream(out)) {
                        outputStream.write(resultRepresentation.getBytes(StandardCharsets.UTF_8));
                    }
                });
                processSession.getProvenanceReporter().modifyContent(flowFile, "Replaced content with result of expression " + jsonPathExp.getPath());
            }
        }
        // jsonPathResults map will be empty if this is false
        if (destinationIsAttribute) {
            flowFile = processSession.putAllAttributes(flowFile, jsonPathResults);
        }
        processSession.transfer(flowFile, REL_MATCH);
    } finally {
        attributeToJsonPathEntrySetQueue.offer(attributeJsonPathEntries);
    }
}
Also used : CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) OnRemoved(org.apache.nifi.annotation.lifecycle.OnRemoved) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) HashSet(java.util.HashSet) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ValidationResult(org.apache.nifi.components.ValidationResult) OutputStream(java.io.OutputStream) OnUnscheduled(org.apache.nifi.annotation.lifecycle.OnUnscheduled) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) DocumentContext(com.jayway.jsonpath.DocumentContext) Queue(java.util.Queue) Tags(org.apache.nifi.annotation.documentation.Tags) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Collections(java.util.Collections) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) FlowFile(org.apache.nifi.flowfile.FlowFile) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BufferedOutputStream(java.io.BufferedOutputStream)

Example 82 with EMPTY

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

the class Wait method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    // Signal id is computed from attribute 'RELEASE_SIGNAL_IDENTIFIER' with expression language support
    final PropertyValue signalIdProperty = context.getProperty(RELEASE_SIGNAL_IDENTIFIER);
    final Integer bufferCount = context.getProperty(WAIT_BUFFER_COUNT).asInteger();
    final Map<Relationship, List<FlowFile>> processedFlowFiles = new HashMap<>();
    final Function<Relationship, List<FlowFile>> getFlowFilesFor = r -> processedFlowFiles.computeIfAbsent(r, k -> new ArrayList<>());
    final AtomicReference<String> targetSignalId = new AtomicReference<>();
    final AtomicInteger bufferedCount = new AtomicInteger(0);
    final List<FlowFile> failedFilteringFlowFiles = new ArrayList<>();
    final Supplier<FlowFileFilter.FlowFileFilterResult> acceptResultSupplier = () -> bufferedCount.incrementAndGet() == bufferCount ? ACCEPT_AND_TERMINATE : ACCEPT_AND_CONTINUE;
    final List<FlowFile> flowFiles = session.get(f -> {
        final String fSignalId = signalIdProperty.evaluateAttributeExpressions(f).getValue();
        // if the computed value is null, or empty, we transfer the FlowFile to failure relationship
        if (StringUtils.isBlank(fSignalId)) {
            // We can't penalize f before getting it from session, so keep it in a temporal list.
            logger.error("FlowFile {} has no attribute for given Release Signal Identifier", new Object[] { f });
            failedFilteringFlowFiles.add(f);
            return ACCEPT_AND_CONTINUE;
        }
        final String targetSignalIdStr = targetSignalId.get();
        if (targetSignalIdStr == null) {
            // This is the first one.
            targetSignalId.set(fSignalId);
            return acceptResultSupplier.get();
        }
        if (targetSignalIdStr.equals(fSignalId)) {
            return acceptResultSupplier.get();
        }
        return REJECT_AND_CONTINUE;
    });
    final String attributeCopyMode = context.getProperty(ATTRIBUTE_COPY_MODE).getValue();
    final boolean replaceOriginalAttributes = ATTRIBUTE_COPY_REPLACE.getValue().equals(attributeCopyMode);
    final AtomicReference<Signal> signalRef = new AtomicReference<>();
    // This map contains original counts before those are consumed to release incoming FlowFiles.
    final HashMap<String, Long> originalSignalCounts = new HashMap<>();
    final Consumer<FlowFile> transferToFailure = flowFile -> {
        flowFile = session.penalize(flowFile);
        getFlowFilesFor.apply(REL_FAILURE).add(flowFile);
    };
    final Consumer<Entry<Relationship, List<FlowFile>>> transferFlowFiles = routedFlowFiles -> {
        Relationship relationship = routedFlowFiles.getKey();
        if (REL_WAIT.equals(relationship)) {
            final String waitMode = context.getProperty(WAIT_MODE).getValue();
            if (WAIT_MODE_KEEP_IN_UPSTREAM.getValue().equals(waitMode)) {
                // Transfer to self.
                relationship = Relationship.SELF;
            }
        }
        final List<FlowFile> flowFilesWithSignalAttributes = routedFlowFiles.getValue().stream().map(f -> copySignalAttributes(session, f, signalRef.get(), originalSignalCounts, replaceOriginalAttributes)).collect(Collectors.toList());
        session.transfer(flowFilesWithSignalAttributes, relationship);
    };
    failedFilteringFlowFiles.forEach(f -> {
        flowFiles.remove(f);
        transferToFailure.accept(f);
    });
    if (flowFiles.isEmpty()) {
        // If there was nothing but failed FlowFiles while filtering, transfer those and end immediately.
        processedFlowFiles.entrySet().forEach(transferFlowFiles);
        return;
    }
    // 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 String signalId = targetSignalId.get();
    final Signal signal;
    // get notifying signal
    try {
        signal = protocol.getSignal(signalId);
        if (signal != null) {
            originalSignalCounts.putAll(signal.getCounts());
        }
        signalRef.set(signal);
    } catch (final IOException e) {
        throw new ProcessException(String.format("Failed to get signal for %s due to %s", signalId, e), e);
    }
    String targetCounterName = null;
    long targetCount = 1;
    int releasableFlowFileCount = 1;
    final List<FlowFile> candidates = new ArrayList<>();
    for (FlowFile flowFile : flowFiles) {
        // Set wait start timestamp if it's not set yet
        String waitStartTimestamp = flowFile.getAttribute(WAIT_START_TIMESTAMP);
        if (waitStartTimestamp == null) {
            waitStartTimestamp = String.valueOf(System.currentTimeMillis());
            flowFile = session.putAttribute(flowFile, WAIT_START_TIMESTAMP, waitStartTimestamp);
        }
        long lWaitStartTimestamp;
        try {
            lWaitStartTimestamp = Long.parseLong(waitStartTimestamp);
        } catch (NumberFormatException nfe) {
            logger.error("{} has an invalid value '{}' on FlowFile {}", new Object[] { WAIT_START_TIMESTAMP, waitStartTimestamp, flowFile });
            transferToFailure.accept(flowFile);
            continue;
        }
        // check for expiration
        long expirationDuration = context.getProperty(EXPIRATION_DURATION).asTimePeriod(TimeUnit.MILLISECONDS);
        long now = System.currentTimeMillis();
        if (now > (lWaitStartTimestamp + expirationDuration)) {
            logger.info("FlowFile {} expired after {}ms", new Object[] { flowFile, (now - lWaitStartTimestamp) });
            getFlowFilesFor.apply(REL_EXPIRED).add(flowFile);
            continue;
        }
        // If there's no signal yet, then we don't have to evaluate target counts. Return immediately.
        if (signal == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("No release signal found for {} on FlowFile {} yet", new Object[] { signalId, flowFile });
            }
            getFlowFilesFor.apply(REL_WAIT).add(flowFile);
            continue;
        }
        // Fix target counter name and count from current FlowFile, if those are not set yet.
        if (candidates.isEmpty()) {
            targetCounterName = context.getProperty(SIGNAL_COUNTER_NAME).evaluateAttributeExpressions(flowFile).getValue();
            try {
                targetCount = Long.valueOf(context.getProperty(TARGET_SIGNAL_COUNT).evaluateAttributeExpressions(flowFile).getValue());
            } catch (final NumberFormatException e) {
                transferToFailure.accept(flowFile);
                logger.error("Failed to parse targetCount when processing {} due to {}", new Object[] { flowFile, e }, e);
                continue;
            }
            try {
                releasableFlowFileCount = Integer.valueOf(context.getProperty(RELEASABLE_FLOWFILE_COUNT).evaluateAttributeExpressions(flowFile).getValue());
            } catch (final NumberFormatException e) {
                transferToFailure.accept(flowFile);
                logger.error("Failed to parse releasableFlowFileCount when processing {} due to {}", new Object[] { flowFile, e }, e);
                continue;
            }
        }
        // FlowFile is now validated and added to candidates.
        candidates.add(flowFile);
    }
    boolean waitCompleted = false;
    boolean waitProgressed = false;
    if (signal != null && !candidates.isEmpty()) {
        if (releasableFlowFileCount > 0) {
            signal.releaseCandidates(targetCounterName, targetCount, releasableFlowFileCount, candidates, released -> getFlowFilesFor.apply(REL_SUCCESS).addAll(released), waiting -> getFlowFilesFor.apply(REL_WAIT).addAll(waiting));
            waitCompleted = signal.getTotalCount() == 0 && signal.getReleasableCount() == 0;
            waitProgressed = !getFlowFilesFor.apply(REL_SUCCESS).isEmpty();
        } else {
            boolean reachedTargetCount = StringUtils.isBlank(targetCounterName) ? signal.isTotalCountReached(targetCount) : signal.isCountReached(targetCounterName, targetCount);
            if (reachedTargetCount) {
                getFlowFilesFor.apply(REL_SUCCESS).addAll(candidates);
            } else {
                getFlowFilesFor.apply(REL_WAIT).addAll(candidates);
            }
        }
    }
    // Transfer FlowFiles.
    processedFlowFiles.entrySet().forEach(transferFlowFiles);
    // Update signal if needed.
    try {
        if (waitCompleted) {
            protocol.complete(signalId);
        } else if (waitProgressed) {
            protocol.replace(signal);
        }
    } catch (final IOException e) {
        session.rollback();
        throw new ProcessException(String.format("Unable to communicate with cache while updating %s due to %s", signalId, e), e);
    }
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) 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) ACCEPT_AND_CONTINUE(org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult.ACCEPT_AND_CONTINUE) ComponentLog(org.apache.nifi.logging.ComponentLog) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ACCEPT_AND_TERMINATE(org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult.ACCEPT_AND_TERMINATE) AtomicDistributedMapCacheClient(org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient) Signal(org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal) 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) AllowableValue(org.apache.nifi.components.AllowableValue) Collectors(java.util.stream.Collectors) REJECT_AND_CONTINUE(org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult.REJECT_AND_CONTINUE) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) Entry(java.util.Map.Entry) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Signal(org.apache.nifi.processors.standard.WaitNotifyProtocol.Signal) Entry(java.util.Map.Entry) AtomicDistributedMapCacheClient(org.apache.nifi.distributed.cache.client.AtomicDistributedMapCacheClient) ArrayList(java.util.ArrayList) List(java.util.List) FlowFile(org.apache.nifi.flowfile.FlowFile) PropertyValue(org.apache.nifi.components.PropertyValue) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProcessException(org.apache.nifi.processor.exception.ProcessException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Relationship(org.apache.nifi.processor.Relationship)

Example 83 with EMPTY

use of org.apache.commons.lang3.StringUtils.EMPTY in project bisq-api by mrosseel.

the class BisqApiWithUI method start.

@SuppressWarnings("PointlessBooleanExpression")
@Override
public void start(Stage stage) throws IOException {
    BisqApiWithUI.primaryStage = stage;
    String logPath = Paths.get(bisqEnvironment.getProperty(AppOptionKeys.APP_DATA_DIR_KEY), "bisq").toString();
    Log.setup(logPath);
    log.info("Log files under: " + logPath);
    Utilities.printSysInfo();
    Log.setLevel(Level.toLevel(bisqEnvironment.getRequiredProperty(CommonOptionKeys.LOG_LEVEL_KEY)));
    UserThread.setExecutor(Platform::runLater);
    UserThread.setTimerClass(UITimer.class);
    shutDownHandler = this::stop;
    BisqApp.shutDownHandler = shutDownHandler;
    // setup UncaughtExceptionHandler
    Thread.UncaughtExceptionHandler handler = (thread, throwable) -> {
        // Might come from another thread
        if (throwable.getCause() != null && throwable.getCause().getCause() != null && throwable.getCause().getCause() instanceof BlockStoreException) {
            log.error(throwable.getMessage());
        } else if (throwable instanceof ClassCastException && "sun.awt.image.BufImgSurfaceData cannot be cast to sun.java2d.xr.XRSurfaceData".equals(throwable.getMessage())) {
            log.warn(throwable.getMessage());
        } else {
            log.error("Uncaught Exception from thread " + Thread.currentThread().getName());
            log.error("throwableMessage= " + throwable.getMessage());
            log.error("throwableClass= " + throwable.getClass());
            log.error("Stack trace:\n" + ExceptionUtils.getStackTrace(throwable));
            throwable.printStackTrace();
            UserThread.execute(() -> showErrorPopup(throwable, false));
        }
    };
    Thread.setDefaultUncaughtExceptionHandler(handler);
    Thread.currentThread().setUncaughtExceptionHandler(handler);
    try {
        Utilities.checkCryptoPolicySetup();
    } catch (NoSuchAlgorithmException | LimitedKeyStrengthException e) {
        e.printStackTrace();
        UserThread.execute(() -> showErrorPopup(e, true));
    }
    Security.addProvider(new BouncyCastleProvider());
    final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
    final String currencyCode = baseCurrencyNetwork.getCurrencyCode();
    Res.setBaseCurrencyCode(currencyCode);
    Res.setBaseCurrencyName(baseCurrencyNetwork.getCurrencyName());
    CurrencyUtil.setBaseCurrencyCode(currencyCode);
    try {
        // Guice
        bisqAppModule = new BisqAppModule(bisqEnvironment, primaryStage);
        injector = Guice.createInjector(bisqAppModule);
        injector.getInstance(InjectorViewFactory.class).setInjector(injector);
        injector.getInstance(BisqApiApplication.class).run("server", "bisq-api.yml");
        // All classes which are persisting objects need to be added here
        // Maintain order!
        ArrayList<PersistedDataHost> persistedDataHosts = new ArrayList<>();
        persistedDataHosts.add(injector.getInstance(Preferences.class));
        persistedDataHosts.add(injector.getInstance(User.class));
        persistedDataHosts.add(injector.getInstance(Navigation.class));
        persistedDataHosts.add(injector.getInstance(AddressEntryList.class));
        persistedDataHosts.add(injector.getInstance(TradeManager.class));
        persistedDataHosts.add(injector.getInstance(OpenOfferManager.class));
        persistedDataHosts.add(injector.getInstance(TradeManager.class));
        persistedDataHosts.add(injector.getInstance(ClosedTradableManager.class));
        persistedDataHosts.add(injector.getInstance(FailedTradesManager.class));
        persistedDataHosts.add(injector.getInstance(DisputeManager.class));
        persistedDataHosts.add(injector.getInstance(P2PService.class));
        persistedDataHosts.add(injector.getInstance(VotingManager.class));
        persistedDataHosts.add(injector.getInstance(CompensationRequestManager.class));
        // we apply at startup the reading of persisted data but don't want to get it triggered in the constructor
        persistedDataHosts.stream().forEach(e -> {
            try {
                log.debug("call readPersisted at " + e.getClass().getSimpleName());
                e.readPersisted();
            } catch (Throwable e1) {
                log.error("readPersisted error", e1);
            }
        });
        Version.setBaseCryptoNetworkId(BisqEnvironment.getBaseCurrencyNetwork().ordinal());
        Version.printVersion();
        if (Utilities.isLinux())
            System.setProperty("prism.lcdtext", "false");
        Storage.setDatabaseCorruptionHandler((String fileName) -> {
            corruptedDatabaseFiles.add(fileName);
            if (mainView != null)
                mainView.setPersistedFilesCorrupted(corruptedDatabaseFiles);
        });
        // load the main view and create the main scene
        CachingViewLoader viewLoader = injector.getInstance(CachingViewLoader.class);
        mainView = (MainView) viewLoader.load(MainView.class);
        mainView.setPersistedFilesCorrupted(corruptedDatabaseFiles);
        /* Storage.setDatabaseCorruptionHandler((String fileName) -> {
                corruptedDatabaseFiles.add(fileName);
                if (mainView != null)
                    mainView.setPersistedFilesCorrupted(corruptedDatabaseFiles);
            });*/
        // 740
        scene = new Scene(mainView.getRoot(), 1200, 710);
        Font.loadFont(getClass().getResource("/fonts/Verdana.ttf").toExternalForm(), 13);
        Font.loadFont(getClass().getResource("/fonts/VerdanaBold.ttf").toExternalForm(), 13);
        Font.loadFont(getClass().getResource("/fonts/VerdanaItalic.ttf").toExternalForm(), 13);
        Font.loadFont(getClass().getResource("/fonts/VerdanaBoldItalic.ttf").toExternalForm(), 13);
        scene.getStylesheets().setAll("/io/bisq/gui/bisq.css", "/io/bisq/gui/images.css", "/io/bisq/gui/CandleStickChart.css");
        // configure the system tray
        SystemTray.create(primaryStage, shutDownHandler);
        primaryStage.setOnCloseRequest(event -> {
            event.consume();
            stop();
        });
        scene.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
            Utilities.isAltOrCtrlPressed(KeyCode.W, keyEvent);
            if (Utilities.isCtrlPressed(KeyCode.W, keyEvent) || Utilities.isCtrlPressed(KeyCode.Q, keyEvent)) {
                stop();
            } else {
                if (Utilities.isAltOrCtrlPressed(KeyCode.E, keyEvent)) {
                    showEmptyWalletPopup(injector.getInstance(BtcWalletService.class));
                } else if (Utilities.isAltOrCtrlPressed(KeyCode.M, keyEvent)) {
                    showSendAlertMessagePopup();
                } else if (Utilities.isAltOrCtrlPressed(KeyCode.F, keyEvent)) {
                    showFilterPopup();
                } else if (Utilities.isAltOrCtrlPressed(KeyCode.J, keyEvent)) {
                    WalletsManager walletsManager = injector.getInstance(WalletsManager.class);
                    if (walletsManager.areWalletsAvailable())
                        new ShowWalletDataWindow(walletsManager).show();
                    else
                        new Popup<>().warning(Res.get("popup.warning.walletNotInitialized")).show();
                } else if (Utilities.isAltOrCtrlPressed(KeyCode.G, keyEvent)) {
                    if (injector.getInstance(BtcWalletService.class).isWalletReady())
                        injector.getInstance(ManualPayoutTxWindow.class).show();
                    else
                        new Popup<>().warning(Res.get("popup.warning.walletNotInitialized")).show();
                } else if (DevEnv.DEV_MODE) {
                    // dev ode only
                    if (Utilities.isAltOrCtrlPressed(KeyCode.B, keyEvent)) {
                        // BSQ empty wallet not public yet
                        showEmptyWalletPopup(injector.getInstance(BsqWalletService.class));
                    } else if (Utilities.isAltOrCtrlPressed(KeyCode.P, keyEvent)) {
                        showFPSWindow();
                    } else if (Utilities.isAltOrCtrlPressed(KeyCode.Z, keyEvent)) {
                        showDebugWindow();
                    }
                }
            }
        });
        // configure the primary stage
        primaryStage.setTitle(bisqEnvironment.getRequiredProperty(AppOptionKeys.APP_NAME_KEY));
        primaryStage.setScene(scene);
        primaryStage.setMinWidth(1020);
        primaryStage.setMinHeight(620);
        // on windows the title icon is also used as task bar icon in a larger size
        // on Linux no title icon is supported but also a large task bar icon is derived from that title icon
        String iconPath;
        if (Utilities.isOSX())
            iconPath = ImageUtil.isRetina() ? "/images/window_icon@2x.png" : "/images/window_icon.png";
        else if (Utilities.isWindows())
            iconPath = "/images/task_bar_icon_windows.png";
        else
            iconPath = "/images/task_bar_icon_linux.png";
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(iconPath)));
        // make the UI visible
        primaryStage.show();
        if (!Utilities.isCorrectOSArchitecture()) {
            String osArchitecture = Utilities.getOSArchitecture();
            // We don't force a shutdown as the osArchitecture might in strange cases return a wrong value.
            // Needs at least more testing on different machines...
            new Popup<>().warning(Res.get("popup.warning.wrongVersion", osArchitecture, Utilities.getJVMArchitecture(), osArchitecture)).show();
        }
        UserThread.runPeriodically(() -> Profiler.printSystemLoad(log), LOG_MEMORY_PERIOD_MIN, TimeUnit.MINUTES);
    } catch (Throwable throwable) {
        log.error("Error during app init", throwable);
        showErrorPopup(throwable, false);
    }
}
Also used : StageStyle(javafx.stage.StageStyle) Version(io.bisq.common.app.Version) Key(com.google.inject.Key) User(io.bisq.core.user.User) ImageUtil(io.bisq.gui.util.ImageUtil) LoggerFactory(org.slf4j.LoggerFactory) UserThread(io.bisq.common.UserThread) Security(java.security.Security) StackPane(javafx.scene.layout.StackPane) Application(javafx.application.Application) Parent(javafx.scene.Parent) ClosedTradableManager(io.bisq.core.trade.closed.ClosedTradableManager) ResultHandler(io.bisq.common.handlers.ResultHandler) DisputeManager(io.bisq.core.arbitration.DisputeManager) CurrencyUtil(io.bisq.common.locale.CurrencyUtil) BlockStoreException(org.bitcoinj.store.BlockStoreException) Pane(javafx.scene.layout.Pane) AddressEntryList(io.bisq.core.btc.AddressEntryList) Font(javafx.scene.text.Font) Res(io.bisq.common.locale.Res) KeyEvent(javafx.scene.input.KeyEvent) UITimer(io.bisq.gui.common.UITimer) CommonOptionKeys(io.bisq.common.CommonOptionKeys) Platform(javafx.application.Platform) List(java.util.List) BisqAppModule(io.bisq.gui.app.BisqAppModule) Logger(ch.qos.logback.classic.Logger) OpenOfferManager(io.bisq.core.offer.OpenOfferManager) InjectorViewFactory(io.bisq.gui.common.view.guice.InjectorViewFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EventStreams(org.reactfx.EventStreams) Storage(io.bisq.common.storage.Storage) VotingManager(io.bisq.core.dao.vote.VotingManager) io.bisq.core.btc.wallet(io.bisq.core.btc.wallet) SystemTray(io.bisq.gui.SystemTray) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) TradeManager(io.bisq.core.trade.TradeManager) Scene(javafx.scene.Scene) BaseCurrencyNetwork(io.bisq.core.btc.BaseCurrencyNetwork) ArbitratorManager(io.bisq.core.arbitration.ArbitratorManager) Preferences(io.bisq.core.user.Preferences) io.bisq.gui.main.overlays.windows(io.bisq.gui.main.overlays.windows) CompensationRequestManager(io.bisq.core.dao.compensation.CompensationRequestManager) ArrayList(java.util.ArrayList) AlertManager(io.bisq.core.alert.AlertManager) FilterManager(io.bisq.core.filter.FilterManager) FailedTradesManager(io.bisq.core.trade.failed.FailedTradesManager) MainView(io.bisq.gui.main.MainView) LimitedKeyStrengthException(io.bisq.common.crypto.LimitedKeyStrengthException) KeyCode(javafx.scene.input.KeyCode) Modality(javafx.stage.Modality) BisqEnvironment(io.bisq.core.app.BisqEnvironment) Label(javafx.scene.control.Label) Profiler(io.bisq.common.util.Profiler) ViewLoader(io.bisq.gui.common.view.ViewLoader) Popup(io.bisq.gui.main.overlays.popups.Popup) AppOptionKeys(io.bisq.core.app.AppOptionKeys) IOException(java.io.IOException) Names(com.google.inject.name.Names) View(io.bisq.gui.common.view.View) Log(io.bisq.common.app.Log) BisqApiApplication(io.bisq.api.service.BisqApiApplication) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) Navigation(io.bisq.gui.Navigation) Level(ch.qos.logback.classic.Level) P2PService(io.bisq.network.p2p.P2PService) CachingViewLoader(io.bisq.gui.common.view.CachingViewLoader) Stage(javafx.stage.Stage) Utilities(io.bisq.common.util.Utilities) BisqApp(io.bisq.gui.app.BisqApp) Paths(java.nio.file.Paths) DevEnv(io.bisq.common.app.DevEnv) PersistedDataHost(io.bisq.common.proto.persistable.PersistedDataHost) Guice(com.google.inject.Guice) DebugView(io.bisq.gui.main.debug.DebugView) Image(javafx.scene.image.Image) TradeManager(io.bisq.core.trade.TradeManager) User(io.bisq.core.user.User) Platform(javafx.application.Platform) DisputeManager(io.bisq.core.arbitration.DisputeManager) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) P2PService(io.bisq.network.p2p.P2PService) Image(javafx.scene.image.Image) BisqApiApplication(io.bisq.api.service.BisqApiApplication) CachingViewLoader(io.bisq.gui.common.view.CachingViewLoader) CompensationRequestManager(io.bisq.core.dao.compensation.CompensationRequestManager) LimitedKeyStrengthException(io.bisq.common.crypto.LimitedKeyStrengthException) Popup(io.bisq.gui.main.overlays.popups.Popup) AddressEntryList(io.bisq.core.btc.AddressEntryList) Preferences(io.bisq.core.user.Preferences) InjectorViewFactory(io.bisq.gui.common.view.guice.InjectorViewFactory) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) FailedTradesManager(io.bisq.core.trade.failed.FailedTradesManager) Navigation(io.bisq.gui.Navigation) BlockStoreException(org.bitcoinj.store.BlockStoreException) VotingManager(io.bisq.core.dao.vote.VotingManager) Scene(javafx.scene.Scene) UserThread(io.bisq.common.UserThread) OpenOfferManager(io.bisq.core.offer.OpenOfferManager) BisqAppModule(io.bisq.gui.app.BisqAppModule) PersistedDataHost(io.bisq.common.proto.persistable.PersistedDataHost) BaseCurrencyNetwork(io.bisq.core.btc.BaseCurrencyNetwork) ClosedTradableManager(io.bisq.core.trade.closed.ClosedTradableManager)

Example 84 with EMPTY

use of org.apache.commons.lang3.StringUtils.EMPTY in project magnolia-vanity-url by aperto.

the class VanityUrlService method createImageLink.

/**
 * Create the link to the qr image without context path.
 *
 * @param node vanity url node
 * @return link to qr image
 */
public String createImageLink(final Node node) {
    String link = EMPTY;
    try {
        if (node != null && node.hasNode(NN_IMAGE)) {
            link = getLinkFromNode(node.getNode(NN_IMAGE), false);
            link = removeStart(defaultString(link), _contextPath);
            link = replace(link, "." + DEFAULT_EXTENSION, IMAGE_EXTENSION);
        }
    } catch (RepositoryException e) {
        LOGGER.error("Error creating link to image property.", e);
    }
    return link;
}
Also used : RepositoryException(javax.jcr.RepositoryException) PropertyUtil.getString(info.magnolia.jcr.util.PropertyUtil.getString) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString)

Example 85 with EMPTY

use of org.apache.commons.lang3.StringUtils.EMPTY in project knime-core by knime.

the class NodeModel method executeModel.

/**
 * Invokes the abstract <code>#execute()</code> method of this model. In
 * addition, this method notifies all assigned views of the model about the
 * changes.
 *
 * @param rawData An array of <code>PortObject</code> objects holding the data
 *            from the inputs (includes flow variable port).
 * @param exEnv The execution environment used for execution of this model.
 * @param exec The execution monitor which is passed to the execute method
 *            of this model.
 * @return The result of the execution in form of an array with
 *         <code>DataTable</code> elements, as many as the node has
 *         outputs.
 * @throws Exception any exception or error that is fired in the derived
 *             model will be just forwarded. It may throw an
 *             CanceledExecutionException if the user pressed cancel during
 *             execution. Even if the derived model doesn't check, the
 *             result will be discarded and the exception thrown.
 * @throws IllegalStateException If the number of <code>PortObject</code>
 *             objects returned by the derived <code>NodeModel</code>
 *             does not match the number of outputs. Or if any of them is
 *             null.
 * @see #execute(PortObject[],ExecutionContext)
 * @since 2.8
 * @noreference This method is not intended to be referenced by clients
 *              (use Node class instead)
 */
PortObject[] executeModel(final PortObject[] rawData, final ExecutionEnvironment exEnv, final ExecutionContext exec) throws Exception {
    final PortObject[] data = ArrayUtils.remove(rawData, 0);
    assert (data != null && data.length == getNrInPorts());
    assert (exec != null);
    setWarningMessage(null);
    // check for compatible input PortObjects
    for (int i = 0; i < data.length; i++) {
        PortType thisType = getInPortType(i);
        if (thisType.isOptional() && data[i] == null) {
        // ignore non-populated optional input
        } else if (data[i] instanceof InactiveBranchPortObject) {
            assert this instanceof InactiveBranchConsumer;
        // allow Inactive POs at InactiveBranchConsumer
        } else if (!(thisType.getPortObjectClass().isInstance(data[i]))) {
            m_logger.error("  (Wanted: " + thisType.getPortObjectClass().getName() + ", " + "actual: " + data[i].getClass().getName() + ")");
            throw new IllegalStateException("Connection Error: Mismatch" + " of input port types (port " + (i) + ").");
        }
    }
    // temporary storage for result of derived model.
    // EXECUTE DERIVED MODEL
    PortObject[] outData;
    if (!exEnv.reExecute()) {
        outData = execute(data, exec);
    } else {
        // FIXME: implement reexecution with loading view content and execute
        if (this instanceof InteractiveNode) {
            InteractiveNode iThis = (InteractiveNode) this;
            ViewContent viewContent = exEnv.getPreExecuteViewContent();
            iThis.loadViewValue(viewContent, exEnv.getUseAsDefault());
            outData = execute(data, exec);
        } else if (this instanceof LoopStartNode) {
            outData = execute(data, exec);
        } else {
            m_logger.coding("Cannot re-execute non interactive node. Using normal execute instead.");
            outData = execute(data, exec);
        }
    }
    // if execution was canceled without exception flying return false
    if (exec.isCanceled()) {
        throw new CanceledExecutionException("Result discarded due to user cancel");
    }
    if (outData == null) {
        outData = new PortObject[getNrOutPorts()];
    }
    /* Cleanup operation for nodes that just pass on their input
         * data table. We need to wrap those here so that the framework
         * explicitly references them (instead of copying) */
    for (int i = 0; i < outData.length; i++) {
        if (outData[i] instanceof BufferedDataTable) {
            for (int j = 0; j < data.length; j++) {
                if (outData[i] == data[j]) {
                    outData[i] = exec.createWrappedTable((BufferedDataTable) data[j]);
                }
            }
        } else if (outData[i] instanceof FileStorePortObject) {
            // file stores can be 'external', e.g. when a model reader node reads an external model file
            FileStorePortObject fsPO = (FileStorePortObject) outData[i];
            FileStoreHandlerRepository expectedRep = exec.getFileStoreHandler().getFileStoreHandlerRepository();
            FileStoreHandlerRepository actualRep = FileStoreUtil.getFileStores(fsPO).stream().map(FileStoreUtil::getFileStoreHandler).map(h -> h.getFileStoreHandlerRepository()).findFirst().orElse(expectedRep);
            if (actualRep != expectedRep) {
                outData[i] = Node.copyPortObject(fsPO, exec);
            }
        }
    }
    // if number of out tables does not match: fail
    if (outData.length != getNrOutPorts()) {
        throw new IllegalStateException("Invalid result. Execution failed. " + "Reason: Incorrect implementation; the execute" + " method in " + this.getClass().getSimpleName() + " returned null or an incorrect number of output" + " tables.");
    }
    // check the result, data tables must not be null
    for (int i = 0; i < outData.length; i++) {
        // of a loop and another loop iteration is requested
        if ((getLoopContext() == null) && (outData[i] == null)) {
            m_logger.error("Execution failed: Incorrect implementation;" + " the execute method in " + this.getClass().getSimpleName() + "returned a null data table at port: " + i);
            throw new IllegalStateException("Invalid result. " + "Execution failed, reason: data at output " + i + " is null.");
        }
    }
    // - only if the execute didn't issue a warning already
    if ((m_warningMessage == null) || (m_warningMessage.length() == 0)) {
        boolean hasData = false;
        // number of BDT ports
        int bdtPortCount = 0;
        for (int i = 0; i < outData.length; i++) {
            if (outData[i] instanceof BufferedDataTable) {
                // do some sanity checks on PortObjects holding data tables
                bdtPortCount += 1;
                BufferedDataTable outDataTable = (BufferedDataTable) outData[i];
                if (outDataTable.size() > 0) {
                    hasData = true;
                } else {
                    m_logger.info("The result table at port " + i + " contains no rows");
                }
            }
        }
        if (!hasData && bdtPortCount > 0) {
            if (bdtPortCount == 1) {
                setWarningMessage("Node created an empty data table.");
            } else {
                setWarningMessage("Node created empty data tables on all out-ports.");
            }
        }
    }
    setHasContent(true);
    PortObject[] rawOutData = new PortObject[getNrOutPorts() + 1];
    rawOutData[0] = FlowVariablePortObject.INSTANCE;
    System.arraycopy(outData, 0, rawOutData, 1, outData.length);
    return rawOutData;
}
Also used : Arrays(java.util.Arrays) FileStoreUtil(org.knime.core.data.filestore.FileStoreUtil) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) ExecutionEnvironment(org.knime.core.node.workflow.ExecutionEnvironment) Map(java.util.Map) FlowTryCatchContext(org.knime.core.node.workflow.FlowTryCatchContext) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) PortType(org.knime.core.node.port.PortType) PortInput(org.knime.core.node.streamable.PortInput) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) PortObjectOutput(org.knime.core.node.streamable.PortObjectOutput) InputPortRole(org.knime.core.node.streamable.InputPortRole) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) PortObject(org.knime.core.node.port.PortObject) RowOutput(org.knime.core.node.streamable.RowOutput) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DataTableSpec(org.knime.core.data.DataTableSpec) LoopEndNode(org.knime.core.node.workflow.LoopEndNode) FlowVariable(org.knime.core.node.workflow.FlowVariable) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LinkedHashMap(java.util.LinkedHashMap) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) NoSuchElementException(java.util.NoSuchElementException) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) FlowObjectStack(org.knime.core.node.workflow.FlowObjectStack) ScopeStartNode(org.knime.core.node.workflow.ScopeStartNode) RowInput(org.knime.core.node.streamable.RowInput) InteractiveView(org.knime.core.node.interactive.InteractiveView) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowScopeContext(org.knime.core.node.workflow.FlowScopeContext) IOException(java.io.IOException) InactiveBranchConsumer(org.knime.core.node.port.inactive.InactiveBranchConsumer) OutputPortRole(org.knime.core.node.streamable.OutputPortRole) NodeContext(org.knime.core.node.workflow.NodeContext) File(java.io.File) ViewContent(org.knime.core.node.interactive.ViewContent) PortOutput(org.knime.core.node.streamable.PortOutput) ICredentials(org.knime.core.node.workflow.ICredentials) LoopStartNode(org.knime.core.node.workflow.LoopStartNode) FlowLoopContext(org.knime.core.node.workflow.FlowLoopContext) Collections(java.util.Collections) MergeOperator(org.knime.core.node.streamable.MergeOperator) ViewableModel(org.knime.core.node.AbstractNodeView.ViewableModel) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) InactiveBranchConsumer(org.knime.core.node.port.inactive.InactiveBranchConsumer) FileStoreUtil(org.knime.core.data.filestore.FileStoreUtil) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) ViewContent(org.knime.core.node.interactive.ViewContent) LoopStartNode(org.knime.core.node.workflow.LoopStartNode) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortType(org.knime.core.node.port.PortType)

Aggregations

List (java.util.List)44 Map (java.util.Map)42 ArrayList (java.util.ArrayList)41 StringUtils (org.apache.commons.lang3.StringUtils)38 Collectors (java.util.stream.Collectors)37 HashMap (java.util.HashMap)33 IOException (java.io.IOException)27 Set (java.util.Set)25 HashSet (java.util.HashSet)22 LoggerFactory (org.slf4j.LoggerFactory)22 Pair (org.apache.commons.lang3.tuple.Pair)20 Logger (org.slf4j.Logger)20 Optional (java.util.Optional)19 Collections (java.util.Collections)17 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)17 java.util (java.util)15 Arrays.asList (java.util.Arrays.asList)14 Collection (java.util.Collection)14 Stream (java.util.stream.Stream)14 Arrays (java.util.Arrays)12