|
| 1 | +/* |
| 2 | + * Copyright 2017-2019 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 | + * http://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 | + |
| 17 | +package org.springframework.cloud.gcp.data.datastore.repository.config; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 22 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 23 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 24 | +import org.springframework.cloud.gcp.data.datastore.repository.support.DatastoreAuditingEventListener; |
| 25 | +import org.springframework.data.auditing.AuditingHandler; |
| 26 | +import org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport; |
| 27 | +import org.springframework.data.auditing.config.AuditingConfiguration; |
| 28 | + |
| 29 | +/** |
| 30 | + * Registers the annotations and classes for providing auditing support in Spring Data |
| 31 | + * Cloud Datastore. |
| 32 | + * |
| 33 | + * @author Chengyuan Zhao |
| 34 | + * @since 1.2 |
| 35 | + */ |
| 36 | +public class DatastoreAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport { |
| 37 | + |
| 38 | + private static final String AUDITING_HANDLER_BEAN_NAME = "datastoreAuditingHandler"; |
| 39 | + |
| 40 | + private static final String MAPPING_CONTEXT_BEAN_NAME = "datastoreMappingContext"; |
| 41 | + |
| 42 | + @Override |
| 43 | + protected Class<? extends Annotation> getAnnotation() { |
| 44 | + return EnableDatastoreAuditing.class; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandlerDefinition, |
| 49 | + BeanDefinitionRegistry registry) { |
| 50 | + Class<?> listenerClass = DatastoreAuditingEventListener.class; |
| 51 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(listenerClass) |
| 52 | + .addConstructorArgReference(AUDITING_HANDLER_BEAN_NAME); |
| 53 | + |
| 54 | + registerInfrastructureBeanWithId(builder.getRawBeanDefinition(), listenerClass.getName(), registry); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected BeanDefinitionBuilder getAuditHandlerBeanDefinitionBuilder(AuditingConfiguration configuration) { |
| 59 | + BeanDefinitionBuilder builder = configureDefaultAuditHandlerAttributes(configuration, |
| 60 | + BeanDefinitionBuilder.rootBeanDefinition(AuditingHandler.class)); |
| 61 | + return builder.addConstructorArgReference(MAPPING_CONTEXT_BEAN_NAME); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected String getAuditingHandlerBeanName() { |
| 66 | + return AUDITING_HANDLER_BEAN_NAME; |
| 67 | + } |
| 68 | +} |
0 commit comments