Skip to content

Commit

Permalink
Auto-Configure DomainClass Plugin
Browse files Browse the repository at this point in the history
Closes gh-630
  • Loading branch information
rainboyan committed Oct 5, 2024
1 parent 228cebf commit 67ba553
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 66 deletions.
3 changes: 3 additions & 0 deletions grace-plugin-domain-class/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ dependencies {
api(libs.grails.datastore.gorm)
api(libs.grails.datastore.gorm.support)
api(libs.grails.datastore.gorm.validation)

testImplementation libs.assertj.core
testImplementation libs.spring.boot.test
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 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
*
* https://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.grails.plugins.domain;

import java.util.List;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;

import grails.core.GrailsApplication;
import org.grails.datastore.gorm.validation.constraints.eval.ConstraintsEvaluator;
import org.grails.datastore.gorm.validation.constraints.factory.ConstraintFactory;
import org.grails.datastore.mapping.model.MappingContext;
import org.grails.datastore.mapping.validation.ValidatorRegistry;
import org.grails.plugins.domain.support.ConstraintEvaluatorAdapter;
import org.grails.plugins.domain.support.DefaultConstraintEvaluatorFactoryBean;
import org.grails.plugins.domain.support.DefaultMappingContextFactoryBean;
import org.grails.plugins.domain.support.ValidatorRegistryFactoryBean;

/**
* {@link EnableAutoConfiguration Auto-configure} for Domain Class
*
* @author Michael Yan
*
* @since 2023.1.0
*/
@AutoConfiguration
@AutoConfigureOrder(50)
public class DomainClassAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public MappingContext grailsDomainClassMappingContext(ObjectProvider<GrailsApplication> grailsApplication,
ObjectProvider<MessageSource> messageSource, ObjectProvider<ConstraintFactory<?>> ConstraintFactoryProvider) throws Exception {
DefaultMappingContextFactoryBean mappingContextFactoryBean = new DefaultMappingContextFactoryBean(grailsApplication.getObject(), messageSource.getObject());
ConstraintFactory<?>[] constraintFactories = ConstraintFactoryProvider.orderedStream().toArray(ConstraintFactory[]::new);
mappingContextFactoryBean.setConstraintFactories(List.of(constraintFactories));
return mappingContextFactoryBean.getObject();
}

@Bean
@ConditionalOnMissingBean
public ConstraintsEvaluator validateableConstraintsEvaluator(
ObjectProvider<GrailsApplication> grailsApplication,
ObjectProvider<MappingContext> grailsDomainClassMappingContext,
ObjectProvider<MessageSource> messageSource) throws Exception {
return new DefaultConstraintEvaluatorFactoryBean(grailsApplication.getObject(),
grailsDomainClassMappingContext.getObject(), messageSource.getObject()).getObject();
}

@Bean("org.grails.beans.ConstraintsEvaluator")
@ConditionalOnMissingBean
public ConstraintEvaluatorAdapter constraintEvaluatorAdapter(ObjectProvider<ConstraintsEvaluator> validateableConstraintsEvaluator) {
return new ConstraintEvaluatorAdapter(validateableConstraintsEvaluator.getObject());
}

@Bean
@ConditionalOnMissingBean
public ValidatorRegistry gormValidatorRegistry(ObjectProvider<MappingContext> grailsDomainClassMappingContext) throws Exception {
return new ValidatorRegistryFactoryBean(grailsDomainClassMappingContext.getObject()).getObject();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2022 the original author or authors.
* Copyright 2004-2024 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.
Expand All @@ -15,18 +15,10 @@
*/
package org.grails.plugins.domain

import groovy.util.logging.Slf4j
import org.springframework.core.PriorityOrdered

import grails.core.GrailsApplication
import grails.plugins.Plugin
import grails.util.GrailsUtil
import grails.validation.ConstraintsEvaluator

import org.grails.plugins.domain.support.ConstraintEvaluatorAdapter
import org.grails.plugins.domain.support.DefaultConstraintEvaluatorFactoryBean
import org.grails.plugins.domain.support.DefaultMappingContextFactoryBean
import org.grails.plugins.domain.support.ValidatorRegistryFactoryBean

/**
* Configures the domain classes in the spring context.
Expand All @@ -35,7 +27,6 @@ import org.grails.plugins.domain.support.ValidatorRegistryFactoryBean
* @author Michael Yan
* @since 0.4
*/
@Slf4j
class DomainClassGrailsPlugin extends Plugin implements PriorityOrdered {

def watchedResources = ['file:./grails-app/domain/**/*.groovy',
Expand All @@ -48,20 +39,7 @@ class DomainClassGrailsPlugin extends Plugin implements PriorityOrdered {

Closure doWithSpring() {
{ ->
GrailsApplication application = grailsApplication
validateableConstraintsEvaluator(DefaultConstraintEvaluatorFactoryBean) { bean ->
bean.lazyInit = true
bean.role = 'infrastructure'
}
"${ConstraintsEvaluator.BEAN_NAME}"(ConstraintEvaluatorAdapter, ref('validateableConstraintsEvaluator')) { bean ->
bean.lazyInit = true
}
grailsDomainClassMappingContext(DefaultMappingContextFactoryBean, application, applicationContext) { bean ->
bean.lazyInit = true
}
gormValidatorRegistry(ValidatorRegistryFactoryBean) { bean ->
bean.lazyInit = true
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 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.
Expand All @@ -15,11 +15,9 @@
*/
package org.grails.plugins.domain.support

import groovy.transform.CompileStatic
import org.springframework.beans.factory.FactoryBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.MessageSource
import org.springframework.context.annotation.Lazy

import grails.core.GrailsApplication

Expand All @@ -30,25 +28,34 @@ import org.grails.datastore.gorm.validation.constraints.registry.DefaultConstrai
import org.grails.datastore.mapping.model.MappingContext
import org.grails.validation.ConstraintEvalUtils

/**
* A factory bean for creating the default ConstraintsEvaluator where an implementation of GORM is not present
*
* @author James Kleeh
* @author Michael Yan
* @since 3.3
*/
@CompileStatic
class DefaultConstraintEvaluatorFactoryBean implements FactoryBean<ConstraintsEvaluator> {

@Lazy
@Autowired
MessageSource messageSource
private final GrailsApplication grailsApplication
private final MappingContext grailsDomainClassMappingContext
private final MessageSource messageSource

@Lazy
@Autowired
@Qualifier('grailsDomainClassMappingContext')
MappingContext grailsDomainClassMappingContext

@Autowired
GrailsApplication grailsApplication
DefaultConstraintEvaluatorFactoryBean(GrailsApplication grailsApplication,
MappingContext grailsDomainClassMappingContext,
MessageSource messageSource) {
this.grailsApplication = grailsApplication
this.grailsDomainClassMappingContext = grailsDomainClassMappingContext
this.messageSource = messageSource
}

@Override
ConstraintsEvaluator getObject() throws Exception {
ConstraintRegistry registry = new DefaultConstraintRegistry(messageSource)
ConstraintRegistry registry = new DefaultConstraintRegistry(this.messageSource)

new DefaultConstraintEvaluator(registry, grailsDomainClassMappingContext, ConstraintEvalUtils.getDefaultConstraints(grailsApplication.config))
new DefaultConstraintEvaluator(registry, this.grailsDomainClassMappingContext,
ConstraintEvalUtils.getDefaultConstraints(this.grailsApplication.config))
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2022 the original author or authors.
* Copyright 2004-2024 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.
Expand All @@ -18,7 +18,6 @@ package org.grails.plugins.domain.support
import groovy.transform.CompileStatic
import org.springframework.beans.factory.FactoryBean
import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.context.MessageSource
import org.springframework.core.env.PropertyResolver
Expand All @@ -38,16 +37,19 @@ import org.grails.datastore.mapping.model.MappingContext
* A factory bean for creating the default mapping context where an implementation of GORM is not present
*
* @author Graeme Rocher
* @author Michael Yan
* @since 3.3
*/
@CompileStatic
class DefaultMappingContextFactoryBean implements FactoryBean<MappingContext>, InitializingBean {

final PropertyResolver configuration
final GrailsApplication grailsApplication
protected final PropertyResolver configuration
protected final GrailsApplication grailsApplication
protected final MessageSource messageSource
protected final ApplicationContext applicationContext
private MappingContext mappingContext
protected MappingContext mappingContext

private List<ConstraintFactory> constraintFactories = new ArrayList<>()

DefaultMappingContextFactoryBean(GrailsApplication grailsApplication, MessageSource messageSource) {
this.configuration = grailsApplication.config
Expand All @@ -57,13 +59,16 @@ class DefaultMappingContextFactoryBean implements FactoryBean<MappingContext>, I
this.applicationContext = (ApplicationContext) messageSource
}
else {
applicationContext = null
this.applicationContext = null
}
}

@Override
MappingContext getObject() throws Exception {
mappingContext
if (this.mappingContext == null) {
afterPropertiesSet()
}
this.mappingContext
}

@Override
Expand All @@ -76,25 +81,24 @@ class DefaultMappingContextFactoryBean implements FactoryBean<MappingContext>, I
true
}

@Autowired(required = false)
List<ConstraintFactory> constraintFactories = []
void setConstraintFactories(List<ConstraintFactory> constraintFactories) {
this.constraintFactories = constraintFactories
}

@Override
void afterPropertiesSet() throws Exception {
ConnectionSourceSettingsBuilder builder = new ConnectionSourceSettingsBuilder(configuration)
ConnectionSourceSettingsBuilder builder = new ConnectionSourceSettingsBuilder(this.configuration)
ConnectionSourceSettings settings = builder.build()

this.mappingContext = new KeyValueMappingContext('default', settings)
DefaultValidatorRegistry validatorRegistry = new DefaultValidatorRegistry(mappingContext, settings, messageSource)
for (factory in constraintFactories) {
DefaultValidatorRegistry validatorRegistry = new DefaultValidatorRegistry(this.mappingContext, settings, this.messageSource)
for (ConstraintFactory factory in this.constraintFactories) {
validatorRegistry.addConstraintFactory(factory)
}
mappingContext.setValidatorRegistry(
validatorRegistry
)
this.mappingContext.setValidatorRegistry(validatorRegistry)

GrailsClass[] persistentClasses = grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE)
mappingContext.addPersistentEntities(persistentClasses*.clazz as Class[])
GrailsClass[] persistentClasses = this.grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE)
this.mappingContext.addPersistentEntities(persistentClasses*.clazz as Class[])
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 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.
Expand All @@ -17,9 +17,6 @@ package org.grails.plugins.domain.support

import groovy.transform.CompileStatic
import org.springframework.beans.factory.FactoryBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Lazy

import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.validation.ValidatorRegistry
Expand All @@ -33,14 +30,15 @@ import org.grails.datastore.mapping.validation.ValidatorRegistry
@CompileStatic
class ValidatorRegistryFactoryBean implements FactoryBean<ValidatorRegistry> {

@Lazy
@Autowired
@Qualifier('grailsDomainClassMappingContext')
MappingContext mappingContext
private final MappingContext mappingContext

ValidatorRegistryFactoryBean(MappingContext mappingContext) {
this.mappingContext = mappingContext
}

@Override
ValidatorRegistry getObject() throws Exception {
mappingContext.validatorRegistry
this.mappingContext.validatorRegistry
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.grails.plugins.domain.DomainClassAutoConfiguration
Loading

0 comments on commit 67ba553

Please sign in to comment.