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 public class BeanBuilderTransformer extends AbstractExpressionTransformer
34 {
35 private ObjectFactory beanFactory;
36 private Class<?> beanClass;
37
38 public Class<?> getBeanClass()
39 {
40 return beanClass;
41 }
42
43 public void setBeanClass(Class<?> beanClass)
44 {
45 this.beanClass = beanClass;
46 }
47
48 public ObjectFactory getBeanFactory()
49 {
50 return beanFactory;
51 }
52
53 public void setBeanFactory(ObjectFactory beanFactory)
54 {
55 this.beanFactory = beanFactory;
56 }
57
58
59
60
61
62
63
64 @Override
65 public void initialise() throws InitialisationException
66 {
67 super.initialise();
68
69 if(getBeanFactory()==null && getBeanClass()==null)
70 {
71 throw new InitialisationException(CoreMessages.objectIsNull("beanFactory"), this);
72 }
73 else if(getBeanClass()!=null)
74 {
75 setBeanFactory(new PrototypeObjectFactory(getBeanClass()));
76 }
77 setReturnDataType(DataTypeFactory.create(getBeanFactory().getObjectClass()));
78
79 if(getBeanFactory() instanceof MuleContextAware)
80 {
81 ((MuleContextAware)getBeanFactory()).setMuleContext(muleContext);
82 }
83 }
84
85 @Override
86 public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
87 {
88 Object bean;
89 try
90 {
91 bean = getBeanFactory().getInstance(muleContext);
92 }
93 catch (Exception e)
94 {
95 throw new TransformerException(this, e);
96 }
97
98 Map<String, Object> args = new HashMap<String, Object>(arguments.size());
99
100 for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext();)
101 {
102 ExpressionArgument argument = iterator.next();
103 Object value = null;
104 try
105 {
106 value = argument.evaluate(message);
107 }
108 catch (RequiredValueException e)
109 {
110 logger.warn(e.getMessage());
111 }
112 catch (ExpressionRuntimeException e)
113 {
114 throw new TransformerException(this, e);
115 }
116
117 if (!argument.isOptional() && value == null)
118 {
119 throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
120 argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this);
121
122 }
123 args.put(argument.getName(), value);
124 }
125
126 try
127 {
128 BeanUtils.populate(bean, args);
129 }
130 catch (IllegalAccessException e)
131 {
132 throw new TransformerException(this, e);
133 }
134 catch (InvocationTargetException e)
135 {
136 throw new TransformerException(this, e.getTargetException());
137 }
138
139 return bean;
140 }
141 }