Add entity lifercycle interceptor interface and add a entity listener.

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-07-28 12:39:12 +02:00
parent a978662812
commit 7fa89d7d3c
5 changed files with 201 additions and 1 deletions

View File

@@ -0,0 +1,79 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.model;
/**
* Interface for the entity interceptor lifecycle.
*/
public interface EntityInterceptor {
/**
* Callback for the {@link @PrePersist} lifecycle event.
*
* @param entity
* the model entity
*/
default void prePersist(final Object entity) {
};
/**
* Callback for the {@link @PostPersist} lifecycle event.
*
* @param entity
* the model entity
*/
default void postPersist(final Object entity) {
};
/**
* Callback for the {@link @PostRemove} lifecycle event.
*
* @param entity
* the model entity
*/
default void postRemove(final Object entity) {
};
/**
* Callback for the {@link @PreRemove} lifecycle event.
*
* @param entity
* the model entity
*/
default void preRemove(final Object entity) {
};
/**
* Callback for the {@link @PostLoad} lifecycle event.
*
* @param entity
* the model entity
*/
default void postLoad(final Object entity) {
};
/**
* Callback for the {@link @PreUpdate} lifecycle event.
*
* @param entity
* the model entity
*/
default void preUpdate(final Object entity) {
};
/**
* Callback for the {@link @PostUpdate} lifecycle event.
*
* @param entity
* the model entity
*/
default void postUpdate(final Object entity) {
};
}

View File

@@ -46,6 +46,7 @@ import org.eclipse.hawkbit.repository.jpa.aspects.ExceptionMappingAspectHandler;
import org.eclipse.hawkbit.repository.jpa.configuration.MultiTenantJpaTransactionManager;
import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.CacheManagerHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.EntityInterceptorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.SecurityTokenGeneratorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.SystemManagementHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.SystemSecurityContextHolder;
@@ -144,6 +145,14 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
return SecurityTokenGeneratorHolder.getInstance();
}
/**
* @return the singleton instance of the {@link EntityInterceptorHolder}
*/
@Bean
public EntityInterceptorHolder entityInterceptorHolder() {
return EntityInterceptorHolder.getInstance();
}
/**
* @return the singleton instance of the {@link CacheManagerHolder}
*/

View File

@@ -31,7 +31,8 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
*/
@MappedSuperclass
@Access(AccessType.FIELD)
@EntityListeners({ AuditingEntityListener.class, CacheFieldEntityListener.class, EntityPropertyChangeListener.class })
@EntityListeners({ AuditingEntityListener.class, CacheFieldEntityListener.class, EntityPropertyChangeListener.class,
EntityInterceptorListener.class })
public abstract class AbstractJpaBaseEntity implements BaseEntity {
private static final long serialVersionUID = 1L;

View File

@@ -0,0 +1,69 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.model;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import org.eclipse.hawkbit.repository.jpa.model.helper.EntityInterceptorHolder;
/**
* Entity listener which calls all entity interceptor for the lifecyles
* callbacks.
*/
public class EntityInterceptorListener {
@PrePersist
protected void prePersist(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.prePersist(entity));
}
@PostPersist
protected void postPersist(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.postPersist(entity));
}
@PostRemove
protected void postRemove(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.postRemove(entity));
}
@PreRemove
protected void preRemove(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.preRemove(entity));
}
@PostLoad
protected void postLoad(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.postLoad(entity));
}
@PreUpdate
protected void preUpdate(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.preUpdate(entity));
}
@PostUpdate
protected void postUpdate(final Object entity) {
EntityInterceptorHolder.getInstance().getEntityInterceptors()
.forEach(interceptor -> interceptor.postUpdate(entity));
}
}

View File

@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.model.helper;
import java.util.List;
import org.eclipse.hawkbit.repository.model.EntityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
/**
* A singleton bean which holds the {@link EntityInterceptor} to have all
* interceptors in spring beans.
*
*/
public final class EntityInterceptorHolder {
private static final EntityInterceptorHolder SINGLETON = new EntityInterceptorHolder();
@Autowired
private List<EntityInterceptor> entityInterceptors;
private EntityInterceptorHolder() {
}
/**
* @return the entity intreceptor holder singleton instance
*/
public static EntityInterceptorHolder getInstance() {
return SINGLETON;
}
public List<EntityInterceptor> getEntityInterceptors() {
return entityInterceptors;
}
}