View Javadoc

1   /*
2    * $Id: TransactionalMessageProcessorsFactoryBean.java 21937 2011-05-17 21:21:29Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.factories;
12  
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.api.processor.MessageProcessorBuilder;
15  import org.mule.api.processor.MessageProcessors;
16  import org.mule.api.transaction.TransactionConfig;
17  import org.mule.processor.TransactionalInterceptingMessageProcessor;
18  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
19  
20  import java.util.List;
21  
22  import org.springframework.beans.factory.FactoryBean;
23  
24  public class TransactionalMessageProcessorsFactoryBean implements FactoryBean
25  {
26  
27      protected List messageProcessors;
28      protected TransactionConfig transactionConfig;
29  
30      public Class getObjectType()
31      {
32          return MessageProcessor.class;
33      }
34  
35      public void setMessageProcessors(List messageProcessors)
36      {
37          this.messageProcessors = messageProcessors;
38      }
39  
40      public Object getObject() throws Exception
41      {
42          DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
43          builder.setName("'transaction' child processor chain");
44          TransactionalInterceptingMessageProcessor txProcessor = 
45              new TransactionalInterceptingMessageProcessor(transactionConfig);
46          builder.chain(txProcessor);
47          for (Object processor : messageProcessors)
48          {
49              if (processor instanceof MessageProcessor)
50              {
51                  builder.chain((MessageProcessor) processor);
52              }
53              else if (processor instanceof MessageProcessorBuilder)
54              {
55                  builder.chain((MessageProcessorBuilder) processor);
56              }
57              else
58              {
59                  throw new IllegalArgumentException(
60                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
61              }
62          }
63          return MessageProcessors.lifecyleAwareMessageProcessorWrapper(builder.build());
64      }
65  
66      public boolean isSingleton()
67      {
68          return false;
69      }
70  
71      public void setTransactionConfig(TransactionConfig transactionConfig)
72      {
73          this.transactionConfig = transactionConfig;
74      }
75  }