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

com.sap.cds.framework.spring.config.CdsRuntimeConfig Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/*********************************************************************
 * (C) 2019 SAP SE or an SAP affiliate company. All rights reserved. *
 *********************************************************************/
package com.sap.cds.framework.spring.config;

import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.function.Supplier;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.sap.cds.CdsDataStore;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.impl.ServiceImpl;
import com.sap.cds.services.impl.runtime.CdsRuntimeBuilderSPI;
import com.sap.cds.services.persistence.PersistenceService;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cds.services.runtime.CdsRuntimeBuilder;
import com.sap.cds.services.transaction.TransactionManagerFactory;

@Configuration
// TODO move to dedicated config, once property for enabled OData adapter is available via CdsProperties
@PropertySource("classpath:/odata.properties")
public class CdsRuntimeConfig {

	@Bean
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public CdsRuntime cdsRuntime(TransactionManagerFactory txMgrFactory, ApplicationContext context) throws Exception {

		CdsRuntimeBuilder builder = CdsRuntimeBuilder.builder().withTransactionManagerFactory(() -> txMgrFactory);

		Arrays.stream(context.getBeanNamesForType(EventHandler.class))
		// filter all beans which types have been duplicated through scoping by CGLIB
		.filter(name -> !name.startsWith("scopedTarget."))
		.map(context::getType).distinct().forEach((handlerClass) -> {
			// ignore EventHandler, which were already registered as service
			if(!ServiceImpl.class.isAssignableFrom(handlerClass)) {
				builder.withHandlerClass(handlerClass, (Supplier) () -> context.getBean(handlerClass));
			}
		});

		// set as default via internal SPI
		((CdsRuntimeBuilderSPI) builder).asDefault();
		return builder.build();
	}

	@Bean
	public CdsDataStore cdsDataStore(CdsRuntime runtime) {
		PersistenceService ps = runtime.getServiceCatalog().getService(PersistenceService.class, PersistenceService.DEFAULT_NAME);

		return (CdsDataStore) Proxy.newProxyInstance(CdsDataStore.class.getClassLoader(), new Class[] { CdsDataStore.class },
				(proxy, method, methodArgs) -> {
					// works, also if no request context is set
					return method.invoke(ps.getCdsDataStore(), methodArgs);
				});
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy