|
| 1 | +package org.springframework.data.mongodb.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.BeanDefinitionStoreException; |
| 4 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 5 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 6 | +import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
| 7 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 8 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 9 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 10 | +import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; |
| 11 | +import org.springframework.beans.factory.xml.ParserContext; |
| 12 | +import org.springframework.data.mongodb.core.mapping.event.BeforeSaveValidator; |
| 13 | +import org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser; |
| 14 | +import org.springframework.util.ClassUtils; |
| 15 | +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| 16 | +import org.w3c.dom.Element; |
| 17 | + |
| 18 | +public class MongoValidationParser extends AbstractBeanDefinitionParser { |
| 19 | + private static final boolean jsr303Present = ClassUtils.isPresent("javax.validation.Validator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); |
| 20 | + |
| 21 | + @Override |
| 22 | + protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException { |
| 23 | + return "beforeSaveValidator"; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { |
| 28 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); |
| 29 | + |
| 30 | + RuntimeBeanReference validator = getValidator(builder, parserContext); |
| 31 | + |
| 32 | + if (validator == null) { |
| 33 | + return null; |
| 34 | + } else { |
| 35 | + builder.getRawBeanDefinition().setBeanClass(BeforeSaveValidator.class); |
| 36 | + builder.addPropertyValue("validator", validator); |
| 37 | + |
| 38 | + return builder.getBeanDefinition(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) { |
| 43 | + if (!jsr303Present) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + RootBeanDefinition validatorDef = new RootBeanDefinition(LocalValidatorFactoryBean.class); |
| 48 | + validatorDef.setSource(source); |
| 49 | + validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 50 | + String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef); |
| 51 | + parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName)); |
| 52 | + |
| 53 | + return new RuntimeBeanReference(validatorName); |
| 54 | + } |
| 55 | +} |
0 commit comments