Java Code Examples for org.apache.nifi.controller.ControllerService#initialize()

The following examples show how to use org.apache.nifi.controller.ControllerService#initialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardProcessorTestRunner.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void addControllerService(final String identifier, final ControllerService service, final Map<String, String> properties) throws InitializationException {
    final MockComponentLog logger = new MockComponentLog(identifier, service);
    controllerServiceLoggers.put(identifier, logger);
    final MockStateManager serviceStateManager = new MockStateManager(service);
    final MockControllerServiceInitializationContext initContext = new MockControllerServiceInitializationContext(requireNonNull(service), requireNonNull(identifier), logger, serviceStateManager);
    controllerServiceStateManagers.put(identifier, serviceStateManager);
    initContext.addControllerServices(context);
    service.initialize(initContext);

    final Map<PropertyDescriptor, String> resolvedProps = new HashMap<>();
    for (final Map.Entry<String, String> entry : properties.entrySet()) {
        resolvedProps.put(service.getPropertyDescriptor(entry.getKey()), entry.getValue());
    }

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, service);
    } catch (final InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        throw new InitializationException(e);
    }

    context.addControllerService(identifier, service, resolvedProps, null);
}
 
Example 2
Source File: StandardProcessorTestRunner.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void addControllerService(final String identifier, final ControllerService service, final Map<String, String> properties) throws InitializationException {
    final MockComponentLog logger = new MockComponentLog(identifier, service);
    controllerServiceLoggers.put(identifier, logger);
    final MockStateManager serviceStateManager = new MockStateManager(service);
    final MockControllerServiceInitializationContext initContext = new MockControllerServiceInitializationContext(
            requireNonNull(service), requireNonNull(identifier), logger, serviceStateManager, kerberosContext);
    controllerServiceStateManagers.put(identifier, serviceStateManager);
    initContext.addControllerServices(context);
    service.initialize(initContext);

    final Map<PropertyDescriptor, String> resolvedProps = new HashMap<>();
    for (final Map.Entry<String, String> entry : properties.entrySet()) {
        resolvedProps.put(service.getPropertyDescriptor(entry.getKey()), entry.getValue());
    }

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, service);
    } catch (final InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        throw new InitializationException(e);
    }

    context.addControllerService(identifier, service, resolvedProps, null);
}
 
Example 3
Source File: StatelessControllerServiceLookup.java    From nifi with Apache License 2.0 6 votes vote down vote up
public void addControllerService(final ControllerService service, final String serviceName) throws InitializationException {
    final String identifier = service.getIdentifier();
    final SLF4JComponentLog logger = new SLF4JComponentLog(service);
    controllerServiceLoggers.put(identifier, logger);

    StatelessStateManager serviceStateManager = new StatelessStateManager();
    controllerServiceStateManagers.put(identifier, serviceStateManager);

    final StatelessProcessContext initContext = new StatelessProcessContext(requireNonNull(service), this, serviceName, logger, serviceStateManager, parameterContext);
    service.initialize(initContext);

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, service);
    } catch (final InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        throw new InitializationException(e);
    }

    final StatelessControllerServiceConfiguration config = new StatelessControllerServiceConfiguration(service, serviceName);
    controllerServiceMap.put(identifier, config);
}
 
Example 4
Source File: ControllerServiceInitializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
 
Example 5
Source File: ControllerServiceInitializer.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
 
Example 6
Source File: ControllerServiceInitializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
 
Example 7
Source File: AbstractDocumentationWriter.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected void initialize(final ControllerService service) throws InitializationException {
    service.initialize(new DocumentationControllerServiceInitializationContext());
}
 
Example 8
Source File: ComponentFactory.java    From nifi with Apache License 2.0 4 votes vote down vote up
private ControllerService createControllerService(final VersionedControllerService versionedControllerService, final VariableRegistry variableRegistry, final Set<URL> classpathUrls,
                                                  final ControllerServiceLookup serviceLookup, final StateManager stateManager, final ParameterLookup parameterLookup) {

    final String type = versionedControllerService.getType();
    final String identifier = versionedControllerService.getIdentifier();

    final Bundle bundle = getAvailableBundle(versionedControllerService.getBundle(), type);
    if (bundle == null) {
        throw new IllegalStateException("Unable to find bundle for coordinate "
            + versionedControllerService.getBundle().getGroup() + ":"
            + versionedControllerService.getBundle().getArtifact() + ":"
            + versionedControllerService.getBundle().getVersion());
    }

    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoader = extensionManager.createInstanceClassLoader(type, identifier, bundle,
            classpathUrls == null ? Collections.emptySet() : classpathUrls);

        logger.debug("Setting context class loader to {} (parent = {}) to create {}", detectedClassLoader, detectedClassLoader.getParent(), type);
        final Class<?> rawClass = Class.forName(type, true, detectedClassLoader);
        Thread.currentThread().setContextClassLoader(detectedClassLoader);

        final Object extensionInstance = rawClass.newInstance();
        final ComponentLog componentLog = new SLF4JComponentLog(extensionInstance);

        final ControllerService service = (ControllerService) extensionInstance;
        final ControllerServiceInitializationContext initializationContext = new StatelessControllerServiceInitializationContext(identifier, service, serviceLookup, stateManager);
        service.initialize(initializationContext);

        // If no classpath urls were provided, check if we need to add additional classpath URL's based on configured properties.
        if (classpathUrls == null) {
            final Set<URL> additionalClasspathUrls = getAdditionalClasspathResources(service.getPropertyDescriptors(), service.getIdentifier(), versionedControllerService.getProperties(),
                parameterLookup, variableRegistry, componentLog);

            if (!additionalClasspathUrls.isEmpty()) {
                return createControllerService(versionedControllerService, variableRegistry, additionalClasspathUrls, serviceLookup, stateManager, parameterLookup);
            }
        }

        return service;
    } catch (final Exception e) {
        throw new ControllerServiceInstantiationException(type, e);
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}
 
Example 9
Source File: HtmlDocumentationWriterTest.java    From localization_nifi with Apache License 2.0 3 votes vote down vote up
@Test
public void testControllerServiceWithLogger() throws InitializationException, IOException {

    ControllerService controllerService = new ControllerServiceWithLogger();
    controllerService.initialize(new MockControllerServiceInitializationContext());

    DocumentationWriter writer = new HtmlDocumentationWriter();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    writer.write(controllerService, baos, false);

    String results = new String(baos.toByteArray());
    XmlValidator.assertXmlValid(results);
}
 
Example 10
Source File: HtmlDocumentationWriterTest.java    From nifi with Apache License 2.0 3 votes vote down vote up
@Test
public void testControllerServiceWithLogger() throws InitializationException, IOException {

    ControllerService controllerService = new ControllerServiceWithLogger();
    controllerService.initialize(new MockControllerServiceInitializationContext());

    DocumentationWriter writer = new HtmlDocumentationWriter(extensionManager);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    writer.write(controllerService, baos, false);

    String results = new String(baos.toByteArray());
    XmlValidator.assertXmlValid(results);
}