1 package org.apache.onami.lifecycle.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.google.inject.AbstractModule;
23 import com.google.inject.ProvisionException;
24 import com.google.inject.TypeLiteral;
25 import com.google.inject.matcher.Matcher;
26 import com.google.inject.spi.InjectionListener;
27 import com.google.inject.spi.TypeEncounter;
28
29 import java.lang.annotation.Annotation;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.util.List;
33
34 import static com.google.inject.matcher.Matchers.any;
35 import static java.lang.String.format;
36 import static java.util.Arrays.asList;
37
38
39
40
41 public abstract class LifeCycleModule
42 extends AbstractModule
43 {
44
45
46
47
48
49
50 protected final void bindLifeCycle( Class<? extends Annotation> annotation )
51 {
52 bindLifeCycle( annotation, any() );
53 }
54
55
56
57
58
59
60
61 protected final void bindLifeCycle( Class<? extends Annotation> annotation, Matcher<? super TypeLiteral<?>> typeMatcher )
62 {
63 bindLifeCycle( asList( annotation ), typeMatcher );
64 }
65
66
67
68
69
70
71
72 protected final void bindLifeCycle( List<? extends Class<? extends Annotation>> annotations, Matcher<? super TypeLiteral<?>> typeMatcher )
73 {
74 bindListener( typeMatcher, new AbstractMethodTypeListener( annotations )
75 {
76
77 @Override
78 protected <I> void hear( final Method method, TypeLiteral<I> parentType, TypeEncounter<I> encounter,
79 final Class<? extends Annotation> annotationType )
80 {
81 encounter.register( new InjectionListener<I>()
82 {
83
84 @Override
85 public void afterInjection( I injectee )
86 {
87 try
88 {
89 method.invoke( injectee );
90 }
91 catch ( IllegalArgumentException e )
92 {
93
94 throw new ProvisionException(
95 format( "Method @%s %s requires arguments", annotationType.getName(), method ), e );
96 }
97 catch ( IllegalAccessException e )
98 {
99 throw new ProvisionException(
100 format( "Impossible to access to @%s %s on %s", annotationType.getName(), method,
101 injectee ), e );
102 }
103 catch ( InvocationTargetException e )
104 {
105 throw new ProvisionException(
106 format( "An error occurred while invoking @%s %s on %s", annotationType.getName(),
107 method, injectee ), e.getCause() );
108 }
109 }
110
111 } );
112 }
113
114 } );
115 }
116
117 }