1 package org.apache.turbine.annotation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.lang.reflect.Field;
25
26 import org.apache.commons.configuration.Configuration;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.turbine.Turbine;
31 import org.apache.turbine.modules.Loader;
32 import org.apache.turbine.services.ServiceManager;
33 import org.apache.turbine.services.TurbineServices;
34 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
35 import org.apache.turbine.util.TurbineException;
36
37
38
39
40
41
42
43
44 public class AnnotationProcessor
45 {
46
47 private static Log log
48 = LogFactory.getLog(AnnotationProcessor.class);
49
50
51
52
53
54
55
56
57 public static void process(Object object) throws TurbineException
58 {
59 ServiceManager manager = TurbineServices.getInstance();
60 Class<?> clazz = object.getClass();
61
62 while (clazz != null)
63 {
64 Field[] fields = clazz.getDeclaredFields();
65
66 for (Field field : fields)
67 {
68 if (field.isAnnotationPresent(TurbineService.class))
69 {
70 TurbineService sa = field.getAnnotation(TurbineService.class);
71 String serviceName = null;
72
73 if (StringUtils.isNotEmpty(sa.value()))
74 {
75 serviceName = sa.value();
76 }
77
78 else
79 {
80 Field[] typeFields = field.getType().getFields();
81 for (Field f : typeFields)
82 {
83 if (TurbineService.SERVICE_NAME.equals(f.getName()))
84 {
85 try
86 {
87 serviceName = (String)f.get(null);
88 }
89 catch (Exception e)
90 {
91 continue;
92 }
93 break;
94 }
95 else if (TurbineService.ROLE.equals(f.getName()))
96 {
97 try
98 {
99 serviceName = (String)f.get(null);
100 }
101 catch (Exception e)
102 {
103 continue;
104 }
105 break;
106 }
107 }
108 }
109
110 if (StringUtils.isEmpty(serviceName))
111 {
112
113 serviceName = field.getType().getName();
114 }
115
116 if (log.isDebugEnabled())
117 {
118 log.debug("Looking up service for injection: " + serviceName + " for object " + object);
119 }
120
121 Object service = manager.getService(serviceName);
122 field.setAccessible(true);
123
124 try
125 {
126 if (log.isDebugEnabled())
127 {
128 log.debug("Injection of " + serviceName + " into object " + object);
129 }
130
131 field.set(object, service);
132 }
133 catch (IllegalArgumentException e)
134 {
135 throw new TurbineException("Could not inject service "
136 + serviceName + " into object " + object, e);
137 }
138 catch (IllegalAccessException e)
139 {
140 throw new TurbineException("Could not inject service "
141 + serviceName + " into object " + object, e);
142 }
143 }
144 else if (field.isAnnotationPresent(TurbineConfiguration.class))
145 {
146 TurbineConfiguration ca = field.getAnnotation(TurbineConfiguration.class);
147 Configuration conf = null;
148
149
150 if (StringUtils.isNotEmpty(ca.value()))
151 {
152 conf = Turbine.getConfiguration().subset(ca.value());
153 }
154 else
155 {
156 conf = Turbine.getConfiguration();
157 }
158
159 field.setAccessible(true);
160
161 try
162 {
163 if (log.isDebugEnabled())
164 {
165 log.debug("Injection of " + conf + " into object " + object);
166 }
167
168 field.set(object, conf);
169 }
170 catch (IllegalArgumentException e)
171 {
172 throw new TurbineException("Could not inject configuration "
173 + conf + " into object " + object, e);
174 }
175 catch (IllegalAccessException e)
176 {
177 throw new TurbineException("Could not inject configuration "
178 + conf + " into object " + object, e);
179 }
180 }
181 else if (field.isAnnotationPresent(TurbineLoader.class))
182 {
183 TurbineLoader la = field.getAnnotation(TurbineLoader.class);
184 Loader<?> loader = TurbineAssemblerBroker.getLoader(la.value());
185 field.setAccessible(true);
186
187 try
188 {
189 if (log.isDebugEnabled())
190 {
191 log.debug("Injection of " + loader + " into object " + object);
192 }
193
194 field.set(object, loader);
195 }
196 catch (IllegalArgumentException e)
197 {
198 throw new TurbineException("Could not inject loader "
199 + loader + " into object " + object, e);
200 }
201 catch (IllegalAccessException e)
202 {
203 throw new TurbineException("Could not inject loader "
204 + loader + " into object " + object, e);
205 }
206 }
207 }
208
209 clazz = clazz.getSuperclass();
210 }
211 }
212 }