|
| 1 | +/* |
| 2 | + * Copyright 2020 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 org.springframework.data.elasticsearch.config; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | + |
| 20 | +import org.springframework.beans.factory.FactoryBean; |
| 21 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 22 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 23 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 24 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 25 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
| 26 | +import org.springframework.core.type.AnnotationMetadata; |
| 27 | +import org.springframework.data.auditing.IsNewAwareAuditingHandler; |
| 28 | +import org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport; |
| 29 | +import org.springframework.data.auditing.config.AuditingConfiguration; |
| 30 | +import org.springframework.data.config.ParsingUtils; |
| 31 | +import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; |
| 32 | +import org.springframework.data.elasticsearch.core.event.AuditingEntityCallback; |
| 33 | +import org.springframework.data.elasticsearch.core.event.ReactiveAuditingEntityCallback; |
| 34 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; |
| 35 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
| 36 | +import org.springframework.data.mapping.context.MappingContext; |
| 37 | +import org.springframework.data.repository.util.ReactiveWrappers; |
| 38 | +import org.springframework.util.Assert; |
| 39 | + |
| 40 | +/** |
| 41 | + * {@link ImportBeanDefinitionRegistrar} to enable {@link EnableElasticsearchAuditing} annotation. |
| 42 | + * |
| 43 | + * @author Peter-Josef Meisch |
| 44 | + * @since 4.0 |
| 45 | + */ |
| 46 | +class ElasticsearchAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport { |
| 47 | + |
| 48 | + /* |
| 49 | + * (non-Javadoc) |
| 50 | + * @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAnnotation() |
| 51 | + */ |
| 52 | + @Override |
| 53 | + protected Class<? extends Annotation> getAnnotation() { |
| 54 | + return EnableElasticsearchAuditing.class; |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * (non-Javadoc) |
| 59 | + * @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAuditingHandlerBeanName() |
| 60 | + */ |
| 61 | + @Override |
| 62 | + protected String getAuditingHandlerBeanName() { |
| 63 | + return "elasticsearchAuditingHandler"; |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * (non-Javadoc) |
| 68 | + * @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry) |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) { |
| 72 | + |
| 73 | + Assert.notNull(annotationMetadata, "AnnotationMetadata must not be null!"); |
| 74 | + Assert.notNull(registry, "BeanDefinitionRegistry must not be null!"); |
| 75 | + |
| 76 | + super.registerBeanDefinitions(annotationMetadata, registry); |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + * (non-Javadoc) |
| 81 | + * @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAuditHandlerBeanDefinitionBuilder(org.springframework.data.auditing.config.AuditingConfiguration) |
| 82 | + */ |
| 83 | + @Override |
| 84 | + protected BeanDefinitionBuilder getAuditHandlerBeanDefinitionBuilder(AuditingConfiguration configuration) { |
| 85 | + |
| 86 | + Assert.notNull(configuration, "AuditingConfiguration must not be null!"); |
| 87 | + |
| 88 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(IsNewAwareAuditingHandler.class); |
| 89 | + |
| 90 | + BeanDefinitionBuilder definition = BeanDefinitionBuilder |
| 91 | + .genericBeanDefinition(ElasticsearchMappingContextLookup.class); |
| 92 | + definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
| 93 | + |
| 94 | + builder.addConstructorArgValue(definition.getBeanDefinition()); |
| 95 | + return configureDefaultAuditHandlerAttributes(configuration, builder); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * (non-Javadoc) |
| 100 | + * @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#registerAuditListener(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry) |
| 101 | + */ |
| 102 | + @Override |
| 103 | + protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandlerDefinition, |
| 104 | + BeanDefinitionRegistry registry) { |
| 105 | + |
| 106 | + Assert.notNull(auditingHandlerDefinition, "BeanDefinition must not be null!"); |
| 107 | + Assert.notNull(registry, "BeanDefinitionRegistry must not be null!"); |
| 108 | + |
| 109 | + BeanDefinitionBuilder listenerBeanDefinitionBuilder = BeanDefinitionBuilder |
| 110 | + .rootBeanDefinition(AuditingEntityCallback.class); |
| 111 | + listenerBeanDefinitionBuilder |
| 112 | + .addConstructorArgValue(ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), registry)); |
| 113 | + |
| 114 | + registerInfrastructureBeanWithId(listenerBeanDefinitionBuilder.getBeanDefinition(), |
| 115 | + AuditingEntityCallback.class.getName(), registry); |
| 116 | + |
| 117 | + if (ReactiveWrappers.isAvailable(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR)) { |
| 118 | + registerReactiveAuditingEntityCallback(registry, auditingHandlerDefinition.getSource()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private void registerReactiveAuditingEntityCallback(BeanDefinitionRegistry registry, Object source) { |
| 123 | + |
| 124 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ReactiveAuditingEntityCallback.class); |
| 125 | + |
| 126 | + builder.addConstructorArgValue(ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), registry)); |
| 127 | + builder.getRawBeanDefinition().setSource(source); |
| 128 | + |
| 129 | + registerInfrastructureBeanWithId(builder.getBeanDefinition(), ReactiveAuditingEntityCallback.class.getName(), |
| 130 | + registry); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Simple helper to be able to wire the {@link MappingContext} from a {@link MappingElasticsearchConverter} bean |
| 135 | + * available in the application context. |
| 136 | + * |
| 137 | + * @author Oliver Gierke |
| 138 | + */ |
| 139 | + static class ElasticsearchMappingContextLookup implements |
| 140 | + FactoryBean<MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty>> { |
| 141 | + |
| 142 | + private final MappingElasticsearchConverter converter; |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a new {@link ElasticsearchMappingContextLookup} for the given {@link MappingElasticsearchConverter}. |
| 146 | + * |
| 147 | + * @param converter must not be {@literal null}. |
| 148 | + */ |
| 149 | + public ElasticsearchMappingContextLookup(MappingElasticsearchConverter converter) { |
| 150 | + this.converter = converter; |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + * (non-Javadoc) |
| 155 | + * @see org.springframework.beans.factory.FactoryBean#getObject() |
| 156 | + */ |
| 157 | + @Override |
| 158 | + public MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> getObject() |
| 159 | + throws Exception { |
| 160 | + return converter.getMappingContext(); |
| 161 | + } |
| 162 | + |
| 163 | + /* |
| 164 | + * (non-Javadoc) |
| 165 | + * @see org.springframework.beans.factory.FactoryBean#getObjectType() |
| 166 | + */ |
| 167 | + @Override |
| 168 | + public Class<?> getObjectType() { |
| 169 | + return MappingContext.class; |
| 170 | + } |
| 171 | + |
| 172 | + /* |
| 173 | + * (non-Javadoc) |
| 174 | + * @see org.springframework.beans.factory.FactoryBean#isSingleton() |
| 175 | + */ |
| 176 | + @Override |
| 177 | + public boolean isSingleton() { |
| 178 | + return true; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments