1 package org.apache.turbine.annotation;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
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 * AnnotationProcessor contains static helper methods that handle the
39 * Turbine annotations for objects
40 *
41 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
42 * @version $Id: TurbineAssemblerBrokerService.java 1521103 2013-09-09 13:38:07Z tv $
43 */
44 public class AnnotationProcessor
45 {
46 /** Logging */
47 private static Log log
48 = LogFactory.getLog(AnnotationProcessor.class);
49
50 /**
51 * Search for annotated fields of the object and provide them with the
52 * appropriate TurbineService
53 *
54 * @param object the object
55 * @throws TurbineException if the service could not be injected
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 // Check for annotation value
73 if (StringUtils.isNotEmpty(sa.value()))
74 {
75 serviceName = sa.value();
76 }
77 // Check for fields SERVICE_NAME and ROLE
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 // Try interface class name
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); // throws Exception on unknown service
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 // Check for annotation value
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 }