diff --git a/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeCachingProviderAutoConfiguration.java b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeCachingProviderAutoConfiguration.java new file mode 100644 index 00000000..68e52fb3 --- /dev/null +++ b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeCachingProviderAutoConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.springframework.boot.data.geode.autoconfigure; + +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; + +import java.util.Optional; + +import javax.annotation.PostConstruct; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.client.ClientCache; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers; +import org.springframework.boot.autoconfigure.cache.CacheProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.cache.CacheManager; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.gemfire.CacheFactoryBean; +import org.springframework.data.gemfire.cache.GemfireCacheManager; +import org.springframework.data.gemfire.cache.config.EnableGemfireCaching; +import org.springframework.data.gemfire.client.ClientCacheFactoryBean; + +/** + * Cache auto-configuration for Spring's Cache Abstraction, using Apache Geode as the caching provider. + * + * @author John Blum + * @see org.apache.geode.cache.Cache + * @see org.apache.geode.cache.GemFireCache + * @see org.apache.geode.cache.client.ClientCache + * @see org.springframework.context.annotation.Configuration + * @see org.springframework.data.gemfire.cache.GemfireCacheManager + * @since 1.5.0 + */ +@Configuration +//@Conditional(CacheCondition.class) +@ConditionalOnBean({ CacheFactoryBean.class, Cache.class, ClientCacheFactoryBean.class, ClientCache.class }) +@ConditionalOnClass(GemfireCacheManager.class) +@ConditionalOnMissingBean(CacheManager.class) +@EnableGemfireCaching +@SuppressWarnings("all") +class GeodeCachingProviderAutoConfiguration { + + private final CacheManagerCustomizers cacheManagerCustomizers; + + private final CacheProperties cacheProperties; + + @Autowired + private GemfireCacheManager cacheManager; + + GeodeCachingProviderAutoConfiguration(CacheProperties cacheProperties, CacheManagerCustomizers cacheManagerCustomizers) { + this.cacheProperties = cacheProperties; + this.cacheManagerCustomizers = cacheManagerCustomizers; + } + + GemfireCacheManager getCacheManager() { + return Optional.ofNullable(this.cacheManager) + .orElseThrow(() -> newIllegalStateException("GemfireCacheManager was not properly configured")); + } + + Optional getCacheManagerCustomizers() { + return Optional.ofNullable(this.cacheManagerCustomizers); + } + + Optional getCacheProperties() { + return Optional.ofNullable(this.cacheProperties); + } + + @PostConstruct + public void onGeodeCachingInitialization() { + getCacheManagerCustomizers() + .ifPresent(cacheManagerCustomizers -> cacheManagerCustomizers.customize(getCacheManager())); + } +} diff --git a/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeClientCacheAutoConfiguration.java b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeClientCacheAutoConfiguration.java new file mode 100644 index 00000000..910e2a68 --- /dev/null +++ b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeClientCacheAutoConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.springframework.boot.data.geode.autoconfigure; + +import org.apache.geode.cache.client.ClientCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.gemfire.client.ClientCacheFactoryBean; +import org.springframework.data.gemfire.config.annotation.ClientCacheApplication; + +/** + * The GeodeClientCacheAutoConfiguration class... + * + * @author John Blum + * @since 1.0.0 + */ +@Configuration +@ConditionalOnClass({ ClientCacheFactoryBean.class, ClientCache.class }) +@ConditionalOnMissingBean(ClientCache.class) +@ClientCacheApplication +@SuppressWarnings("all") +public class GeodeClientCacheAutoConfiguration { + +} diff --git a/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfiguration.java b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfiguration.java new file mode 100644 index 00000000..b2e8a673 --- /dev/null +++ b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfiguration.java @@ -0,0 +1,59 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.springframework.boot.data.geode.autoconfigure; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.client.ClientCache; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.gemfire.repository.GemfireRepository; +import org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension; +import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean; + +/** + * Spring Boot {@link EnableAutoConfiguration auto-configuration} for Spring Data's Geode Repositories. + * + * Activates when there is a bean of type {@link Cache} or {@link ClientCache} configured in the Spring context, + * the Spring Data Geode {@link org.springframework.data.gemfire.repository.GemfireRepository} type is on the classpath, + * and there is no other existing {@link org.springframework.data.gemfire.repository.GemfireRepository} configured. + * + * Once in effect, the auto-configuration is the equivalent of enabling Geode Repositories using the + * {@link org.springframework.data.gemfire.repository.config.EnableGemfireRepositories} annotation. + * + * @author John Blum + * @since 1.0.0 + * @see GeodeRepositoriesAutoConfigurationRegistrar + * @see org.springframework.context.annotation.Configuration + * @see org.springframework.data.gemfire.repository.config.EnableGemfireRepositories + * @see org.apache.geode.cache.Cache + * @see org.apache.geode.cache.client.ClientCache + */ +@Configuration +@ConditionalOnBean({ Cache.class, ClientCache.class }) +@ConditionalOnClass(GemfireRepository.class) +@ConditionalOnMissingBean({ GemfireRepositoryConfigurationExtension.class, GemfireRepositoryFactoryBean.class }) +@ConditionalOnProperty(prefix = "spring.data.geode.repositories", + name = "enabled", havingValue = "true", matchIfMissing = true) +@Import(GeodeRepositoriesAutoConfigurationRegistrar.class) +public class GeodeRepositoriesAutoConfiguration { + +} diff --git a/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfigurationRegistrar.java b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfigurationRegistrar.java new file mode 100644 index 00000000..ac80ac03 --- /dev/null +++ b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/GeodeRepositoriesAutoConfigurationRegistrar.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.springframework.boot.data.geode.autoconfigure; + +import java.lang.annotation.Annotation; + +import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories; +import org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension; +import org.springframework.data.repository.config.RepositoryConfigurationExtension; + +/** + * Spring {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Geode Repositories. + * + * @author John Blum + * @see org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport + * @see org.springframework.data.gemfire.repository.config.EnableGemfireRepositories + * @see org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension + * @since 1.5.0 + */ +public class GeodeRepositoriesAutoConfigurationRegistrar + extends AbstractRepositoryConfigurationSourceSupport { + + @Override + protected Class getAnnotation() { + return EnableGemfireRepositories.class; + } + + @Override + protected Class getConfiguration() { + return EnableGeodeRepositoriesConfiguration.class; + } + + @Override + protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() { + return new GemfireRepositoryConfigurationExtension(); + } + + @EnableGemfireRepositories + private static class EnableGeodeRepositoriesConfiguration { + + } +} diff --git a/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/package-info.java b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/package-info.java new file mode 100644 index 00000000..b49e8093 --- /dev/null +++ b/spring-boot-data-geode/src/main/java/org/springframework/boot/data/geode/autoconfigure/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +/** + * Spring Boot auto-configuration for Spring Data Geode. + */ +package org.springframework.boot.data.geode.autoconfigure; diff --git a/spring-boot-data-geode/src/main/resources/spring.factories b/spring-boot-data-geode/src/main/resources/spring.factories new file mode 100644 index 00000000..1d8f985d --- /dev/null +++ b/spring-boot-data-geode/src/main/resources/spring.factories @@ -0,0 +1,5 @@ +# Auto Configuration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.boot.data.geode.autoconfigure.GeodeCachingProviderAutoConfiguration,\ +org.springframework.boot.data.geode.autoconfigure.GeodeClientCacheAutoConfiguration,\ +org.springframework.boot.data.geode.autoconfigure.GeodeRepositoriesAutoConfiguration