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.routing;
8   
9   import org.apache.commons.lang.Validate;
10  import org.apache.commons.lang.builder.ToStringBuilder;
11  import org.apache.commons.lang.builder.ToStringStyle;
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.construct.FlowConstruct;
15  import org.mule.api.construct.FlowConstructAware;
16  import org.mule.api.context.MuleContextAware;
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.MessageProcessor;
24  import org.mule.api.routing.filter.Filter;
25  
26  /**
27   * A holder for a pair of MessageProcessor and Filter.
28   */
29  public class MessageProcessorFilterPair implements FlowConstructAware, MuleContextAware, Lifecycle
30  {
31      private final MessageProcessor messageProcessor;
32      private final Filter filter;
33  
34      public MessageProcessorFilterPair(MessageProcessor messageProcessor, Filter filter)
35      {
36          Validate.notNull(messageProcessor, "messageProcessor can't be null");
37          Validate.notNull(filter, "filter can't be null");
38          this.messageProcessor = messageProcessor;
39          this.filter = filter;
40      }
41  
42      public MessageProcessor getMessageProcessor()
43      {
44          return messageProcessor;
45      }
46  
47      public Filter getFilter()
48      {
49          return filter;
50      }
51  
52      @Override
53      public String toString()
54      {
55          return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
56      }
57  
58      // This class being just a logic-less tuple, it directly delegates lifecyle
59      // events to its members, without any control.
60  
61      public void setFlowConstruct(FlowConstruct flowConstruct)
62      {
63          if (messageProcessor instanceof FlowConstructAware)
64          {
65              ((FlowConstructAware) messageProcessor).setFlowConstruct(flowConstruct);
66          }
67          if (filter instanceof FlowConstructAware)
68          {
69              ((FlowConstructAware) filter).setFlowConstruct(flowConstruct);
70          }
71      }
72  
73      public void setMuleContext(MuleContext context)
74      {
75           if (messageProcessor instanceof MuleContextAware)
76          {
77              ((MuleContextAware) messageProcessor).setMuleContext(context);
78          }
79          if (filter instanceof MuleContextAware)
80          {
81              ((MuleContextAware) filter).setMuleContext(context);
82          }
83      }
84  
85      public void initialise() throws InitialisationException
86      {
87          if (messageProcessor instanceof Initialisable)
88          {
89              ((Initialisable) messageProcessor).initialise();
90          }
91          if (filter instanceof Initialisable)
92          {
93              ((Initialisable) filter).initialise();
94          }
95      }
96  
97      public void start() throws MuleException
98      {
99          if (messageProcessor instanceof Startable)
100         {
101             ((Startable) messageProcessor).start();
102         }
103         if (filter instanceof Startable)
104         {
105             ((Startable) filter).start();
106         }
107     }
108 
109     public void stop() throws MuleException
110     {
111         if (messageProcessor instanceof Stoppable)
112         {
113             ((Stoppable) messageProcessor).stop();
114         }
115         if (filter instanceof Stoppable)
116         {
117             ((Stoppable) filter).stop();
118         }
119     }
120 
121     public void dispose()
122     {
123         if (messageProcessor instanceof Disposable)
124         {
125             ((Disposable) messageProcessor).dispose();
126         }
127         if (filter instanceof Disposable)
128         {
129             ((Disposable) filter).dispose();
130         }
131     }
132 }