|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2011 by the original author(s). |
| 2 | + * Copyright 2011-2012 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 | 7 | *
|
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | *
|
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| - |
17 | 16 | package org.springframework.data.mongodb.config;
|
18 | 17 |
|
19 | 18 | import static org.springframework.data.mongodb.config.BeanNames.*;
|
|
30 | 29 | import org.springframework.beans.factory.config.BeanDefinition;
|
31 | 30 | import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
32 | 31 | import org.springframework.beans.factory.config.RuntimeBeanReference;
|
| 32 | +import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
33 | 33 | import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
34 | 34 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
35 | 35 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
36 | 36 | import org.springframework.beans.factory.support.GenericBeanDefinition;
|
37 | 37 | import org.springframework.beans.factory.support.ManagedList;
|
38 | 38 | import org.springframework.beans.factory.support.ManagedSet;
|
| 39 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
39 | 40 | import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
|
40 | 41 | import org.springframework.beans.factory.xml.ParserContext;
|
41 | 42 | import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
|
52 | 53 | import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
|
53 | 54 | import org.springframework.data.mongodb.core.mapping.Document;
|
54 | 55 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 56 | +import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener; |
55 | 57 | import org.springframework.util.Assert;
|
| 58 | +import org.springframework.util.ClassUtils; |
56 | 59 | import org.springframework.util.StringUtils;
|
57 | 60 | import org.springframework.util.xml.DomUtils;
|
58 | 61 | import org.w3c.dom.Element;
|
59 | 62 |
|
60 | 63 | /**
|
61 |
| - * @author Jon Brisbin <[email protected]> |
| 64 | + * Bean definition parser for the {@code mapping-converter} element. |
| 65 | + * |
| 66 | + * @author Jon Brisbin |
62 | 67 | * @author Oliver Gierke
|
| 68 | + * @author Maciej Walkowiak |
63 | 69 | */
|
64 | 70 | public class MappingMongoConverterParser extends AbstractBeanDefinitionParser {
|
65 | 71 |
|
66 | 72 | private static final String BASE_PACKAGE = "base-package";
|
| 73 | + private static final boolean jsr303Present = ClassUtils.isPresent("javax.validation.Validator", |
| 74 | + MappingMongoConverterParser.class.getClassLoader()); |
67 | 75 |
|
68 | 76 | @Override
|
69 | 77 | protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
|
@@ -107,9 +115,53 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
|
107 | 115 | registry.registerBeanDefinition(INDEX_HELPER, indexHelperBuilder.getBeanDefinition());
|
108 | 116 | }
|
109 | 117 |
|
| 118 | + BeanDefinition validatingMongoEventListener = potentiallyCreateValidatingMongoEventListener(element, parserContext); |
| 119 | + |
| 120 | + if (validatingMongoEventListener != null) { |
| 121 | + registry.registerBeanDefinition(VALIDATING_EVENT_LISTENER, validatingMongoEventListener); |
| 122 | + } |
| 123 | + |
110 | 124 | return converterBuilder.getBeanDefinition();
|
111 | 125 | }
|
112 | 126 |
|
| 127 | + private BeanDefinition potentiallyCreateValidatingMongoEventListener(Element element, ParserContext parserContext) { |
| 128 | + |
| 129 | + String disableValidation = element.getAttribute("disable-validation"); |
| 130 | + boolean validationDisabled = StringUtils.hasText(disableValidation) && Boolean.valueOf(disableValidation); |
| 131 | + |
| 132 | + if (!validationDisabled) { |
| 133 | + |
| 134 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); |
| 135 | + RuntimeBeanReference validator = getValidator(builder, parserContext); |
| 136 | + |
| 137 | + if (validator != null) { |
| 138 | + |
| 139 | + builder.getRawBeanDefinition().setBeanClass(ValidatingMongoEventListener.class); |
| 140 | + builder.addConstructorArgValue(validator); |
| 141 | + |
| 142 | + return builder.getBeanDefinition(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) { |
| 150 | + |
| 151 | + if (!jsr303Present) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + RootBeanDefinition validatorDef = new RootBeanDefinition( |
| 156 | + "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"); |
| 157 | + validatorDef.setSource(source); |
| 158 | + validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 159 | + String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef); |
| 160 | + parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName)); |
| 161 | + |
| 162 | + return new RuntimeBeanReference(validatorName); |
| 163 | + } |
| 164 | + |
113 | 165 | private String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
|
114 | 166 | BeanDefinition conversionsDefinition) {
|
115 | 167 |
|
|
0 commit comments