| 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 | 0 | public class AnnotationProcessor |
| 45 | |
{ |
| 46 | |
|
| 47 | 31 | 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 | 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 | |
|
| 73 | 106 | if (StringUtils.isNotEmpty(sa.value())) |
| 74 | |
{ |
| 75 | 1 | serviceName = sa.value(); |
| 76 | |
} |
| 77 | |
|
| 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 | |
|
| 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); |
| 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 | |
|
| 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 | |
} |