1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.annotations; |
16 | |
|
17 | |
import java.lang.annotation.Annotation; |
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.List; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.apache.hivemind.ClassResolver; |
23 | |
import org.apache.hivemind.ErrorLog; |
24 | |
import org.apache.hivemind.Location; |
25 | |
import org.apache.hivemind.Resource; |
26 | |
import org.apache.hivemind.util.ClasspathResource; |
27 | |
import org.apache.tapestry.enhance.EnhancementOperation; |
28 | |
import org.apache.tapestry.enhance.EnhancementWorker; |
29 | |
import org.apache.tapestry.spec.IComponentSpecification; |
30 | |
import org.apache.tapestry.util.DescribedLocation; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class AnnotationEnhancementWorker implements EnhancementWorker |
42 | |
{ |
43 | |
private ClassResolver _classResolver; |
44 | |
|
45 | |
private ErrorLog _errorLog; |
46 | |
|
47 | |
private Map<Class, MethodAnnotationEnhancementWorker> _methodWorkers; |
48 | |
|
49 | |
private Map<Class, ClassAnnotationEnhancementWorker> _classWorkers; |
50 | |
|
51 | |
private List<SecondaryAnnotationWorker> _secondaryAnnotationWorkers; |
52 | |
|
53 | |
public void setClassWorkers(Map<Class, ClassAnnotationEnhancementWorker> classWorkers) |
54 | |
{ |
55 | 0 | _classWorkers = classWorkers; |
56 | 0 | } |
57 | |
|
58 | |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
59 | |
{ |
60 | 0 | Class clazz = op.getBaseClass(); |
61 | |
|
62 | 0 | Resource classResource = newClassResource(clazz); |
63 | |
|
64 | 0 | for (Annotation a : clazz.getAnnotations()) |
65 | |
{ |
66 | 0 | performClassEnhancement(op, spec, clazz, a, classResource); |
67 | |
} |
68 | |
|
69 | 0 | for (Method m : clazz.getMethods()) |
70 | |
{ |
71 | 0 | performMethodEnhancement(op, spec, m, classResource); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
private ClasspathResource newClassResource(Class clazz) |
76 | |
{ |
77 | 0 | return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/')); |
78 | |
} |
79 | |
|
80 | |
void performClassEnhancement(EnhancementOperation op, IComponentSpecification spec, |
81 | |
Class clazz, Annotation annotation, Resource classResource) |
82 | |
{ |
83 | 0 | ClassAnnotationEnhancementWorker worker = _classWorkers.get(annotation.annotationType()); |
84 | |
|
85 | 0 | if (worker == null) |
86 | 0 | return; |
87 | |
|
88 | |
try |
89 | |
{ |
90 | 0 | Location location = new DescribedLocation(classResource, AnnotationMessages.classAnnotation(annotation, clazz)); |
91 | |
|
92 | 0 | worker.performEnhancement(op, spec, clazz, location); |
93 | |
} |
94 | 0 | catch (Exception ex) |
95 | |
{ |
96 | 0 | _errorLog.error(AnnotationMessages.failureProcessingClassAnnotation( |
97 | |
annotation, |
98 | |
clazz, |
99 | |
ex), null, ex); |
100 | 0 | } |
101 | |
|
102 | 0 | } |
103 | |
|
104 | |
void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, |
105 | |
Method method, Resource classResource) |
106 | |
{ |
107 | 0 | for (Annotation a : method.getAnnotations()) |
108 | |
{ |
109 | 0 | performMethodEnhancement(op, spec, method, a, classResource); |
110 | |
} |
111 | |
|
112 | |
try |
113 | |
{ |
114 | |
|
115 | 0 | for (SecondaryAnnotationWorker worker : _secondaryAnnotationWorkers) |
116 | 0 | if (worker.canEnhance(method)) |
117 | 0 | worker.peformEnhancement(op, spec, method, classResource); |
118 | |
|
119 | |
} |
120 | 0 | catch (Exception ex) |
121 | |
{ |
122 | 0 | _errorLog.error(AnnotationMessages.failureEnhancingMethod(method, ex), null, ex); |
123 | 0 | } |
124 | 0 | } |
125 | |
|
126 | |
void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, |
127 | |
Method method, Annotation annotation, Resource classResource) |
128 | |
{ |
129 | 0 | MethodAnnotationEnhancementWorker worker = _methodWorkers.get(annotation.annotationType()); |
130 | |
|
131 | 0 | if (worker == null) |
132 | 0 | return; |
133 | |
|
134 | |
try |
135 | |
{ |
136 | 0 | Location location = AnnotationUtils.buildLocationForAnnotation( |
137 | |
method, |
138 | |
annotation, |
139 | |
classResource); |
140 | 0 | worker.performEnhancement(op, spec, method, location); |
141 | |
} |
142 | 0 | catch (Exception ex) |
143 | |
{ |
144 | 0 | _errorLog.error( |
145 | |
AnnotationMessages.failureProcessingAnnotation(annotation, method, ex), |
146 | |
null, |
147 | |
ex); |
148 | 0 | } |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
public void setMethodWorkers(Map<Class, MethodAnnotationEnhancementWorker> methodWorkers) |
153 | |
{ |
154 | 0 | _methodWorkers = methodWorkers; |
155 | 0 | } |
156 | |
|
157 | |
public void setErrorLog(ErrorLog errorLog) |
158 | |
{ |
159 | 0 | _errorLog = errorLog; |
160 | 0 | } |
161 | |
|
162 | |
public void setClassResolver(ClassResolver classResolver) |
163 | |
{ |
164 | 0 | _classResolver = classResolver; |
165 | 0 | } |
166 | |
|
167 | |
public void setSecondaryAnnotationWorkers(List<SecondaryAnnotationWorker> workers) |
168 | |
{ |
169 | 0 | _secondaryAnnotationWorkers = workers; |
170 | 0 | } |
171 | |
} |