DATAREST-388 - Improved annotation based event handling.
We're now using ReflectionUtils.invokeMethod(…) to avoid the exceptions being thrown from reflectively-invoked event handler methods to be wrapped in IllegalStateExceptions. Related pull request: #151.
This commit is contained in:
@@ -30,12 +30,17 @@ import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AnnotatedHandlerBeanPostProcessor implements ApplicationListener<RepositoryEvent>, BeanPostProcessor {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AnnotatedHandlerBeanPostProcessor.class);
|
||||
private final MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod> handlerMethods = new LinkedMultiValueMap<Class<? extends RepositoryEvent>, AnnotatedHandlerBeanPostProcessor.EventHandlerMethod>();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(RepositoryEvent event) {
|
||||
Class<? extends RepositoryEvent> eventType = event.getClass();
|
||||
@@ -44,47 +49,53 @@ public class AnnotatedHandlerBeanPostProcessor implements ApplicationListener<Re
|
||||
}
|
||||
|
||||
for (EventHandlerMethod handlerMethod : handlerMethods.get(eventType)) {
|
||||
try {
|
||||
Object src = event.getSource();
|
||||
|
||||
if (!ClassUtils.isAssignable(handlerMethod.targetType, src.getClass())) {
|
||||
continue;
|
||||
}
|
||||
Object src = event.getSource();
|
||||
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
params.add(src);
|
||||
if (event instanceof BeforeLinkSaveEvent) {
|
||||
params.add(((BeforeLinkSaveEvent) event).getLinked());
|
||||
} else if (event instanceof AfterLinkSaveEvent) {
|
||||
params.add(((AfterLinkSaveEvent) event).getLinked());
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Invoking " + event.getClass().getSimpleName() + " handler for " + event.getSource());
|
||||
}
|
||||
handlerMethod.method.invoke(handlerMethod.handler, params.toArray());
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
if (!ClassUtils.isAssignable(handlerMethod.targetType, src.getClass())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
params.add(src);
|
||||
|
||||
if (event instanceof LinkSaveEvent) {
|
||||
params.add(((LinkSaveEvent) event).getLinked());
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Invoking {} handler for {}.", event.getClass().getSimpleName(), event.getSource());
|
||||
}
|
||||
|
||||
ReflectionUtils.invokeMethod(handlerMethod.method, handlerMethod.handler, params.toArray());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
|
||||
final Class<?> beanType = bean.getClass();
|
||||
|
||||
RepositoryEventHandler typeAnno = AnnotationUtils.findAnnotation(beanType, RepositoryEventHandler.class);
|
||||
|
||||
if (null == typeAnno) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
Class<?>[] targetTypes = typeAnno.value();
|
||||
|
||||
if (targetTypes.length == 0) {
|
||||
targetTypes = new Class<?>[] { null };
|
||||
}
|
||||
@@ -130,7 +141,7 @@ public class AnnotatedHandlerBeanPostProcessor implements ApplicationListener<Re
|
||||
for (Class<?> type : targetTypes) {
|
||||
EventHandlerMethod m = new EventHandlerMethod(type, handler, method);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Annotated handler method found: " + m);
|
||||
LOG.debug("Annotated handler method found: {}", m);
|
||||
}
|
||||
handlerMethods.add(eventType, m);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* 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.rest.core.context;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -10,6 +25,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.rest.core.RepositoryTestsConfig;
|
||||
import org.springframework.data.rest.core.domain.jpa.AnnotatedPersonEventHandler;
|
||||
import org.springframework.data.rest.core.domain.jpa.EventHandlerInvokedException;
|
||||
import org.springframework.data.rest.core.domain.jpa.Person;
|
||||
import org.springframework.data.rest.core.domain.jpa.PersonBeforeSaveHandler;
|
||||
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
|
||||
@@ -32,6 +48,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* Tests around the {@link org.springframework.context.ApplicationEvent} handling abstractions.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@@ -53,7 +70,7 @@ public class RepositoryEventIntegrationTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AnnotatedHandlerBeanPostProcessor annotatedHandlerBeanPostProcessor() {
|
||||
public static AnnotatedHandlerBeanPostProcessor annotatedHandlerBeanPostProcessor() {
|
||||
return new AnnotatedHandlerBeanPostProcessor();
|
||||
}
|
||||
}
|
||||
@@ -67,54 +84,83 @@ public class RepositoryEventIntegrationTests {
|
||||
person = people.save(new Person("Jane", "Doe"));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchBeforeCreate() throws Exception {
|
||||
appCtx.publishEvent(new BeforeCreateEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchAfterCreate() throws Exception {
|
||||
appCtx.publishEvent(new AfterCreateEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchBeforeSave() throws Exception {
|
||||
appCtx.publishEvent(new BeforeSaveEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchAfterSave() throws Exception {
|
||||
appCtx.publishEvent(new AfterSaveEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchBeforeDelete() throws Exception {
|
||||
appCtx.publishEvent(new BeforeDeleteEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchAfterDelete() throws Exception {
|
||||
appCtx.publishEvent(new AfterDeleteEvent(person));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchBeforeLinkSave() throws Exception {
|
||||
appCtx.publishEvent(new BeforeLinkSaveEvent(person, new Object()));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchAfterLinkSave() throws Exception {
|
||||
appCtx.publishEvent(new AfterLinkSaveEvent(person, new Object()));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchBeforeLinkDelete() throws Exception {
|
||||
appCtx.publishEvent(new BeforeLinkDeleteEvent(person, new Object()));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
/**
|
||||
* @see DATAREST-388
|
||||
*/
|
||||
@Test(expected = EventHandlerInvokedException.class)
|
||||
public void shouldDispatchAfterLinkDelete() throws Exception {
|
||||
appCtx.publishEvent(new AfterLinkDeleteEvent(person, new Object()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.rest.core.domain.jpa;
|
||||
|
||||
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
|
||||
@@ -14,32 +29,34 @@ import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RepositoryEventHandler(Person.class)
|
||||
public class AnnotatedPersonEventHandler {
|
||||
|
||||
@HandleAfterCreate
|
||||
@HandleAfterDelete
|
||||
@HandleAfterSave
|
||||
public void handleAfter(Person p) {
|
||||
throw new RuntimeException();
|
||||
throw new EventHandlerInvokedException();
|
||||
}
|
||||
|
||||
@HandleAfterLinkDelete
|
||||
@HandleAfterLinkSave
|
||||
public void handleAfterLink(Person p, Object o) {
|
||||
throw new RuntimeException();
|
||||
throw new EventHandlerInvokedException();
|
||||
}
|
||||
|
||||
@HandleBeforeCreate
|
||||
@HandleBeforeDelete
|
||||
@HandleBeforeSave
|
||||
public void handleBefore(Person p) {
|
||||
throw new RuntimeException();
|
||||
throw new EventHandlerInvokedException();
|
||||
}
|
||||
|
||||
@HandleBeforeLinkDelete
|
||||
@HandleBeforeLinkSave
|
||||
public void handleBeforeLink(Person p, Object o) {
|
||||
throw new RuntimeException();
|
||||
throw new EventHandlerInvokedException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* 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.rest.core.domain.jpa;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class EventHandlerInvokedException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = -7879286986960261090L;
|
||||
}
|
||||
@@ -1,13 +1,34 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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.rest.core.domain.jpa;
|
||||
|
||||
import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PersonBeforeSaveHandler extends AbstractRepositoryEventListener<Person> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.rest.core.event.AbstractRepositoryEventListener#onBeforeSave(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
protected void onBeforeSave(Person person) {
|
||||
throw new RuntimeException();
|
||||
throw new EventHandlerInvokedException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user