1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.fatwire.gst.foundation.mapping;
17
18 import java.lang.reflect.Field;
19 import java.util.Map;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import COM.FutureTense.Util.ftErrors;
26
27 import com.fatwire.assetapi.data.AssetId;
28 import com.fatwire.gst.foundation.CSRuntimeException;
29 import com.fatwire.gst.foundation.DebugHelper;
30 import com.fatwire.gst.foundation.controller.AssetIdWithSite;
31 import com.fatwire.gst.foundation.controller.action.AnnotationInjector;
32 import com.fatwire.gst.foundation.controller.action.Factory;
33 import com.fatwire.gst.foundation.controller.annotation.Mapping;
34 import com.fatwire.gst.foundation.controller.annotation.Mapping.Match;
35 import com.fatwire.gst.foundation.mapping.MappingValue.Type;
36 import com.openmarket.xcelerate.asset.AssetIdImpl;
37
38
39
40
41
42 public class MappingInjector {
43 protected static final Log LOG = LogFactory.getLog(MappingInjector.class.getPackage().getName());
44
45 public final static void inject(final Object object, final Factory factory, final AssetIdWithSite id) {
46 if (object == null) {
47 throw new IllegalArgumentException("object cannot be null.");
48 }
49 if (factory == null) {
50 throw new IllegalArgumentException("factory cannot be null.");
51 }
52 final long start = System.nanoTime();
53 try {
54 final Field[] fields = AnnotationInjector.findFieldsWithAnnotation(object, Mapping.class);
55
56 if (fields.length > 0) {
57 final MappingService mappingService = factory.getObject("mappingService", MappingService.class);
58 if (mappingService == null) {
59 throw new IllegalStateException("MappingService can not be retrieved from "
60 + factory.getClass().getName());
61 }
62 final Map<String, MappingValue> map = mappingService.readMapping(id);
63 for (final Field field : fields) {
64 injectIntoField(object, map, field, id);
65 }
66 }
67 } finally {
68 DebugHelper.printTime("inject mapping for " + object.getClass().getName(), start);
69 }
70 }
71
72 private static void injectIntoField(final Object object, final Map<String, MappingValue> map, final Field field,
73 final AssetIdWithSite id) throws SecurityException {
74
75 final Mapping ifr = field.getAnnotation(Mapping.class);
76
77 String name = ifr.value();
78 if (StringUtils.isBlank(name)) {
79 name = field.getName();
80 }
81
82 final MappingValue value = map.get(name);
83 if (value == null) {
84 throw new CSRuntimeException("Can't find a value for mapping " + name + " for asset " + id,
85 ftErrors.badparams);
86 }
87 Object injectionValue;
88
89 if (MappingValue.class.isAssignableFrom(field.getType())) {
90 injectionValue = value;
91 } else if (AssetId.class.isAssignableFrom(field.getType()) && value.getType() == Type.asset) {
92 injectionValue = new AssetIdImpl(value.getLeft(), Long.parseLong(value.getRight()));
93 } else if (AssetName.class.isAssignableFrom(field.getType()) && value.getType() == Type.assetname) {
94 injectionValue = new AssetName(value.getLeft(), value.getRight());
95 } else {
96 injectionValue = value.getValue();
97 final Match what = ifr.match();
98 switch (what) {
99 case left:
100 injectionValue = value.getLeft();
101 break;
102 case right:
103 injectionValue = value.getRight();
104 break;
105 case all:
106 break;
107 default:
108 break;
109
110 }
111 }
112 if (injectionValue == null) {
113 throw new CSRuntimeException("No value found to map '" + field.getType().getName() + "' into the field '"
114 + field.getName() + "' for an action " + object.getClass().getName(), ftErrors.badparams);
115 }
116 field.setAccessible(true);
117 if (LOG.isDebugEnabled()) {
118 LOG.debug("Injecting " + injectionValue.getClass().getName() + " into field " + field.getName()
119 + " of type " + field.getType().getName() + " for " + object.getClass().getName());
120 }
121 try {
122 field.set(object, injectionValue);
123 } catch (final IllegalArgumentException e) {
124 throw new CSRuntimeException("IllegalArgumentException injecting " + injectionValue + " into field "
125 + field.getName(), ftErrors.exceptionerr, e);
126 } catch (final IllegalAccessException e) {
127 throw new CSRuntimeException("IllegalAccessException injecting " + injectionValue + " into field "
128 + field.getName(), ftErrors.exceptionerr, e);
129 }
130 }
131
132 }