DATADOC-289 - Fixed invocation of AfterLoadEvent in AbstractMongoEventListener.
Invoke events that are not bound to the domain type before the domain type check. Removed unnecessary gentrification of AfterLoadEvent.
This commit is contained in:
@@ -1486,7 +1486,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
|
||||
public T doWith(DBObject object) {
|
||||
if (null != object) {
|
||||
maybeEmitEvent(new AfterLoadEvent<DBObject>(object));
|
||||
maybeEmitEvent(new AfterLoadEvent(object));
|
||||
}
|
||||
T source = reader.read(type, object);
|
||||
if (null != source) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
* Copyright 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,9 +27,9 @@ import org.springframework.core.GenericTypeResolver;
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class AbstractMongoEventListener<E> implements ApplicationListener<MongoMappingEvent<E>> {
|
||||
public abstract class AbstractMongoEventListener<E> implements ApplicationListener<MongoMappingEvent<?>> {
|
||||
|
||||
protected final Log log = LogFactory.getLog(getClass());
|
||||
protected final Log LOG = LogFactory.getLog(getClass());
|
||||
private final Class<?> domainClass;
|
||||
|
||||
/**
|
||||
@@ -43,54 +43,59 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
public void onApplicationEvent(MongoMappingEvent<E> event) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onApplicationEvent(MongoMappingEvent<?> event) {
|
||||
|
||||
E source = event.getSource();
|
||||
Object source = event.getSource();
|
||||
|
||||
// Invoke domain type independent events
|
||||
if (event instanceof AfterLoadEvent) {
|
||||
onAfterLoad(((AfterLoadEvent) event).getSource());
|
||||
}
|
||||
|
||||
// Check for matching domain type and invoke callbacks
|
||||
if (source != null && !domainClass.isAssignableFrom(source.getClass())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event instanceof BeforeConvertEvent) {
|
||||
onBeforeConvert(source);
|
||||
onBeforeConvert((E) source);
|
||||
} else if (event instanceof BeforeSaveEvent) {
|
||||
onBeforeSave(source, event.getDBObject());
|
||||
onBeforeSave((E) source, event.getDBObject());
|
||||
} else if (event instanceof AfterSaveEvent) {
|
||||
onAfterSave(source, event.getDBObject());
|
||||
} else if (event instanceof AfterLoadEvent) {
|
||||
onAfterLoad((DBObject) event.getSource());
|
||||
onAfterSave((E) source, event.getDBObject());
|
||||
} else if (event instanceof AfterConvertEvent) {
|
||||
onAfterConvert(event.getDBObject(), source);
|
||||
onAfterConvert(event.getDBObject(), (E) source);
|
||||
}
|
||||
}
|
||||
|
||||
public void onBeforeConvert(E source) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("onBeforeConvert(" + source + ")");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("onBeforeConvert(" + source + ")");
|
||||
}
|
||||
}
|
||||
|
||||
public void onBeforeSave(E source, DBObject dbo) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("onBeforeSave(" + source + ", " + dbo + ")");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("onBeforeSave(" + source + ", " + dbo + ")");
|
||||
}
|
||||
}
|
||||
|
||||
public void onAfterSave(E source, DBObject dbo) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("onAfterSave(" + source + ", " + dbo + ")");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("onAfterSave(" + source + ", " + dbo + ")");
|
||||
}
|
||||
}
|
||||
|
||||
public void onAfterLoad(DBObject dbo) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("onAfterLoad(" + dbo + ")");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("onAfterLoad(" + dbo + ")");
|
||||
}
|
||||
}
|
||||
|
||||
public void onAfterConvert(DBObject dbo, E source) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("onAfterConvert(" + dbo + "," + source + ")");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("onAfterConvert(" + dbo + "," + source + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
|
||||
package org.springframework.data.mongodb.core.mapping.event;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class AfterLoadEvent<DBObject> extends MongoMappingEvent<DBObject> {
|
||||
public class AfterLoadEvent extends MongoMappingEvent<DBObject> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
* Copyright 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,9 @@ import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.data.mongodb.repository.Person;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractMongoEventListener}.
|
||||
*
|
||||
@@ -34,7 +37,7 @@ public class AbstractMongoEventListenerUnitTest {
|
||||
MongoMappingEvent<Person> event = new BeforeConvertEvent<Person>(new Person("Dave", "Matthews"));
|
||||
SampleEventListener listener = new SampleEventListener();
|
||||
listener.onApplicationEvent(event);
|
||||
assertThat(listener.invoked, is(true));
|
||||
assertThat(listener.invokedOnBeforeConvert, is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -47,21 +50,38 @@ public class AbstractMongoEventListenerUnitTest {
|
||||
context.addApplicationListener(listener);
|
||||
|
||||
context.publishEvent(new BeforeConvertEvent<Person>(new Person("Dave", "Matthews")));
|
||||
assertThat(listener.invoked, is(true));
|
||||
assertThat(listener.invokedOnBeforeConvert, is(true));
|
||||
|
||||
listener.invoked = false;
|
||||
listener.invokedOnBeforeConvert = false;
|
||||
context.publishEvent(new BeforeConvertEvent<String>("Test"));
|
||||
assertThat(listener.invoked, is(false));
|
||||
|
||||
assertThat(listener.invokedOnBeforeConvert, is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-289
|
||||
*/
|
||||
@Test
|
||||
public void afterLoadEffectGetsHandledCorrectly() {
|
||||
|
||||
SampleEventListener listener = new SampleEventListener();
|
||||
listener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject()));
|
||||
assertThat(listener.invokedOnAfterLoad, is(true));
|
||||
}
|
||||
|
||||
|
||||
class SampleEventListener extends AbstractMongoEventListener<Person> {
|
||||
|
||||
boolean invoked;
|
||||
boolean invokedOnBeforeConvert;
|
||||
boolean invokedOnAfterLoad;
|
||||
|
||||
@Override
|
||||
public void onBeforeConvert(Person source) {
|
||||
invoked = true;
|
||||
invokedOnBeforeConvert = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAfterLoad(DBObject dbo) {
|
||||
invokedOnAfterLoad = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class SimpleMappingEventListener extends AbstractMongoEventListener<Objec
|
||||
public final ArrayList<BeforeConvertEvent<Object>> onBeforeConvertEvents = new ArrayList<BeforeConvertEvent<Object>>();
|
||||
public final ArrayList<BeforeSaveEvent<Object>> onBeforeSaveEvents = new ArrayList<BeforeSaveEvent<Object>>();
|
||||
public final ArrayList<AfterSaveEvent<Object>> onAfterSaveEvents = new ArrayList<AfterSaveEvent<Object>>();
|
||||
public final ArrayList<AfterLoadEvent<Object>> onAfterLoadEvents = new ArrayList<AfterLoadEvent<Object>>();
|
||||
public final ArrayList<AfterLoadEvent> onAfterLoadEvents = new ArrayList<AfterLoadEvent>();
|
||||
public final ArrayList<AfterConvertEvent<Object>> onAfterConvertEvents = new ArrayList<AfterConvertEvent<Object>>();
|
||||
|
||||
@Override
|
||||
@@ -45,7 +45,7 @@ public class SimpleMappingEventListener extends AbstractMongoEventListener<Objec
|
||||
|
||||
@Override
|
||||
public void onAfterLoad(DBObject dbo) {
|
||||
onAfterLoadEvents.add(new AfterLoadEvent<Object>(dbo));
|
||||
onAfterLoadEvents.add(new AfterLoadEvent(dbo));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user