View Javadoc

1   /*
2    * $Id: OutboundEndpointFactoryBean.java 11079 2008-02-27 15:52:01Z tcarlson $
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.MuleContext;
14  import org.mule.api.NamedObject;
15  import org.mule.api.config.MuleConfiguration;
16  import org.mule.api.config.ThreadingProfile;
17  import org.mule.api.context.MuleContextAware;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.processor.MessageProcessorBuilder;
20  import org.mule.processor.AsyncInterceptingMessageProcessor;
21  import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder;
22  
23  import org.springframework.beans.factory.FactoryBean;
24  
25  import java.util.List;
26  
27  public class AsyncMessageProcessorsFactoryBean implements FactoryBean, MuleContextAware, NamedObject
28  {
29  
30      protected MuleContext muleContext;
31  
32      protected List messageProcessors;
33      protected ThreadingProfile threadingProfile;
34      protected String name;
35  
36      public Class getObjectType()
37      {
38          return MessageProcessor.class;
39      }
40  
41      public void setThreadingProfile(ThreadingProfile threadingProfile)
42      {
43          this.threadingProfile = threadingProfile;
44      }
45  
46      public void setMessageProcessors(List messageProcessors)
47      {
48          this.messageProcessors = messageProcessors;
49      }
50  
51      public Object getObject() throws Exception
52      {
53          if (threadingProfile == null)
54          {
55              threadingProfile = muleContext.getDefaultThreadingProfile();
56          }
57  
58          InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder();
59          final MuleConfiguration config = muleContext.getConfiguration();
60          final boolean containerMode = config.isContainerMode();
61          final String threadPrefix = containerMode
62                                          ? String.format("[%s].%s.processor.async", config.getId(), name)
63                                          : String.format("%s.processor.async", name);
64          AsyncInterceptingMessageProcessor asyncProcessor = new AsyncInterceptingMessageProcessor(threadingProfile,
65                                                                                                   threadPrefix,
66                                                                                                   config.getShutdownTimeout());
67          builder.chain(asyncProcessor);
68          for (Object processor : messageProcessors)
69          {
70              if (processor instanceof MessageProcessor)
71              {
72                  builder.chain((MessageProcessor) processor);
73              }
74              else if (processor instanceof MessageProcessorBuilder)
75              {
76                  builder.chain((MessageProcessorBuilder) processor);
77              }
78              else
79              {
80                  throw new IllegalArgumentException(
81                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
82              }
83          }
84          return builder.build();
85      }
86  
87      public boolean isSingleton()
88      {
89          return false;
90      }
91  
92      public void setMuleContext(MuleContext context)
93      {
94          this.muleContext = context;
95      }
96  
97      public String getName()
98      {
99          return name;
100     }
101 
102     public void setName(String name)
103     {
104         this.name = name;
105     }
106 
107 }