com.microsoft.azure.spring.data.documentdb.config.DocumentDbConfigurationSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-documentdb Show documentation
Show all versions of spring-data-documentdb Show documentation
Spring Data for Azure Cosmos DB DocumentDB API
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for
* license information.
*/
package com.microsoft.azure.spring.data.documentdb.config;
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentDbMappingContext;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.context.MappingContextIsNewStrategyFactory;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.support.CachingIsNewStrategyFactory;
import org.springframework.data.support.IsNewStrategyFactory;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import java.util.*;
public abstract class DocumentDbConfigurationSupport {
@Bean
public DocumentDbMappingContext documentDbMappingContext() throws ClassNotFoundException {
final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext();
mappingContext.setInitialEntitySet(getInitialEntitySet());
return mappingContext;
}
protected Collection getMappingBasePackages() {
final Package mappingBasePackage = getClass().getPackage();
return Collections.singleton(mappingBasePackage == null ? null : mappingBasePackage.getName());
}
@Bean
public IsNewStrategyFactory isNewStrategyFactory() throws ClassNotFoundException {
return new CachingIsNewStrategyFactory(new MappingContextIsNewStrategyFactory(
new PersistentEntities(Arrays.>asList(
new MappingContext[]{documentDbMappingContext()}))));
}
protected Set> getInitialEntitySet() throws ClassNotFoundException {
final Set> initialEntitySet = new HashSet>();
for (final String basePackage : getMappingBasePackages()) {
initialEntitySet.addAll(scanForEntities(basePackage));
}
return initialEntitySet;
}
protected Set> scanForEntities(String basePackage) throws ClassNotFoundException {
if (!StringUtils.hasText(basePackage)) {
return Collections.emptySet();
}
final Set> initialEntitySet = new HashSet>();
if (StringUtils.hasText(basePackage)) {
final ClassPathScanningCandidateComponentProvider componentProvider =
new ClassPathScanningCandidateComponentProvider(false);
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
for (final BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
final String className = candidate.getBeanClassName();
Assert.notNull(className, "Bean class name is null.");
initialEntitySet
.add(ClassUtils.forName(className, DocumentDbConfigurationSupport.class.getClassLoader()));
}
}
return initialEntitySet;
}
}