1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.json.transformers; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.context.MuleContextAware; |
14 | |
import org.mule.api.lifecycle.Disposable; |
15 | |
import org.mule.api.registry.RegistrationException; |
16 | |
import org.mule.api.registry.ResolverException; |
17 | |
import org.mule.api.registry.TransformerResolver; |
18 | |
import org.mule.api.transformer.DataType; |
19 | |
import org.mule.api.transformer.Transformer; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
import org.mule.transformer.simple.ObjectToString; |
22 | |
|
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
import org.codehaus.jackson.map.ObjectMapper; |
26 | |
|
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
import java.util.WeakHashMap; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class JsonTransformerResolver implements TransformerResolver, MuleContextAware, Disposable |
43 | |
{ |
44 | |
public static final String JSON_MIME_TYPE = "application/json"; |
45 | |
|
46 | |
|
47 | |
|
48 | 0 | protected transient final Log logger = LogFactory.getLog(JsonTransformerResolver.class); |
49 | |
|
50 | |
private MuleContext muleContext; |
51 | |
|
52 | |
|
53 | 0 | private Map<String, Transformer> transformerCache = new WeakHashMap<String, Transformer>(); |
54 | |
|
55 | |
private JsonMapperResolver resolver; |
56 | |
|
57 | |
public void setMuleContext(MuleContext context) |
58 | |
{ |
59 | 0 | muleContext = context; |
60 | 0 | } |
61 | |
|
62 | |
public Transformer resolve(DataType<?> source, DataType<?> result) throws ResolverException |
63 | |
{ |
64 | |
|
65 | 0 | Transformer t = transformerCache.get(source.toString() + result.toString()); |
66 | |
|
67 | 0 | if (t != null) |
68 | |
{ |
69 | 0 | return t; |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 0 | ObjectMapper mapper = getMapperResolver().resolve(ObjectMapper.class, source, result, muleContext); |
75 | |
|
76 | 0 | if(mapper==null) |
77 | |
{ |
78 | 0 | return null; |
79 | |
} |
80 | |
boolean marshal; |
81 | |
Class<?> annotatedType; |
82 | |
|
83 | |
|
84 | 0 | if (getMapperResolver().getMatchingClasses().contains(result.getType())) |
85 | |
{ |
86 | 0 | annotatedType = result.getType(); |
87 | |
|
88 | 0 | source = source.cloneDataType(); |
89 | 0 | source.setMimeType(JSON_MIME_TYPE); |
90 | 0 | marshal = false; |
91 | |
} |
92 | 0 | else if (getMapperResolver().getMatchingClasses().contains(source.getType())) |
93 | |
{ |
94 | 0 | annotatedType = source.getType(); |
95 | |
|
96 | 0 | result = result.cloneDataType(); |
97 | 0 | result.setMimeType(JSON_MIME_TYPE); |
98 | 0 | marshal = true; |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 0 | return null; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 0 | List<Transformer> ts = muleContext.getRegistry().lookupTransformers(source, result); |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | if (ts.size() == 1 && !(ts.get(0) instanceof ObjectToString)) |
113 | |
{ |
114 | 0 | t = ts.get(0); |
115 | |
} |
116 | 0 | else if (marshal) |
117 | |
{ |
118 | 0 | ObjectToJson otj = new ObjectToJson(); |
119 | 0 | otj.setSourceClass(annotatedType); |
120 | 0 | otj.setReturnDataType(result); |
121 | 0 | otj.setMapper(mapper); |
122 | 0 | muleContext.getRegistry().applyProcessorsAndLifecycle(otj); |
123 | 0 | t = otj; |
124 | 0 | } |
125 | |
else |
126 | |
{ |
127 | 0 | JsonToObject jto = new JsonToObject(); |
128 | 0 | jto.setReturnDataType(result); |
129 | 0 | jto.setMapper(mapper); |
130 | 0 | muleContext.getRegistry().applyProcessorsAndLifecycle(jto); |
131 | 0 | t = jto; |
132 | |
} |
133 | |
|
134 | 0 | transformerCache.put(source.toString() + result.toString(), t); |
135 | 0 | return t; |
136 | |
|
137 | |
} |
138 | 0 | catch (Exception e) |
139 | |
{ |
140 | 0 | throw new ResolverException(CoreMessages.createStaticMessage("Failed to unmarshal"), e); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
public void transformerChange(Transformer transformer, RegistryAction registryAction) |
145 | |
{ |
146 | |
|
147 | 0 | } |
148 | |
|
149 | |
public void dispose() |
150 | |
{ |
151 | 0 | transformerCache.clear(); |
152 | 0 | } |
153 | |
|
154 | |
protected JsonMapperResolver getMapperResolver() throws ResolverException |
155 | |
{ |
156 | 0 | if(resolver==null) |
157 | |
{ |
158 | |
try |
159 | |
{ |
160 | 0 | resolver = muleContext.getRegistry().lookupObject(JsonMapperResolver.class); |
161 | |
} |
162 | 0 | catch (RegistrationException e) |
163 | |
{ |
164 | 0 | throw new ResolverException(e.getI18nMessage(), e); |
165 | 0 | } |
166 | |
} |
167 | 0 | return resolver; |
168 | |
} |
169 | |
} |