View Javadoc

1   /*
2    * $Id$
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.construct;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.MuleConfiguration;
15  import org.mule.api.config.ThreadingProfile;
16  import org.mule.api.context.WorkManager;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.processor.MessageProcessorBuilder;
20  import org.mule.lifecycle.processor.ProcessIfStartedMessageProcessor;
21  import org.mule.processor.OptionalAsyncInterceptingMessageProcessor;
22  import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder;
23  
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * Simple implementation of {@link AbstractFlowConstruct} that allows a list of
29   * {@link MessageProcessor}s that will be used to process messages to be configured.
30   * These MessageProcessors are chained together using the
31   * {@link InterceptingChainMessageProcessorBuilder}.
32   * <p/>
33   * If not message processors are configured then the source message is simply
34   * returned.
35   */
36  public class SimpleFlowConstruct extends AbstractFlowConstruct
37  {
38      protected List<MessageProcessor> messageProcessors = Collections.emptyList();
39  
40      protected ThreadingProfile threadingProfile;
41  
42      protected WorkManager workManager;
43  
44      public SimpleFlowConstruct(String name, MuleContext muleContext)
45      {
46          super(name, muleContext);
47      }
48  
49      @Override
50      protected void configureMessageProcessors(InterceptingChainMessageProcessorBuilder builder)
51      {
52          if (threadingProfile == null)
53          {
54              threadingProfile = muleContext.getDefaultServiceThreadingProfile();
55          }
56  
57          final MuleConfiguration config = muleContext.getConfiguration();
58          final boolean containerMode = config.isContainerMode();
59          final String threadPrefix = containerMode
60                                                   ? String.format("[%s].flow.%s", config.getId(), getName())
61                                                   : String.format("flow.%s", getName());
62  
63          builder.chain(new ProcessIfStartedMessageProcessor(this, getLifecycleState()));
64          builder.chain(new OptionalAsyncInterceptingMessageProcessor(threadingProfile, threadPrefix,
65              muleContext.getConfiguration().getShutdownTimeout()));
66  
67          for (Object processor : messageProcessors)
68          {
69              if (processor instanceof MessageProcessor)
70              {
71                  builder.chain((MessageProcessor) processor);
72              }
73              else if (processor instanceof MessageProcessorBuilder)
74              {
75                  builder.chain((MessageProcessorBuilder) processor);
76              }
77              else
78              {
79                  throw new IllegalArgumentException(
80                      "MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
81              }
82          }
83      }
84  
85      public ThreadingProfile getThreadingProfile()
86      {
87          return threadingProfile;
88      }
89  
90      public void setThreadingProfile(ThreadingProfile threadingProfile)
91      {
92          this.threadingProfile = threadingProfile;
93      }
94  
95      public void setMessageProcessors(List<MessageProcessor> messageProcessors)
96      {
97          this.messageProcessors = messageProcessors;
98      }
99  
100     public List<MessageProcessor> getMessageProcessors()
101     {
102         return messageProcessors;
103     }
104 
105     @Deprecated
106     public void setEndpoint(InboundEndpoint endpoint)
107     {
108         this.messageSource = endpoint;
109     }
110 
111 }