1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.expression.transformers; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.context.MuleContextAware; |
14 | |
import org.mule.api.expression.ExpressionRuntimeException; |
15 | |
import org.mule.api.expression.RequiredValueException; |
16 | |
import org.mule.api.lifecycle.InitialisationException; |
17 | |
import org.mule.api.object.ObjectFactory; |
18 | |
import org.mule.api.transformer.TransformerException; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.object.PrototypeObjectFactory; |
21 | |
import org.mule.transformer.types.DataTypeFactory; |
22 | |
import org.mule.util.BeanUtils; |
23 | |
|
24 | |
import java.lang.reflect.InvocationTargetException; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class BeanBuilderTransformer extends AbstractExpressionTransformer |
34 | |
{ |
35 | |
private ObjectFactory beanFactory; |
36 | |
private Class<?> beanClass; |
37 | |
|
38 | |
public Class<?> getBeanClass() |
39 | |
{ |
40 | 0 | return beanClass; |
41 | |
} |
42 | |
|
43 | |
public void setBeanClass(Class<?> beanClass) |
44 | |
{ |
45 | 0 | this.beanClass = beanClass; |
46 | 0 | } |
47 | |
|
48 | |
public ObjectFactory getBeanFactory() |
49 | |
{ |
50 | 0 | return beanFactory; |
51 | |
} |
52 | |
|
53 | |
public void setBeanFactory(ObjectFactory beanFactory) |
54 | |
{ |
55 | 0 | this.beanFactory = beanFactory; |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Override |
65 | |
public void initialise() throws InitialisationException |
66 | |
{ |
67 | 0 | super.initialise(); |
68 | |
|
69 | 0 | if(getBeanFactory()==null && getBeanClass()==null) |
70 | |
{ |
71 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("beanFactory"), this); |
72 | |
} |
73 | 0 | else if(getBeanClass()!=null) |
74 | |
{ |
75 | 0 | setBeanFactory(new PrototypeObjectFactory(getBeanClass())); |
76 | |
} |
77 | 0 | setReturnDataType(DataTypeFactory.create(getBeanFactory().getObjectClass())); |
78 | |
|
79 | 0 | if(getBeanFactory() instanceof MuleContextAware) |
80 | |
{ |
81 | 0 | ((MuleContextAware)getBeanFactory()).setMuleContext(muleContext); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
@Override |
86 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
87 | |
{ |
88 | |
Object bean; |
89 | |
try |
90 | |
{ |
91 | 0 | bean = getBeanFactory().getInstance(muleContext); |
92 | |
} |
93 | 0 | catch (Exception e) |
94 | |
{ |
95 | 0 | throw new TransformerException(this, e); |
96 | 0 | } |
97 | |
|
98 | 0 | Map<String, Object> args = new HashMap<String, Object>(arguments.size()); |
99 | |
|
100 | 0 | for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext();) |
101 | |
{ |
102 | 0 | ExpressionArgument argument = iterator.next(); |
103 | 0 | Object value = null; |
104 | |
try |
105 | |
{ |
106 | 0 | value = argument.evaluate(message); |
107 | |
} |
108 | 0 | catch (RequiredValueException e) |
109 | |
{ |
110 | 0 | logger.warn(e.getMessage()); |
111 | |
} |
112 | 0 | catch (ExpressionRuntimeException e) |
113 | |
{ |
114 | 0 | throw new TransformerException(this, e); |
115 | 0 | } |
116 | |
|
117 | 0 | if (!argument.isOptional() && value == null) |
118 | |
{ |
119 | 0 | throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull( |
120 | |
argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this); |
121 | |
|
122 | |
} |
123 | 0 | args.put(argument.getName(), value); |
124 | 0 | } |
125 | |
|
126 | |
try |
127 | |
{ |
128 | 0 | BeanUtils.populate(bean, args); |
129 | |
} |
130 | 0 | catch (IllegalAccessException e) |
131 | |
{ |
132 | 0 | throw new TransformerException(this, e); |
133 | |
} |
134 | 0 | catch (InvocationTargetException e) |
135 | |
{ |
136 | 0 | throw new TransformerException(this, e.getTargetException()); |
137 | 0 | } |
138 | |
|
139 | 0 | return bean; |
140 | |
} |
141 | |
} |