All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.feature.messaging.mq.service.MessageQueuingServiceConfiguration Maven / Gradle / Ivy

The newest version!
/**************************************************************************
 * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.feature.messaging.mq.service;

import static com.sap.cds.services.messaging.utils.MessagingOutboxUtils.outboxed;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.cds.feature.messaging.mq.jms.MessageQueuingConnectionProvider;
import com.sap.cds.services.environment.CdsProperties.Messaging;
import com.sap.cds.services.environment.CdsProperties.Messaging.MessagingServiceConfig;
import com.sap.cds.services.runtime.CdsRuntimeConfiguration;
import com.sap.cds.services.runtime.CdsRuntimeConfigurer;
import com.sap.cds.services.utils.StringUtils;
import com.sap.cds.services.utils.environment.ServiceBindingUtils;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;

public class MessageQueuingServiceConfiguration implements CdsRuntimeConfiguration {

	private static final Logger logger = LoggerFactory.getLogger(MessageQueuingServiceConfiguration.class);

	private static final String BINDING_LABEL = "message-queuing";

	@Override
	public void services(CdsRuntimeConfigurer configurer) {
		Messaging config = configurer.getCdsRuntime().getEnvironment().getCdsProperties().getMessaging();
		List bindings = configurer.getCdsRuntime().getEnvironment().getServiceBindings()
				.filter(b -> ServiceBindingUtils.matches(b, null, BINDING_LABEL))
				.collect(Collectors.toList());

		if(bindings.isEmpty()) {
			logger.info("No service bindings with label '{}' found", BINDING_LABEL);
		} else {
			boolean isSingleBinding = bindings.size() == 1;
			bindings.forEach(binding -> {
				logger.debug("Starting the initialization of the message-queueing service binding '{}'", binding.getName().get());

				// MQ JMS connection provider
				MessageQueuingConnectionProvider connectionProvider = new MessageQueuingConnectionProvider(binding);

				// determines whether no configuration is available and the  default service should be created
				boolean createDefaultService = true;

				// check the services configured by binding
				List serviceConfigs = config.getServicesByBinding(binding.getName().get());

				if (!serviceConfigs.isEmpty()) {
					createDefaultService = false;
					serviceConfigs.forEach(serviceConfig -> {
						if (serviceConfig.isEnabled()) {
							MessageQueuingService service = new MessageQueuingService(serviceConfig, binding, connectionProvider, configurer.getCdsRuntime());
							// register the service
							configurer.service(outboxed(service, serviceConfig, configurer.getCdsRuntime()));
						}
					});
				}

				// check the services configured by kind if only one service binding is available
				List serviceConfigsByKind = config.getServicesByKind(BINDING_LABEL);

				if (isSingleBinding && !serviceConfigsByKind.isEmpty()) {
					createDefaultService = false;
					serviceConfigsByKind.forEach(serviceConfig -> {
						// check that the service is enabled and whether not already found by name or binding
						if (serviceConfig.isEnabled() && !serviceConfigs.stream().anyMatch(c -> c.getName().equals(serviceConfig.getName()))) {
							MessageQueuingService service = new MessageQueuingService(serviceConfig, binding, connectionProvider, configurer.getCdsRuntime());
							// register the service
							configurer.service(outboxed(service, serviceConfig, configurer.getCdsRuntime()));
						}
					});
				}

				if (createDefaultService) {
					// otherwise create default service instance for the binding
					MessagingServiceConfig defConfig = config.getService(binding.getName().get());
					if (StringUtils.isEmpty(defConfig.getBinding()) && StringUtils.isEmpty(defConfig.getKind())) {
						MessageQueuingService service = new MessageQueuingService(defConfig, binding, connectionProvider, configurer.getCdsRuntime());
						// register the service
						configurer.service(outboxed(service, defConfig, configurer.getCdsRuntime()));
					} else {
						logger.warn("Could not create service for binding '{}': A configuration with the same name is already defined for another kind or binding.", binding.getName().get());
					}
				}

				logger.debug("Finished the initialization of the message-queueing service binding '{}'", binding.getName().get());
			});
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy