View Javadoc

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