KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > annotations > AnnotationEnhancementWorker


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.annotations;
16
17 import java.lang.annotation.Annotation JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.Resource;
25 import org.apache.hivemind.util.ClasspathResource;
26 import org.apache.tapestry.enhance.EnhancementOperation;
27 import org.apache.tapestry.enhance.EnhancementWorker;
28 import org.apache.tapestry.spec.IComponentSpecification;
29
30 /**
31  * Implementation of {@link org.apache.tapestry.enhance.EnhancementWorker} that finds class and
32  * method annotations and delegates out to specific
33  * {@link org.apache.tapestry.annotations.ClassAnnotationEnhancementWorker} and
34  * {@link org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker} instances.
35  *
36  * @author Howard M. Lewis Ship
37  * @since 4.0
38  */

39 public class AnnotationEnhancementWorker implements EnhancementWorker
40 {
41     private ClassResolver _classResolver;
42
43     private ErrorLog _errorLog;
44
45     private Map JavaDoc _methodWorkers;
46
47     private Map JavaDoc _classWorkers;
48
49     public void setClassWorkers(Map JavaDoc classWorkers)
50     {
51         _classWorkers = classWorkers;
52     }
53
54     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
55     {
56         Class JavaDoc clazz = op.getBaseClass();
57
58         Resource classResource = newClassResource(clazz);
59
60         for (Annotation JavaDoc a : clazz.getAnnotations())
61         {
62             performClassEnhancement(op, spec, clazz, a, classResource);
63         }
64
65         for (Method JavaDoc m : clazz.getMethods())
66         {
67             performMethodEnhancement(op, spec, m, classResource);
68         }
69     }
70
71     private ClasspathResource newClassResource(Class JavaDoc clazz)
72     {
73         return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/'));
74     }
75
76     void performClassEnhancement(EnhancementOperation op, IComponentSpecification spec,
77             Class JavaDoc clazz, Annotation JavaDoc annotation, Resource classResource)
78     {
79         ClassAnnotationEnhancementWorker worker = (ClassAnnotationEnhancementWorker) _classWorkers
80                 .get(annotation.annotationType());
81
82         if (worker == null)
83             return;
84
85         try
86         {
87             Location location = new AnnotationLocation(classResource, AnnotationMessages
88                     .classAnnotation(annotation, clazz));
89
90             worker.performEnhancement(op, spec, clazz, location);
91         }
92         catch (Exception JavaDoc ex)
93         {
94             _errorLog.error(AnnotationMessages.failureProcessingClassAnnotation(
95                     annotation,
96                     clazz,
97                     ex), null, ex);
98         }
99
100     }
101
102     void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec,
103             Method JavaDoc method, Resource classResource)
104     {
105         for (Annotation JavaDoc a : method.getAnnotations())
106         {
107             performMethodEnhancement(op, spec, method, a, classResource);
108         }
109     }
110
111     void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec,
112             Method JavaDoc method, Annotation JavaDoc annotation, Resource classResource)
113     {
114         MethodAnnotationEnhancementWorker worker = (MethodAnnotationEnhancementWorker) _methodWorkers
115                 .get(annotation.annotationType());
116
117         if (worker == null)
118             return;
119
120         try
121         {
122             Location location = new AnnotationLocation(classResource, AnnotationMessages
123                     .methodAnnotation(annotation, method));
124             worker.performEnhancement(op, spec, method, location);
125         }
126         catch (Exception JavaDoc ex)
127         {
128             _errorLog.error(
129                     AnnotationMessages.failureProcessingAnnotation(annotation, method, ex),
130                     null,
131                     ex);
132         }
133
134     }
135
136     public void setMethodWorkers(Map JavaDoc methodWorkers)
137     {
138         _methodWorkers = methodWorkers;
139     }
140
141     public void setErrorLog(ErrorLog errorLog)
142     {
143         _errorLog = errorLog;
144     }
145
146     public void setClassResolver(ClassResolver classResolver)
147     {
148         _classResolver = classResolver;
149     }
150 }
151
Popular Tags