-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0073471
commit 279817d
Showing
16 changed files
with
519 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 0 additions & 114 deletions
114
...oconfigure/src/main/java/org/seasar/doma/boot/event/AnnotatedDomaEventHandlerInvoker.java
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
...-boot-autoconfigure/src/main/java/org/seasar/doma/boot/event/DomaApplicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.seasar.doma.boot.event; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Collections; | ||
import java.util.IdentityHashMap; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import org.seasar.doma.Entity; | ||
import org.seasar.doma.boot.event.annotation.HandleDomaEvent; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
public class DomaApplicationListener implements ApplicationListener<DomaEvent<?, ?>> { | ||
|
||
private final Set<Class<?>> contextClasses; | ||
private final String beanName; | ||
private final Method method; | ||
private final BeanFactory beanFactory; | ||
|
||
public DomaApplicationListener(String beanName, Method method, BeanFactory beanFactory) { | ||
|
||
int parameterCount = method.getParameterCount(); | ||
if (parameterCount < 1) { | ||
throw new IllegalArgumentException("Must receive an entity"); | ||
} else if (parameterCount > 2) { | ||
throw new IllegalArgumentException("Too many parameters"); | ||
} | ||
|
||
Class<?> entityClass = method.getParameterTypes()[0]; | ||
if (entityClass.isAnnotationPresent(Entity.class) == false) { | ||
throw new IllegalArgumentException("First parameter must be entity class"); | ||
} | ||
|
||
Set<Class<?>> contextClasses = Collections.newSetFromMap(new IdentityHashMap<>()); | ||
Annotation[] annotations = AnnotationUtils.getAnnotations(method); | ||
for (Annotation annotation : annotations) { | ||
HandleDomaEvent handleDomaEvent = AnnotationUtils | ||
.findAnnotation(annotation.annotationType(), HandleDomaEvent.class); | ||
if (handleDomaEvent != null) { | ||
contextClasses.add(handleDomaEvent.contextClass()); | ||
} | ||
} | ||
|
||
if (parameterCount == 2) { | ||
if (contextClasses.size() > 1) { | ||
throw new IllegalArgumentException( | ||
"To annotate with multi annotations must be only entity parameter"); | ||
} | ||
|
||
Class<?> contextClass = method.getParameterTypes()[1]; | ||
if (contextClass != contextClasses.iterator().next()) { | ||
throw new IllegalArgumentException( | ||
"Mismatch between annotation and event context"); | ||
} | ||
Type t = method.getGenericParameterTypes()[1]; | ||
if (t instanceof ParameterizedType) { | ||
Type typeArg = ((ParameterizedType) t).getActualTypeArguments()[0]; | ||
if (typeArg != entityClass) { | ||
throw new IllegalArgumentException( | ||
"Mismatch between entity class and variable bound to event context"); | ||
} | ||
} | ||
} | ||
|
||
ReflectionUtils.makeAccessible(method); | ||
|
||
this.contextClasses = Objects.requireNonNull(contextClasses); | ||
this.beanName = Objects.requireNonNull(beanName); | ||
this.method = Objects.requireNonNull(method); | ||
this.beanFactory = Objects.requireNonNull(beanFactory); | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(DomaEvent<?, ?> event) { | ||
Object entity = event.getSource(); | ||
Object context = event.getContext(); | ||
if (shouldHandle(context.getClass()) | ||
&& entity.getClass() == method.getParameterTypes()[0]) { | ||
Object[] args; | ||
if (method.getParameterCount() == 1) { | ||
args = new Object[] { entity }; | ||
} else { | ||
args = new Object[] { entity, context }; | ||
} | ||
Object target = beanFactory.getBean(beanName); | ||
ReflectionUtils.invokeMethod(method, target, args); | ||
} | ||
} | ||
|
||
private boolean shouldHandle(Class<?> clazz) { | ||
for (Class<?> contextClasse : contextClasses) { | ||
if (contextClasse.isAssignableFrom(clazz)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...boot-autoconfigure/src/main/java/org/seasar/doma/boot/event/DomaEventListenerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.seasar.doma.boot.event; | ||
|
||
import java.lang.reflect.Method; | ||
import org.seasar.doma.boot.event.annotation.HandleDomaEvent; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.EventListenerFactory; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
|
||
public class DomaEventListenerFactory implements EventListenerFactory, Ordered, BeanFactoryAware { | ||
|
||
private int order = Ordered.HIGHEST_PRECEDENCE; | ||
private BeanFactory beanFactory; | ||
|
||
@Override | ||
public boolean supportsMethod(Method method) { | ||
return AnnotationUtils.findAnnotation(method, HandleDomaEvent.class) != null; | ||
} | ||
|
||
@Override | ||
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, | ||
Method method) { | ||
return new DomaApplicationListener(beanName, method, beanFactory); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return order; | ||
} | ||
|
||
public void setOrder(int order) { | ||
this.order = order; | ||
} | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = beanFactory; | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
...t-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/DomaEventHandler.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
...ot-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandleDomaEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.seasar.doma.boot.event.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.springframework.context.event.EventListener; | ||
|
||
@EventListener | ||
@Target({ ElementType.ANNOTATION_TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface HandleDomaEvent { | ||
Class<?> contextClass(); | ||
} |
2 changes: 2 additions & 0 deletions
2
...t-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePostDelete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...t-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePostInsert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...t-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePostUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ot-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePreDelete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ot-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePreInsert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ot-autoconfigure/src/main/java/org/seasar/doma/boot/event/annotation/HandlePreUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.