Search in sources :

Example 1 with NotificationInitializationContext

use of org.apache.nifi.bootstrap.notification.NotificationInitializationContext 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)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)1 NotificationInitializationContext (org.apache.nifi.bootstrap.notification.NotificationInitializationContext)1 NotificationService (org.apache.nifi.bootstrap.notification.NotificationService)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 PropertyValue (org.apache.nifi.components.PropertyValue)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1