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