##### Coverage Report - org.apache.turbine.annotation.AnnotationProcessor

Classes in this File | Line Coverage | Branch Coverage | Complexity
AnnotationProcessor |
72% | 51/70
73% | 22/30
| 30.0;30

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 | 0 |
     public class AnnotationProcessor
45 |  |
     {
46 |  |
         /** Logging */
47 | 31 |
         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 | 459 |
             ServiceManager manager = TurbineServices.getInstance();
60 | 459 |
             Class<?> clazz = object.getClass();
61 |  |
62 | 1990 |
             while (clazz != null)
63 |  |
             {
64 | 1531 |
                 Field[] fields = clazz.getDeclaredFields();
65 |  |
66 | 2764 |
                 for (Field field : fields)
67 |  |
                 {
68 | 1233 |
                     if (field.isAnnotationPresent(TurbineService.class))
69 |  |
                     {
70 | 106 |
                         TurbineService sa = field.getAnnotation(TurbineService.class);
71 | 106 |
                         String serviceName = null;
72 |  |
                         // Check for annotation value
73 | 106 |
                         if (StringUtils.isNotEmpty(sa.value()))
74 |  |
                         {
75 | 1 |
                             serviceName = sa.value();
76 |  |
                         }
77 |  |
                         // Check for fields SERVICE_NAME and ROLE
78 |  |
                         else
79 |  |
                         {
80 | 105 |
                             Field[] typeFields = field.getType().getFields();
81 | 105 |
                             for (Field f : typeFields)
82 |  |
                             {
83 | 105 |
                                 if (TurbineService.SERVICE_NAME.equals(f.getName()))
84 |  |
                                 {
85 |  |
                                     try
86 |  |
                                     {
87 | 104 |
                                         serviceName = (String)f.get(null);
88 |  |
                                     }
89 | 0 |
                                     catch (Exception e)
90 |  |
                                     {
91 | 0 |
                                         continue;
92 | 104 |
                                     }
93 |  |
                                     break;
94 |  |
                                 }
95 | 1 |
                                 else if (TurbineService.ROLE.equals(f.getName()))
96 |  |
                                 {
97 |  |
                                     try
98 |  |
                                     {
99 | 1 |
                                         serviceName = (String)f.get(null);
100 |  |
                                     }
101 | 0 |
                                     catch (Exception e)
102 |  |
                                     {
103 | 0 |
                                         continue;
104 | 1 |
                                     }
105 |  |
                                     break;
106 |  |
                                 }
107 |  |
                             }
108 |  |
                         }
109 |  |
110 | 106 |
                         if (StringUtils.isEmpty(serviceName))
111 |  |
                         {
112 |  |
                             // Try interface class name
113 | 0 |
                             serviceName = field.getType().getName();
114 |  |
                         }
115 |  |
116 | 106 |
                         if (log.isDebugEnabled())
117 |  |
                         {
118 | 106 |
                             log.debug("Looking up service for injection: " + serviceName + " for object " + object);
119 |  |
                         }
120 |  |
121 | 106 |
                         Object service = manager.getService(serviceName); // throws Exception on unknown service
122 | 106 |
                         field.setAccessible(true);
123 |  |
124 |  |
                         try
125 |  |
                         {
126 | 106 |
                             if (log.isDebugEnabled())
127 |  |
                             {
128 | 106 |
                                 log.debug("Injection of " + serviceName + " into object " + object);
129 |  |
                             }
130 |  |
131 | 106 |
                             field.set(object, service);
132 |  |
                         }
133 | 0 |
                         catch (IllegalArgumentException e)
134 |  |
                         {
135 | 0 |
                             throw new TurbineException("Could not inject service "
136 |  |
                                     + serviceName + " into object " + object, e);
137 |  |
                         }
138 | 0 |
                         catch (IllegalAccessException e)
139 |  |
                         {
140 | 0 |
                             throw new TurbineException("Could not inject service "
141 |  |
                                     + serviceName + " into object " + object, e);
142 | 106 |
                         }
143 | 106 |
                     }
144 | 1127 |
                     else if (field.isAnnotationPresent(TurbineConfiguration.class))
145 |  |
                     {
146 | 185 |
                         TurbineConfiguration ca = field.getAnnotation(TurbineConfiguration.class);
147 | 185 |
                         Configuration conf = null;
148 |  |
149 |  |
                         // Check for annotation value
150 | 185 |
                         if (StringUtils.isNotEmpty(ca.value()))
151 |  |
                         {
152 | 0 |
                             conf = Turbine.getConfiguration().subset(ca.value());
153 |  |
                         }
154 |  |
                         else
155 |  |
                         {
156 | 185 |
                             conf = Turbine.getConfiguration();
157 |  |
                         }
158 |  |
159 | 185 |
                         field.setAccessible(true);
160 |  |
161 |  |
                         try
162 |  |
                         {
163 | 185 |
                             if (log.isDebugEnabled())
164 |  |
                             {
165 | 185 |
                                 log.debug("Injection of " + conf + " into object " + object);
166 |  |
                             }
167 |  |
168 | 185 |
                             field.set(object, conf);
169 |  |
                         }
170 | 0 |
                         catch (IllegalArgumentException e)
171 |  |
                         {
172 | 0 |
                             throw new TurbineException("Could not inject configuration "
173 |  |
                                     + conf + " into object " + object, e);
174 |  |
                         }
175 | 0 |
                         catch (IllegalAccessException e)
176 |  |
                         {
177 | 0 |
                             throw new TurbineException("Could not inject configuration "
178 |  |
                                     + conf + " into object " + object, e);
179 | 185 |
                         }
180 | 185 |
                     }
181 | 942 |
                     else if (field.isAnnotationPresent(TurbineLoader.class))
182 |  |
                     {
183 | 202 |
                         TurbineLoader la = field.getAnnotation(TurbineLoader.class);
184 | 202 |
                         Loader<?> loader = TurbineAssemblerBroker.getLoader(la.value());
185 | 202 |
                         field.setAccessible(true);
186 |  |
187 |  |
                         try
188 |  |
                         {
189 | 202 |
                             if (log.isDebugEnabled())
190 |  |
                             {
191 | 202 |
                                 log.debug("Injection of " + loader + " into object " + object);
192 |  |
                             }
193 |  |
194 | 202 |
                             field.set(object, loader);
195 |  |
                         }
196 | 0 |
                         catch (IllegalArgumentException e)
197 |  |
                         {
198 | 0 |
                             throw new TurbineException("Could not inject loader "
199 |  |
                                     + loader + " into object " + object, e);
200 |  |
                         }
201 | 0 |
                         catch (IllegalAccessException e)
202 |  |
                         {
203 | 0 |
                             throw new TurbineException("Could not inject loader "
204 |  |
                                     + loader + " into object " + object, e);
205 | 202 |
                         }
206 |  |
                     }
207 |  |
                 }
208 |  |
209 | 1531 |
                 clazz = clazz.getSuperclass();
210 | 1531 |
             }
211 | 459 |
         }
212 |  |
     }