From 99739658b8235a974e0704aa14695e5b878e53cc Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 4 Dec 2014 19:11:24 +0100 Subject: [PATCH] DATAREST-388 - Improved annotation based event handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../AnnotatedHandlerBeanPostProcessor.java | 53 ++++++++------ .../RepositoryEventIntegrationTests.java | 70 +++++++++++++++---- .../jpa/AnnotatedPersonEventHandler.java | 25 +++++-- .../jpa/EventHandlerInvokedException.java | 24 +++++++ .../domain/jpa/PersonBeforeSaveHandler.java | 23 +++++- 5 files changed, 157 insertions(+), 38 deletions(-) create mode 100644 spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/EventHandlerInvokedException.java diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedHandlerBeanPostProcessor.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedHandlerBeanPostProcessor.java index 62e434cb6..c868e2205 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedHandlerBeanPostProcessor.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedHandlerBeanPostProcessor.java @@ -30,12 +30,17 @@ import org.springframework.util.ReflectionUtils; /** * @author Jon Brisbin + * @author Oliver Gierke */ public class AnnotatedHandlerBeanPostProcessor implements ApplicationListener, BeanPostProcessor { private static final Logger LOG = LoggerFactory.getLogger(AnnotatedHandlerBeanPostProcessor.class); private final MultiValueMap, EventHandlerMethod> handlerMethods = new LinkedMultiValueMap, AnnotatedHandlerBeanPostProcessor.EventHandlerMethod>(); + /* + * (non-Javadoc) + * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) + */ @Override public void onApplicationEvent(RepositoryEvent event) { Class eventType = event.getClass(); @@ -44,47 +49,53 @@ public class AnnotatedHandlerBeanPostProcessor implements ApplicationListener params = new ArrayList(); - 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 params = new ArrayList(); + 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 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); } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java index e1ac7aedc..12faed074 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/context/RepositoryEventIntegrationTests.java @@ -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())); } - } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AnnotatedPersonEventHandler.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AnnotatedPersonEventHandler.java index 1bba59f9f..df193a323 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AnnotatedPersonEventHandler.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AnnotatedPersonEventHandler.java @@ -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(); } } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/EventHandlerInvokedException.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/EventHandlerInvokedException.java new file mode 100644 index 000000000..0d5cb8678 --- /dev/null +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/EventHandlerInvokedException.java @@ -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; +} diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/PersonBeforeSaveHandler.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/PersonBeforeSaveHandler.java index 83d06ce6c..df09424da 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/PersonBeforeSaveHandler.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/PersonBeforeSaveHandler.java @@ -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 { + + /* + * (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(); } }