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.module.cxf.config;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.construct.FlowConstructAware;
13  import org.mule.api.lifecycle.Disposable;
14  import org.mule.api.lifecycle.Initialisable;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.lifecycle.Lifecycle;
17  import org.mule.api.lifecycle.Startable;
18  import org.mule.api.lifecycle.Stoppable;
19  import org.mule.api.processor.InterceptingMessageProcessor;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.processor.MessageProcessorBuilder;
22  
23  /**
24   * Wraps a {@link MessageProcessorBuilder} and configures it lazily so it can
25   * be injected with the {@link FlowConstruct}.
26   */
27  public class FlowConfiguringMessageProcessor implements FlowConstructAware, Lifecycle, InterceptingMessageProcessor
28  {
29  
30      private MessageProcessorBuilder builder;
31      private MessageProcessor messageProcessor;
32      private MessageProcessor listener;
33  
34      public FlowConfiguringMessageProcessor(MessageProcessorBuilder builder)
35      {
36          this.builder = builder;
37      }
38  
39      public void setListener(MessageProcessor listener)
40      {
41          this.listener = listener;
42      }
43  
44      public MuleEvent process(MuleEvent event) throws MuleException
45      {
46          return messageProcessor.process(event);
47      }
48  
49      public void start() throws MuleException
50      {
51          if (messageProcessor instanceof Startable)
52          {
53              ((Startable) messageProcessor).start();
54          }
55      }
56  
57      public void setFlowConstruct(FlowConstruct flowConstruct)
58      {
59          if (builder instanceof FlowConstructAware)
60          {
61              ((FlowConstructAware) builder).setFlowConstruct(flowConstruct);
62          }
63      }
64  
65      public void dispose()
66      {
67          if (messageProcessor instanceof Disposable)
68          {
69              ((Disposable) messageProcessor).dispose();
70          }
71      }
72  
73      public void stop() throws MuleException
74      {
75          if (messageProcessor instanceof Stoppable)
76          {
77              ((Stoppable) messageProcessor).stop();
78          }
79      }
80  
81      public void initialise() throws InitialisationException
82      {
83          if (builder instanceof Initialisable)
84          {
85              ((Initialisable) builder).initialise();
86          }
87  
88          try
89          {
90              messageProcessor = builder.build();
91          }
92          catch (Exception e)
93          {
94              throw new InitialisationException(e, this);
95          }
96  
97          if (messageProcessor instanceof Initialisable)
98          {
99              ((Initialisable) messageProcessor).initialise();
100         }
101 
102         if (messageProcessor instanceof InterceptingMessageProcessor && listener != null)
103         {
104             ((InterceptingMessageProcessor) messageProcessor).setListener(listener);
105         }
106     }
107 
108     /**
109      * The MessageProcessor that this class built.
110      */
111     public MessageProcessor getWrappedMessageProcessor()
112     {
113         return messageProcessor;
114     }
115 
116     public MessageProcessorBuilder getMessageProcessorBuilder()
117     {
118         return builder;
119     }
120 
121 }