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.processor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.ThreadSafeAccess;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.construct.FlowConstructAware;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.lifecycle.Disposable;
16  import org.mule.api.lifecycle.Initialisable;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.lifecycle.Lifecycle;
19  import org.mule.api.lifecycle.Startable;
20  import org.mule.api.lifecycle.Stoppable;
21  import org.mule.api.processor.MessageProcessor;
22  
23  public class ResponseMessageProcessorAdapter extends AbstractResponseMessageProcessor implements Lifecycle, FlowConstructAware
24  {
25  
26      protected MessageProcessor responseProcessor;
27      protected FlowConstruct flowConstruct;
28  
29      public ResponseMessageProcessorAdapter()
30      {
31          super();
32      }
33  
34      public ResponseMessageProcessorAdapter(MessageProcessor responseProcessor)
35      {
36          super();
37          this.responseProcessor = responseProcessor;
38      }
39  
40      public void setProcessor(MessageProcessor processor)
41      {
42          this.responseProcessor = processor;
43      }
44  
45      @Override
46      protected MuleEvent processResponse(MuleEvent event) throws MuleException
47      {
48          if (responseProcessor == null || event == null)
49          {
50              return event;
51          }
52          else
53          {
54              MuleEvent copy = (MuleEvent) ((ThreadSafeAccess) event).newThreadCopy();
55              MuleEvent result = responseProcessor.process(event);
56              if (result == null)
57              {
58                  // If <response> returns null then it acts as an implicit branch like in flows, the different
59                  // here is that what's next, it's not another message processor that follows this one in the
60                  // configuration file but rather the response phase of the inbound endpoint, or optionally
61                  // other response processing on the way back to the inbound endpoint.
62                  return copy;
63              }
64              else
65              {
66                  return result;
67              }
68          }
69      }
70  
71      public void initialise() throws InitialisationException
72      {
73          if (responseProcessor instanceof MuleContextAware)
74          {
75              ((MuleContextAware) responseProcessor).setMuleContext(muleContext);
76          }
77          if (responseProcessor instanceof FlowConstructAware)
78          {
79              ((FlowConstructAware) responseProcessor).setFlowConstruct(flowConstruct);
80          }
81          if (responseProcessor instanceof Initialisable)
82          {
83              ((Initialisable) responseProcessor).initialise();
84          }
85      }
86  
87      public void start() throws MuleException
88      {
89          if (responseProcessor instanceof Startable)
90          {
91              ((Startable) responseProcessor).start();
92          }
93      }
94  
95      public void stop() throws MuleException
96      {
97          if (responseProcessor instanceof Stoppable)
98          {
99              ((Stoppable) responseProcessor).stop();
100         }
101     }
102 
103     public void dispose()
104     {
105         if (responseProcessor instanceof Disposable)
106         {
107             ((Disposable) responseProcessor).dispose();
108         }
109     }
110 
111     public void setFlowConstruct(FlowConstruct flowConstruct)
112     {
113         this.flowConstruct = flowConstruct;
114     }
115 
116 }