View Javadoc

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