View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * This transformer uses the returnClass to create the return object and then will populate the bean
28   * with arguments defined as expressions
29   */
30  public class BeanBuilderTransformer extends AbstractExpressionTransformer
31  {
32      private ObjectFactory beanFactory;
33      private Class<?> beanClass;
34  
35      public Class<?> getBeanClass()
36      {
37          return beanClass;
38      }
39  
40      public void setBeanClass(Class<?> beanClass)
41      {
42          this.beanClass = beanClass;
43      }
44  
45      public ObjectFactory getBeanFactory()
46      {
47          return beanFactory;
48      }
49  
50      public void setBeanFactory(ObjectFactory beanFactory)
51      {
52          this.beanFactory = beanFactory;
53      }
54  
55      /**
56       * Template method were deriving classes can do any initialisation after the
57       * properties have been set on this transformer
58       *
59       * @throws org.mule.api.lifecycle.InitialisationException
60       */
61      @Override
62      public void initialise() throws InitialisationException
63      {
64          super.initialise();
65  
66          if(getBeanFactory()==null && getBeanClass()==null)
67          {
68              throw new InitialisationException(CoreMessages.objectIsNull("beanFactory"), this);
69          }
70          else if(getBeanClass()!=null)
71          {
72              setBeanFactory(new PrototypeObjectFactory(getBeanClass()));
73          }
74          setReturnDataType(DataTypeFactory.create(getBeanFactory().getObjectClass()));
75          //We need to set the MuleContext if we create the factory here
76          if(getBeanFactory() instanceof MuleContextAware)
77          {
78              ((MuleContextAware)getBeanFactory()).setMuleContext(muleContext);
79          }
80      }
81  
82      @Override
83      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
84      {
85          Object bean;
86          try
87          {
88              bean = getBeanFactory().getInstance(muleContext);
89          }
90          catch (Exception e)
91          {
92              throw new TransformerException(this, e);
93          }
94  
95          Map<String, Object> args = new HashMap<String, Object>(arguments.size());
96  
97          for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext();)
98          {
99              ExpressionArgument argument = iterator.next();
100             Object value = null;
101             try
102             {
103                 value = argument.evaluate(message);
104             }
105             catch (RequiredValueException e)
106             {
107                 logger.warn(e.getMessage());
108             }
109             catch (ExpressionRuntimeException e)
110             {
111                 throw new TransformerException(this, e);
112             }
113 
114             if (!argument.isOptional() && value == null)
115             {
116                 throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
117                         argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this);
118 
119             }
120             args.put(argument.getName(), value);
121         }
122 
123         try
124         {
125             BeanUtils.populate(bean, args);
126         }
127         catch (IllegalAccessException e)
128         {
129             throw new TransformerException(this, e);
130         }
131         catch (InvocationTargetException e)
132         {
133             throw new TransformerException(this, e.getTargetException());
134         }
135 
136         return bean;
137     }
138 }