|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package grails.plugins.mongodb; |
| 17 | + |
| 18 | +import java.beans.Introspector; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import com.mongodb.client.MongoClient; |
| 24 | +import org.springframework.beans.BeansException; |
| 25 | +import org.springframework.beans.factory.ObjectProvider; |
| 26 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigureOrder; |
| 28 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
| 32 | +import org.springframework.context.ApplicationContext; |
| 33 | +import org.springframework.context.ApplicationContextAware; |
| 34 | +import org.springframework.context.ConfigurableApplicationContext; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Primary; |
| 37 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 38 | +import org.springframework.transaction.PlatformTransactionManager; |
| 39 | + |
| 40 | +import grails.boot.config.GrailsComponentScanner; |
| 41 | +import grails.core.GrailsApplication; |
| 42 | +import grails.core.GrailsClass; |
| 43 | +import org.grails.core.artefact.DomainClassArtefactHandler; |
| 44 | +import org.grails.datastore.gorm.events.AutoTimestampEventListener; |
| 45 | +import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher; |
| 46 | +import org.grails.datastore.gorm.plugin.support.PersistenceContextInterceptorAggregator; |
| 47 | +import org.grails.datastore.gorm.support.DatastorePersistenceContextInterceptor; |
| 48 | +import org.grails.datastore.mapping.model.MappingContext; |
| 49 | +import org.grails.datastore.mapping.mongo.MongoDatastore; |
| 50 | +import org.grails.datastore.mapping.services.Service; |
| 51 | +import org.grails.datastore.mapping.web.support.OpenSessionInViewInterceptor; |
| 52 | + |
| 53 | +/** |
| 54 | + * {@link EnableAutoConfiguration Auto-Configure} for GORM for MongoDB |
| 55 | + * |
| 56 | + * @author Michael Yan |
| 57 | + * @since 2023.3 |
| 58 | + */ |
| 59 | +@AutoConfigureOrder(300) |
| 60 | +@AutoConfiguration(after = MongoAutoConfiguration.class) |
| 61 | +@ConditionalOnMissingBean(MongoDatastore.class) |
| 62 | +public class MongoDbGormAutoConfiguration implements ApplicationContextAware { |
| 63 | + |
| 64 | + private ConfigurableApplicationContext applicationContext; |
| 65 | + |
| 66 | + @Bean |
| 67 | + @Primary |
| 68 | + @ConditionalOnMissingBean |
| 69 | + public MongoDatastore mongoDatastore(ObjectProvider<MongoClient> mongo, ObjectProvider<GrailsApplication> grailsApplication) { |
| 70 | + GrailsClass[] grailsClasses = grailsApplication.getObject().getArtefacts(DomainClassArtefactHandler.TYPE); |
| 71 | + Set<Class<?>> domainClasses = new HashSet<>(); |
| 72 | + for (GrailsClass grailsClass : grailsClasses) { |
| 73 | + if (grailsClass.getClazz() != null) { |
| 74 | + domainClasses.add(grailsClass.getClazz()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + GrailsComponentScanner scanner = new GrailsComponentScanner(this.applicationContext); |
| 79 | + Set<Class<?>> entityClasses; |
| 80 | + try { |
| 81 | + entityClasses = scanner.scan(grails.gorm.annotation.Entity.class, grails.persistence.Entity.class); |
| 82 | + } |
| 83 | + catch (ClassNotFoundException ignored) { |
| 84 | + entityClasses = Collections.emptySet(); |
| 85 | + } |
| 86 | + |
| 87 | + domainClasses.addAll(entityClasses); |
| 88 | + |
| 89 | + ConfigurableEnvironment environment = this.applicationContext.getEnvironment(); |
| 90 | + ConfigurableApplicationContextEventPublisher eventPublisher = new ConfigurableApplicationContextEventPublisher(this.applicationContext); |
| 91 | + MongoDatastore datastore; |
| 92 | + if (mongo.getIfAvailable() != null) { |
| 93 | + datastore = new MongoDatastore(mongo.getObject(), environment, eventPublisher, domainClasses.toArray(new Class[0])); |
| 94 | + } |
| 95 | + else { |
| 96 | + datastore = new MongoDatastore(environment, eventPublisher, domainClasses.toArray(new Class[0])); |
| 97 | + } |
| 98 | + |
| 99 | + for (Service<?> service : datastore.getServices()) { |
| 100 | + Class<?> serviceClass = service.getClass(); |
| 101 | + grails.gorm.services.Service ann = serviceClass.getAnnotation(grails.gorm.services.Service.class); |
| 102 | + String serviceName; |
| 103 | + if (ann == null) { |
| 104 | + serviceName = Introspector.decapitalize(serviceClass.getSimpleName()); |
| 105 | + } |
| 106 | + else { |
| 107 | + serviceName = ann.name(); |
| 108 | + } |
| 109 | + if (!this.applicationContext.containsBean(serviceName)) { |
| 110 | + this.applicationContext.getBeanFactory().registerSingleton( |
| 111 | + serviceName, |
| 112 | + service |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return datastore; |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + @ConditionalOnMissingBean |
| 122 | + public MappingContext mongoMappingContext(MongoDatastore mongoDatastore) { |
| 123 | + return mongoDatastore.getMappingContext(); |
| 124 | + } |
| 125 | + |
| 126 | + @Bean |
| 127 | + @ConditionalOnMissingBean |
| 128 | + public AutoTimestampEventListener mongoAutoTimestampEventListener(MongoDatastore mongoDatastore) { |
| 129 | + return mongoDatastore.getAutoTimestampEventListener(); |
| 130 | + } |
| 131 | + |
| 132 | + @Bean |
| 133 | + @ConditionalOnMissingBean |
| 134 | + public DatastorePersistenceContextInterceptor datastorePersistenceContextInterceptor(MongoDatastore mongoDatastore) { |
| 135 | + return new DatastorePersistenceContextInterceptor(mongoDatastore); |
| 136 | + } |
| 137 | + |
| 138 | + @Bean |
| 139 | + @ConditionalOnMissingBean |
| 140 | + public PersistenceContextInterceptorAggregator persistenceContextInterceptorAggregator() { |
| 141 | + return new PersistenceContextInterceptorAggregator(); |
| 142 | + } |
| 143 | + |
| 144 | + @Bean({"mongoTransactionManager", "transactionManager"}) |
| 145 | + @Primary |
| 146 | + @ConditionalOnMissingBean |
| 147 | + public PlatformTransactionManager transactionManager(MongoDatastore mongoDatastore) { |
| 148 | + return mongoDatastore.getTransactionManager(); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + @ConditionalOnMissingBean |
| 153 | + @ConditionalOnClass(name = "org.grails.datastore.mapping.web.support.OpenSessionInViewInterceptor") |
| 154 | + public OpenSessionInViewInterceptor mongoOpenSessionInViewInterceptor(MongoDatastore mongoDatastore) { |
| 155 | + OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); |
| 156 | + interceptor.setDatastore(mongoDatastore); |
| 157 | + return interceptor; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 162 | + if (!(applicationContext instanceof ConfigurableApplicationContext)) { |
| 163 | + throw new IllegalArgumentException("MongoDbGormAutoConfiguration requires an instance of ConfigurableApplicationContext"); |
| 164 | + } |
| 165 | + this.applicationContext = (ConfigurableApplicationContext) applicationContext; |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments