View Javadoc

1   /*
2    * $Id: AbstractConfigurationPattern.java 22557 2011-07-25 22:48:27Z 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.construct;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.api.processor.MessageProcessorChainBuilder;
17  import org.mule.api.processor.ProcessingStrategy;
18  import org.mule.construct.processor.FlowConstructStatisticsMessageProcessor;
19  import org.mule.interceptor.ProcessingTimeInterceptor;
20  import org.mule.lifecycle.processor.ProcessIfStartedMessageProcessor;
21  import org.mule.processor.ResponseMessageProcessorAdapter;
22  import org.mule.processor.StopFurtherMessageProcessingMessageProcessor;
23  import org.mule.processor.chain.DefaultMessageProcessorChain;
24  
25  import java.util.List;
26  
27  import org.apache.commons.lang.Validate;
28  
29  /**
30   * A template class for configuration patterns, which takes care of setting common message processors and
31   * optional transformers defined on the pattern.
32   */
33  public abstract class AbstractConfigurationPattern extends AbstractPipeline
34  {
35      protected final List<MessageProcessor> transformers;
36      protected final List<MessageProcessor> responseTransformers;
37  
38      public AbstractConfigurationPattern(String name,
39                                          MuleContext muleContext,
40                                          List<MessageProcessor> transformers,
41                                          List<MessageProcessor> responseTransformers)
42      {
43          super(name, muleContext);
44  
45          Validate.notNull(transformers, "transformers can't be null");
46          Validate.notNull(responseTransformers, "transformers can't be null");
47  
48          this.transformers = transformers;
49          this.responseTransformers = responseTransformers;
50      }
51  
52      @Override
53      protected final void configureMessageProcessors(final MessageProcessorChainBuilder builder) throws MuleException
54      {
55          configureMessageProcessorsBeforeTransformation(builder);
56  
57          builder.chain(DefaultMessageProcessorChain.from(transformers));
58          builder.chain(new ResponseMessageProcessorAdapter(
59              DefaultMessageProcessorChain.from(responseTransformers)));
60  
61          builder.chain(new StopFurtherMessageProcessingMessageProcessor());
62  
63          configureMessageProcessorsAfterTransformation(builder);
64      }
65      
66      @Override
67      protected void configurePreProcessors(MessageProcessorChainBuilder builder) throws MuleException
68      {
69          super.configurePreProcessors(builder);
70          builder.chain(new ProcessIfStartedMessageProcessor(this, getLifecycleState()));
71          builder.chain(new ProcessingTimeInterceptor());
72          builder.chain(new FlowConstructStatisticsMessageProcessor());
73      }
74      
75      public boolean hasTransformers()
76      {
77          return !transformers.isEmpty();
78      }
79  
80      public boolean hasResponseTransformers()
81      {
82          return !responseTransformers.isEmpty();
83      }
84  
85      @Override
86      public final void setProcessingStrategy(ProcessingStrategy processingStrategy)
87      {
88          throw new UnsupportedOperationException();
89      }
90  
91      protected abstract void configureMessageProcessorsBeforeTransformation(final MessageProcessorChainBuilder builder)
92          throws MuleException;
93  
94      protected abstract void configureMessageProcessorsAfterTransformation(final MessageProcessorChainBuilder builder)
95          throws MuleException;
96  }