DATADOC-170 - Streamlined AbstractMongoEventListener.

Renamed Abstract{Mapping => Mongo}EventListener. Removed generic typing for the MongoMappingEvent and stick to domain type generification only.
This commit is contained in:
Oliver Gierke
2011-08-29 22:44:50 +02:00
parent df10bb2168
commit 44def7dddb
5 changed files with 108 additions and 34 deletions

View File

@@ -13,42 +13,54 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.mapping.event;
import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.GenericTypeResolver;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
* Base class to implement domain class specific {@link ApplicationListener}s.
*
* @author Jon Brisbin
* @author Oliver Gierke
*/
public abstract class AbstractMappingEventListener<T extends ApplicationEvent, E> implements ApplicationListener<T> {
public abstract class AbstractMongoEventListener<E> implements ApplicationListener<MongoMappingEvent<E>> {
protected final Log log = LogFactory.getLog(getClass());
private final Class<?> domainClass;
/**
* Creates a new {@link AbstractMongoEventListener}.
*/
public AbstractMongoEventListener() {
this.domainClass = GenericTypeResolver.resolveTypeArgument(this.getClass(), AbstractMongoEventListener.class);
}
@SuppressWarnings({ "unchecked" })
public void onApplicationEvent(T appEvent) {
if (appEvent instanceof MongoMappingEvent) {
try {
MongoMappingEvent<E> event = (MongoMappingEvent<E>) appEvent;
if (event instanceof BeforeConvertEvent) {
onBeforeConvert(event.getSource());
} else if (event instanceof BeforeSaveEvent) {
onBeforeSave(event.getSource(), event.getDBObject());
} else if (event instanceof AfterSaveEvent) {
onAfterSave(event.getSource(), event.getDBObject());
} else if (event instanceof AfterLoadEvent) {
onAfterLoad((DBObject) event.getSource());
} else if (event instanceof AfterConvertEvent) {
onAfterConvert(event.getDBObject(), event.getSource());
}
} catch (ClassCastException e) {
// Not a mapping event for this entity, apparently.
// Just ignore it for now.
}
/*
* (non-Javadoc)
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
*/
public void onApplicationEvent(MongoMappingEvent<E> event) {
E source = event.getSource();
if (source != null && !domainClass.isAssignableFrom(source.getClass())) {
return;
}
if (event instanceof BeforeConvertEvent) {
onBeforeConvert(source);
} else if (event instanceof BeforeSaveEvent) {
onBeforeSave(source, event.getDBObject());
} else if (event instanceof AfterSaveEvent) {
onAfterSave(source, event.getDBObject());
} else if (event instanceof AfterLoadEvent) {
onAfterLoad((DBObject) event.getSource());
} else if (event instanceof AfterConvertEvent) {
onAfterConvert(event.getDBObject(), source);
}
}
@@ -81,5 +93,4 @@ public abstract class AbstractMappingEventListener<T extends ApplicationEvent, E
log.debug("onAfterConvert(" + dbo + "," + source + ")");
}
}
}

View File

@@ -22,8 +22,7 @@ import org.apache.commons.logging.LogFactory;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@SuppressWarnings({ "hiding", "rawtypes" })
public class LoggingEventListener<MongoMappingEvent> extends AbstractMappingEventListener {
public class LoggingEventListener extends AbstractMongoEventListener<Object> {
private Log log = LogFactory.getLog(getClass());

View File

@@ -20,7 +20,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
@Configuration
public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
@@ -37,9 +36,8 @@ public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
}
@Bean
@SuppressWarnings("rawtypes")
public LoggingEventListener<MongoMappingEvent> mappingEventsListener() {
return new LoggingEventListener<MongoMappingEvent>();
public LoggingEventListener mappingEventsListener() {
return new LoggingEventListener();
}
@Override

View File

@@ -4,7 +4,6 @@ import com.mongodb.Mongo;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
public class GeoIndexedAppConfig extends AbstractMongoConfiguration {
@@ -28,7 +27,7 @@ public class GeoIndexedAppConfig extends AbstractMongoConfiguration {
}
@Bean
public LoggingEventListener<MongoMappingEvent<?>> mappingEventsListener() {
return new LoggingEventListener<MongoMappingEvent<?>>();
public LoggingEventListener mappingEventsListener() {
return new LoggingEventListener();
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (c) 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.mapping.event;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.repository.Person;
/**
* Unit tests for {@link AbstractMongoEventListener}.
*
* @author Oliver Gierke
*/
public class AbstractMongoEventListenerUnitTest {
@Test
public void invokesCallbackForEventForPerson() {
MongoMappingEvent<Person> event = new BeforeConvertEvent<Person>(new Person("Dave", "Matthews"));
SampleEventListener listener = new SampleEventListener();
listener.onApplicationEvent(event);
assertThat(listener.invoked, is(true));
}
@Test
public void dropsEventIfNotForCorrectDomainType() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.refresh();
SampleEventListener listener = new SampleEventListener();
context.addApplicationListener(listener);
context.publishEvent(new BeforeConvertEvent<Person>(new Person("Dave", "Matthews")));
assertThat(listener.invoked, is(true));
listener.invoked = false;
context.publishEvent(new BeforeConvertEvent<String>("Test"));
assertThat(listener.invoked, is(false));
}
class SampleEventListener extends AbstractMongoEventListener<Person> {
boolean invoked;
@Override
public void onBeforeConvert(Person source) {
invoked = true;
}
}
}