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.config.spring.factories;
8   
9   import org.mule.api.processor.MessageProcessor;
10  import org.mule.api.processor.MessageProcessorBuilder;
11  import org.mule.api.processor.MessageProcessors;
12  import org.mule.api.transaction.TransactionConfig;
13  import org.mule.processor.TransactionalInterceptingMessageProcessor;
14  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
15  
16  import java.util.List;
17  
18  import org.springframework.beans.factory.FactoryBean;
19  
20  public class TransactionalMessageProcessorsFactoryBean implements FactoryBean
21  {
22  
23      protected List messageProcessors;
24      protected TransactionConfig transactionConfig;
25  
26      public Class getObjectType()
27      {
28          return MessageProcessor.class;
29      }
30  
31      public void setMessageProcessors(List messageProcessors)
32      {
33          this.messageProcessors = messageProcessors;
34      }
35  
36      public Object getObject() throws Exception
37      {
38          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
39          builder.setName("'transaction' child processor chain");
40          TransactionalInterceptingMessageProcessor txProcessor = 
41              new TransactionalInterceptingMessageProcessor(transactionConfig);
42          builder.chain(txProcessor);
43          for (Object processor : messageProcessors)
44          {
45              if (processor instanceof MessageProcessor)
46              {
47                  builder.chain((MessageProcessor) processor);
48              }
49              else if (processor instanceof MessageProcessorBuilder)
50              {
51                  builder.chain((MessageProcessorBuilder) processor);
52              }
53              else
54              {
55                  throw new IllegalArgumentException(
56                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
57              }
58          }
59          return MessageProcessors.lifecyleAwareMessageProcessorWrapper(builder.build());
60      }
61  
62      public boolean isSingleton()
63      {
64          return false;
65      }
66  
67      public void setTransactionConfig(TransactionConfig transactionConfig)
68      {
69          this.transactionConfig = transactionConfig;
70      }
71  }