View Javadoc

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