Example usage for org.apache.commons.lang3 StringUtils substringAfterLast

List of usage examples for org.apache.commons.lang3 StringUtils substringAfterLast

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringAfterLast.

Prototype

public static String substringAfterLast(final String str, final String separator) 

Source Link

Document

Gets the substring after the last occurrence of a separator.

Usage

From source file:org.aliuge.crawler.extractor.selector.action.string.StringAfterLastAction.java

/**
 * ?content?separator?/* ww w  . j  av  a2 s. c  o  m*/
 */
@Override
public String doAction(String content) {
    if (StringUtils.isNotBlank(content)) {
        return StringUtils.substringAfterLast(content, separator);
    }
    return "";
}

From source file:org.apache.hadoop.hive.ql.log.TestSlidingFilenameRolloverStrategy.java

@Test
public void testSlidingLogFiles() throws Exception {
    assertEquals("bad props file", PROPERTIES_FILE, System.getProperty("log4j.configurationFile"));

    // Where the log files wll be written
    Path logTemplate = FileSystems.getDefault().getPath(FILE_PATTERN);
    String fileName = logTemplate.getFileName().toString();
    Path parent = logTemplate.getParent();
    try {// w ww  .j av a  2s  .  co  m
        Files.createDirectory(parent);
    } catch (FileAlreadyExistsException e) {
        // OK, fall through.
    }

    // Delete any stale log files left around from previous failed tests
    deleteLogFiles(parent, fileName);

    Logger logger = LogManager.getLogger(LineageLogger.class);

    // Does the logger config look correct?
    org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger;
    LoggerConfig loggerConfig = coreLogger.get();
    Map<String, Appender> appenders = loggerConfig.getAppenders();

    assertNotNull("sliding appender is missing", appenders.get("sliding"));

    // Do some logging and force log rollover
    int NUM_LOGS = 7;
    logger.debug("Debug Message Logged !!!");
    logger.info("Info Message Logged !!!");

    String errorString = "Error Message Logged ";
    for (int i = 0; i < NUM_LOGS; i++) {
        TimeUnit.MILLISECONDS.sleep(100);
        // log an exception - this produces enough text to force a new logfile
        // (as appender.sliding.policies.size.size=1KB)
        logger.error(errorString + i, new RuntimeException("part of a test"));
    }

    // Check log files look OK
    DirectoryStream<Path> stream = Files.newDirectoryStream(parent, fileName + ".*");
    int count = 0;
    for (Path path : stream) {
        count++;
        String contents = new String(Files.readAllBytes(path), "UTF-8");
        // There should be one exception message per file
        assertTrue("File " + path + " did not have expected content", contents.contains(errorString));
        String suffix = StringUtils.substringAfterLast(path.toString(), ".");
        // suffix should be a timestamp
        try {
            long timestamp = Long.parseLong(suffix);
        } catch (NumberFormatException e) {
            fail("Suffix " + suffix + " is not a long");
        }
    }
    assertEquals("bad count of log files", NUM_LOGS, count);

    // Check there is no log file without the suffix
    assertFalse("file should not exist:" + logTemplate, Files.exists(logTemplate));

    // Clean up
    deleteLogFiles(parent, fileName);
}

From source file:org.apache.maven.cli.transfer.ConsoleMavenTransferListener.java

private String getStatus(String resourceName, long complete, long total) {
    DecimalFormat format = new FileDecimalFormat(Locale.ENGLISH);
    StringBuilder status = new StringBuilder();

    if (printResourceNames) {
        status.append(StringUtils.substringAfterLast(resourceName, "/"));
        status.append(" (");
    }// ww  w . j  a  v a2 s. com

    status.append(format.format(complete));
    if (total > 0 && complete != total) {
        status.append("/").append(format.format(total));
    }

    if (printResourceNames) {
        status.append(")");
    }

    return status.toString();
}

From source file:org.apache.nifi.audit.SnippetAuditor.java

/**
 * Audits the specified snippet.//from   ww  w .j  a  v  a  2s .  co m
 */
private void auditSnippet(final FlowSnippetDTO snippet) {
    final Collection<Action> actions = new ArrayList<>();
    final Date timestamp = new Date();

    // input ports
    for (final PortDTO inputPort : snippet.getInputPorts()) {
        actions.add(generateAuditRecord(inputPort.getId(), inputPort.getName(), Component.InputPort,
                Operation.Add, timestamp));
    }

    // output ports
    for (final PortDTO outputPort : snippet.getOutputPorts()) {
        actions.add(generateAuditRecord(outputPort.getId(), outputPort.getName(), Component.OutputPort,
                Operation.Add, timestamp));
    }

    // remote processor groups
    for (final RemoteProcessGroupDTO remoteProcessGroup : snippet.getRemoteProcessGroups()) {
        FlowChangeRemoteProcessGroupDetails remoteProcessGroupDetails = new FlowChangeRemoteProcessGroupDetails();
        remoteProcessGroupDetails.setUri(remoteProcessGroup.getTargetUri());

        final FlowChangeAction action = generateAuditRecord(remoteProcessGroup.getId(),
                remoteProcessGroup.getName(), Component.RemoteProcessGroup, Operation.Add, timestamp);
        action.setComponentDetails(remoteProcessGroupDetails);
        actions.add(action);
    }

    // processor groups
    for (final ProcessGroupDTO processGroup : snippet.getProcessGroups()) {
        actions.add(generateAuditRecord(processGroup.getId(), processGroup.getName(), Component.ProcessGroup,
                Operation.Add, timestamp));
    }

    // processors
    for (final ProcessorDTO processor : snippet.getProcessors()) {
        final FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
        processorDetails.setType(StringUtils.substringAfterLast(processor.getType(), "."));

        final FlowChangeAction action = generateAuditRecord(processor.getId(), processor.getName(),
                Component.Processor, Operation.Add, timestamp);
        action.setComponentDetails(processorDetails);
        actions.add(action);
    }

    // funnels
    for (final FunnelDTO funnel : snippet.getFunnels()) {
        actions.add(generateAuditRecord(funnel.getId(), StringUtils.EMPTY, Component.Funnel, Operation.Add,
                timestamp));
    }

    // connections
    for (final ConnectionDTO connection : snippet.getConnections()) {
        final ConnectableDTO source = connection.getSource();
        final ConnectableDTO destination = connection.getDestination();

        // determine the relationships and connection name
        final String relationships = CollectionUtils.isEmpty(connection.getSelectedRelationships())
                ? StringUtils.EMPTY
                : StringUtils.join(connection.getSelectedRelationships(), ", ");
        final String name = StringUtils.isBlank(connection.getName()) ? relationships : connection.getName();

        // create the connect details
        FlowChangeConnectDetails connectDetails = new FlowChangeConnectDetails();
        connectDetails.setSourceId(source.getId());
        connectDetails.setSourceName(source.getName());
        connectDetails.setSourceType(determineConnectableType(source));
        connectDetails.setRelationship(relationships);
        connectDetails.setDestinationId(destination.getId());
        connectDetails.setDestinationName(destination.getName());
        connectDetails.setDestinationType(determineConnectableType(destination));

        // create the audit record
        final FlowChangeAction action = generateAuditRecord(connection.getId(), name, Component.Connection,
                Operation.Connect, timestamp);
        action.setActionDetails(connectDetails);
        actions.add(action);
    }

    // save the actions
    if (!actions.isEmpty()) {
        saveActions(actions, logger);
    }
}

From source file:org.apache.nifi.controller.ExtensionBuilder.java

private ProcessorNode createProcessorNode(final LoggableComponent<Processor> processor,
        final boolean creationSuccessful) {
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(
            this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(
            serviceProvider, componentVarRegistry);

    final ProcessorNode procNode;
    if (creationSuccessful) {
        procNode = new StandardProcessorNode(processor, identifier, validationContextFactory, processScheduler,
                serviceProvider, componentVarRegistry, reloadComponent, extensionManager, validationTrigger);
    } else {//from www  .java  2 s. com
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        procNode = new StandardProcessorNode(processor, identifier, validationContextFactory, processScheduler,
                serviceProvider, componentType, type, componentVarRegistry, reloadComponent, extensionManager,
                validationTrigger, true);
    }

    applyDefaultSettings(procNode);
    return procNode;
}

From source file:org.apache.nifi.controller.ExtensionBuilder.java

private ReportingTaskNode createReportingTaskNode(final LoggableComponent<ReportingTask> reportingTask,
        final boolean creationSuccessful) {
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(
            this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(
            serviceProvider, componentVarRegistry);
    final ReportingTaskNode taskNode;
    if (creationSuccessful) {
        taskNode = new StandardReportingTaskNode(reportingTask, identifier, flowController, processScheduler,
                validationContextFactory, componentVarRegistry, reloadComponent, extensionManager,
                validationTrigger);//  www . j a va  2 s. c o  m
        taskNode.setName(taskNode.getReportingTask().getClass().getSimpleName());
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;

        taskNode = new StandardReportingTaskNode(reportingTask, identifier, flowController, processScheduler,
                validationContextFactory, componentType, type, componentVarRegistry, reloadComponent,
                extensionManager, validationTrigger, true);
        taskNode.setName(componentType);
    }

    return taskNode;
}

From source file:org.apache.nifi.controller.ExtensionBuilder.java

private ControllerServiceNode createGhostControllerServiceNode() {
    final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
    final String componentType = "(Missing) " + simpleClassName;

    final GhostControllerService ghostService = new GhostControllerService(identifier, type);
    final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>(ghostService,
            bundleCoordinate, null);/*w w  w . j  a  v a2 s  .co m*/

    final ControllerServiceInvocationHandler invocationHandler = new StandardControllerServiceInvocationHandler(
            extensionManager, ghostService);

    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(
            this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(
            serviceProvider, variableRegistry);
    final ControllerServiceNode serviceNode = new StandardControllerServiceNode(proxiedLoggableComponent,
            proxiedLoggableComponent, invocationHandler, identifier, validationContextFactory, serviceProvider,
            componentType, type, componentVarRegistry, reloadComponent, extensionManager, validationTrigger,
            true);

    return serviceNode;
}

From source file:org.apache.nifi.controller.FlowController.java

/**
 * <p>/*from   www . j  av  a  2  s.  c o  m*/
 * Creates a new ProcessorNode with the given type and identifier and
 * optionally initializes it.
 * </p>
 *
 * @param type the fully qualified Processor class name
 * @param id the unique ID of the Processor
 * @param firstTimeAdded whether or not this is the first time this
 * Processor is added to the graph. If {@code true}, will invoke methods
 * annotated with the {@link OnAdded} annotation.
 * @return new processor node
 * @throws NullPointerException if either arg is null
 * @throws ProcessorInstantiationException if the processor cannot be
 * instantiated for any reason
 */
public ProcessorNode createProcessor(final String type, String id, final boolean firstTimeAdded)
        throws ProcessorInstantiationException {
    id = id.intern();

    boolean creationSuccessful;
    Processor processor;
    try {
        processor = instantiateProcessor(type, id);
        creationSuccessful = true;
    } catch (final ProcessorInstantiationException pie) {
        LOG.error("Could not create Processor of type " + type + " for ID " + id
                + "; creating \"Ghost\" implementation", pie);
        final GhostProcessor ghostProc = new GhostProcessor();
        ghostProc.setIdentifier(id);
        ghostProc.setCanonicalClassName(type);
        processor = ghostProc;
        creationSuccessful = false;
    }

    final ComponentLog logger = new SimpleProcessLogger(id, processor);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(
            controllerServiceProvider, variableRegistry);
    final ProcessorNode procNode;
    if (creationSuccessful) {
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler,
                controllerServiceProvider, nifiProperties, variableRegistry, logger);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler,
                controllerServiceProvider, componentType, type, nifiProperties, variableRegistry, logger);
    }

    final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
    logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN,
            new ProcessorLogObserver(getBulletinRepository(), procNode));

    try {
        final Class<?> procClass = processor.getClass();
        if (procClass.isAnnotationPresent(DefaultSettings.class)) {
            DefaultSettings ds = procClass.getAnnotation(DefaultSettings.class);
            try {
                procNode.setYieldPeriod(ds.yieldDuration());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting yield period from DefaultSettings annotation:%s",
                        ex.getMessage()), ex);
            }
            try {

                procNode.setPenalizationPeriod(ds.penaltyDuration());
            } catch (Throwable ex) {
                LOG.error(
                        String.format("Error while setting penalty duration from DefaultSettings annotation:%s",
                                ex.getMessage()),
                        ex);
            }
            try {
                procNode.setBulletinLevel(ds.bulletinLevel());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting bulletin level from DefaultSettings annotation:%s",
                        ex.getMessage()), ex);
            }

        }
    } catch (Throwable ex) {
        LOG.error(String.format("Error while setting default settings from DefaultSettings annotation: %s",
                ex.getMessage()), ex);
    }

    if (firstTimeAdded) {
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(processor.getClass(),
                processor.getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor);
        } catch (final Exception e) {
            logRepository.removeObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID);
            throw new ComponentLifeCycleException(
                    "Failed to invoke @OnAdded methods of " + procNode.getProcessor(), e);
        }

        if (firstTimeAdded) {
            try (final NarCloseable nc = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(),
                    processor.getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class,
                        procNode.getProcessor());
            }
        }
    }

    return procNode;
}

From source file:org.apache.nifi.controller.FlowController.java

public ReportingTaskNode createReportingTask(final String type, final String id, final boolean firstTimeAdded,
        final boolean register) throws ReportingTaskInstantiationException {
    if (type == null || id == null) {
        throw new NullPointerException();
    }//from   w  w w . j  av a2s  . c  o m

    ReportingTask task = null;
    boolean creationSuccessful = true;
    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoader = ExtensionManager.getClassLoader(type, id);
        final Class<?> rawClass;
        if (detectedClassLoader == null) {
            rawClass = Class.forName(type);
        } else {
            rawClass = Class.forName(type, false, detectedClassLoader);
        }

        Thread.currentThread().setContextClassLoader(detectedClassLoader);
        final Class<? extends ReportingTask> reportingTaskClass = rawClass.asSubclass(ReportingTask.class);
        final Object reportingTaskObj = reportingTaskClass.newInstance();
        task = reportingTaskClass.cast(reportingTaskObj);
    } catch (final Exception e) {
        LOG.error("Could not create Reporting Task of type " + type + " for ID " + id
                + "; creating \"Ghost\" implementation", e);
        final GhostReportingTask ghostTask = new GhostReportingTask();
        ghostTask.setIdentifier(id);
        ghostTask.setCanonicalClassName(type);
        task = ghostTask;
        creationSuccessful = false;
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }

    final ComponentLog logger = new SimpleProcessLogger(id, task);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(
            controllerServiceProvider, variableRegistry);
    final ReportingTaskNode taskNode;
    if (creationSuccessful) {
        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory,
                variableRegistry, logger);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;

        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory,
                componentType, type, variableRegistry, logger);
    }

    taskNode.setName(task.getClass().getSimpleName());

    if (firstTimeAdded) {
        final ComponentLog componentLog = new SimpleProcessLogger(id, taskNode.getReportingTask());
        final ReportingInitializationContext config = new StandardReportingInitializationContext(id,
                taskNode.getName(), SchedulingStrategy.TIMER_DRIVEN, "1 min", componentLog, this,
                nifiProperties);

        try {
            task.initialize(config);
        } catch (final InitializationException ie) {
            throw new ReportingTaskInstantiationException("Failed to initialize reporting task of type " + type,
                    ie);
        }

        try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(),
                taskNode.getReportingTask().getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, task);
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class,
                    taskNode.getReportingTask());
        } catch (final Exception e) {
            throw new ComponentLifeCycleException("Failed to invoke On-Added Lifecycle methods of " + task, e);
        }
    }

    if (register) {
        reportingTasks.put(id, taskNode);

        // Register log observer to provide bulletins when reporting task logs anything at WARN level or above
        final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
        logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN,
                new ReportingTaskLogObserver(getBulletinRepository(), taskNode));
    }

    return taskNode;
}

From source file:org.apache.nifi.controller.service.StandardControllerServiceProvider.java

private ControllerServiceNode createGhostControllerService(final String type, final String id) {
    final InvocationHandler invocationHandler = new InvocationHandler() {
        @Override/* w  w w  .  jav a 2s  .  com*/
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            final String methodName = method.getName();

            if ("validate".equals(methodName)) {
                final ValidationResult result = new ValidationResult.Builder().input("Any Property")
                        .subject("Missing Controller Service").valid(false)
                        .explanation(
                                "Controller Service could not be created because the Controller Service Type ("
                                        + type + ") could not be found")
                        .build();
                return Collections.singleton(result);
            } else if ("getPropertyDescriptor".equals(methodName)) {
                final String propertyName = (String) args[0];
                return new PropertyDescriptor.Builder().name(propertyName).description(propertyName)
                        .sensitive(true).required(true).build();
            } else if ("getPropertyDescriptors".equals(methodName)) {
                return Collections.emptyList();
            } else if ("onPropertyModified".equals(methodName)) {
                return null;
            } else if ("getIdentifier".equals(methodName)) {
                return id;
            } else if ("toString".equals(methodName)) {
                return "GhostControllerService[id=" + id + ", type=" + type + "]";
            } else if ("hashCode".equals(methodName)) {
                return 91 * type.hashCode() + 41 * id.hashCode();
            } else if ("equals".equals(methodName)) {
                return proxy == args[0];
            } else {
                throw new IllegalStateException(
                        "Controller Service could not be created because the Controller Service Type (" + type
                                + ") could not be found");
            }
        }
    };

    final ControllerService proxiedService = (ControllerService) Proxy.newProxyInstance(
            getClass().getClassLoader(), new Class[] { ControllerService.class }, invocationHandler);

    final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
    final String componentType = "(Missing) " + simpleClassName;

    final ComponentLog logger = new SimpleProcessLogger(id, proxiedService);

    final ControllerServiceNode serviceNode = new StandardControllerServiceNode(proxiedService, proxiedService,
            id, new StandardValidationContextFactory(this, variableRegistry), this, componentType, type,
            variableRegistry, logger);
    return serviceNode;
}