Search in sources :

Example 61 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class TestSiteToSiteStatusReportingTask method initTask.

public MockSiteToSiteStatusReportingTask initTask(Map<PropertyDescriptor, String> customProperties, ProcessGroupStatus pgStatus) throws InitializationException {
    final MockSiteToSiteStatusReportingTask task = new MockSiteToSiteStatusReportingTask();
    Map<PropertyDescriptor, String> properties = new HashMap<>();
    for (final PropertyDescriptor descriptor : task.getSupportedPropertyDescriptors()) {
        properties.put(descriptor, descriptor.getDefaultValue());
    }
    properties.putAll(customProperties);
    context = Mockito.mock(ReportingContext.class);
    Mockito.when(context.getStateManager()).thenReturn(new MockStateManager(task));
    Mockito.doAnswer(new Answer<PropertyValue>() {

        @Override
        public PropertyValue answer(final InvocationOnMock invocation) throws Throwable {
            final PropertyDescriptor descriptor = invocation.getArgumentAt(0, PropertyDescriptor.class);
            return new MockPropertyValue(properties.get(descriptor));
        }
    }).when(context).getProperty(Mockito.any(PropertyDescriptor.class));
    final EventAccess eventAccess = Mockito.mock(EventAccess.class);
    Mockito.when(context.getEventAccess()).thenReturn(eventAccess);
    Mockito.when(eventAccess.getControllerStatus()).thenReturn(pgStatus);
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class);
    Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(initContext.getLogger()).thenReturn(logger);
    task.initialize(initContext);
    return task;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) JsonString(javax.json.JsonString) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) MockStateManager(org.apache.nifi.state.MockStateManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 62 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class RangerNiFiAuthorizer method getConfigValue.

private String getConfigValue(final AuthorizerConfigurationContext context, final String name, final String defaultValue) {
    final PropertyValue configValue = context.getProperty(name);
    String retValue = defaultValue;
    if (configValue != null && !StringUtils.isBlank(configValue.getValue())) {
        retValue = configValue.getValue();
    }
    return retValue;
}
Also used : PropertyValue(org.apache.nifi.components.PropertyValue)

Example 63 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class NotificationServiceManager method createService.

/**
 * Creates a Notification Service and initializes it. Then returns the service and its configured properties
 *
 * @param serviceElement the XML element from which to build the Notification Service
 * @return a Tuple with the NotificationService as the key and the configured properties as the value, or <code>null</code> if
 *         unable to create the service
 */
private ConfiguredNotificationService createService(final Element serviceElement) {
    final Element idElement = getChild(serviceElement, "id");
    if (idElement == null) {
        logger.error("Found configuration for Notification Service with no 'id' element; this service cannot be referenced so it will not be loaded");
        return null;
    }
    final String serviceId = idElement.getTextContent().trim();
    logger.debug("Loading Notification Service with ID {}", serviceId);
    final Element classElement = getChild(serviceElement, "class");
    if (classElement == null) {
        logger.error("Found configuration for Notification Service with no 'class' element; Service ID is '{}'. This service annot be loaded", serviceId);
        return null;
    }
    final String className = classElement.getTextContent().trim();
    final Class<?> clazz;
    try {
        clazz = Class.forName(className);
    } catch (final Exception e) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but could not load class.", serviceId, className);
        logger.error("", e);
        return null;
    }
    if (!NotificationService.class.isAssignableFrom(clazz)) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but class is not a Notification Service.", serviceId, className);
        return null;
    }
    final Object serviceObject;
    try {
        serviceObject = clazz.newInstance();
    } catch (final Exception e) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but could not instantiate Notification Service.", serviceId, className);
        logger.error("", e);
        return null;
    }
    final Map<String, String> propertyValues = new HashMap<>();
    final List<Element> propertyElements = getChildElementsByTagName(serviceElement, "property");
    for (final Element propertyElement : propertyElements) {
        final String propName = propertyElement.getAttribute("name");
        if (propName == null || propName.trim().isEmpty()) {
            logger.warn("Found configuration for Notification Service with ID '{}' that has property value configured but no name for the property.", serviceId);
            continue;
        }
        final String propValue = propertyElement.getTextContent().trim();
        propertyValues.put(propName, propValue);
    }
    final NotificationService service = (NotificationService) serviceObject;
    try {
        service.initialize(new NotificationInitializationContext() {

            @Override
            public PropertyValue getProperty(final PropertyDescriptor descriptor) {
                final String propName = descriptor.getName();
                String value = propertyValues.get(propName);
                if (value == null) {
                    value = descriptor.getDefaultValue();
                }
                return new StandardPropertyValue(value, null, variableRegistry);
            }

            @Override
            public Map<String, String> getAllProperties() {
                return Collections.unmodifiableMap(propertyValues);
            }

            @Override
            public String getIdentifier() {
                return serviceId;
            }
        });
    } catch (final Exception e) {
        logger.error("Failed to load Notification Service with ID '{}'", serviceId);
        logger.error("", e);
    }
    return new ConfiguredNotificationService(service, propertyValues);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NotificationInitializationContext(org.apache.nifi.bootstrap.notification.NotificationInitializationContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 64 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class ITConsumeKafka method validateConsumerRetainer.

@Test
public void validateConsumerRetainer() throws Exception {
    // skip if on windows
    assumeFalse(isWindowsEnvironment());
    final ConsumerPool consumerPool = mock(ConsumerPool.class);
    final ConsumeKafka processor = new ConsumeKafka() {

        @Override
        protected ConsumerPool createConsumerPool(ProcessContext context, ComponentLog log) {
            return consumerPool;
        }
    };
    final ComponentLog logger = mock(ComponentLog.class);
    final ProcessorInitializationContext initializationContext = mock(ProcessorInitializationContext.class);
    when(initializationContext.getLogger()).thenReturn(logger);
    processor.initialize(initializationContext);
    final ProcessContext processContext = mock(ProcessContext.class);
    final PropertyValue heartbeatInternalMsConfig = mock(PropertyValue.class);
    when(heartbeatInternalMsConfig.isSet()).thenReturn(true);
    when(heartbeatInternalMsConfig.asInteger()).thenReturn(100);
    when(processContext.getProperty(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG)).thenReturn(heartbeatInternalMsConfig);
    processor.onScheduled(processContext);
    // retainConsumers should be called at least 1 time if it passed longer than heartbeat interval milliseconds.
    Thread.sleep(200);
    verify(consumerPool, atLeast(1)).retainConsumers();
    processor.stopConnectionRetainer();
    // After stopping connection retainer, it shouldn't interact with consumerPool.
    Thread.sleep(200);
    verifyNoMoreInteractions(consumerPool);
}
Also used : PropertyValue(org.apache.nifi.components.PropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessContext(org.apache.nifi.processor.ProcessContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Test(org.junit.Test)

Example 65 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class RouteOnAttribute method onScheduled.

/**
 * When this processor is scheduled, update the dynamic properties into the map
 * for quick access during each onTrigger call
 * @param context ProcessContext used to retrieve dynamic properties
 */
@OnScheduled
public void onScheduled(final ProcessContext context) {
    final Map<Relationship, PropertyValue> newPropertyMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        getLogger().debug("Adding new dynamic property: {}", new Object[] { descriptor });
        newPropertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor));
    }
    this.propertyMap = newPropertyMap;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) PropertyValue(org.apache.nifi.components.PropertyValue) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

PropertyValue (org.apache.nifi.components.PropertyValue)73 HashMap (java.util.HashMap)29 Test (org.junit.Test)22 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)21 ComponentLog (org.apache.nifi.logging.ComponentLog)18 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)16 IOException (java.io.IOException)15 Map (java.util.Map)13 FlowFile (org.apache.nifi.flowfile.FlowFile)11 ProcessException (org.apache.nifi.processor.exception.ProcessException)11 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)11 ArrayList (java.util.ArrayList)9 SSLContext (javax.net.ssl.SSLContext)7 Relationship (org.apache.nifi.processor.Relationship)7 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)6 File (java.io.File)5 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)5 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)5 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5