View Javadoc

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