View Javadoc

1   /*
2    * $Id: MessageProcessors.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.api.processor;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
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.processor.chain.DefaultMessageProcessorChain;
26  
27  /**
28   * Some convenience methods for message processors.
29   */
30  public class MessageProcessors
31  {
32  
33      private MessageProcessors()
34      {
35          // do not instantiate
36      }
37  
38      public static MessageProcessorChain singletonChain(MessageProcessor mp)
39      {
40          return DefaultMessageProcessorChain.from(mp);
41      }
42  
43      public static MessageProcessor lifecyleAwareMessageProcessorWrapper(final MessageProcessor mp)
44      {
45          return new LifecyleAwareMessageProcessorWrapper(mp);
46      }
47  
48      private static class LifecyleAwareMessageProcessorWrapper
49          implements MessageProcessor, Lifecycle, MuleContextAware, FlowConstructAware
50      {
51          private MessageProcessor delegate;
52  
53          public LifecyleAwareMessageProcessorWrapper(MessageProcessor delegate)
54          {
55              this.delegate = delegate;
56          }
57  
58          public void initialise() throws InitialisationException
59          {
60              if (delegate instanceof Initialisable)
61              {
62                  ((Initialisable) delegate).initialise();
63              }
64  
65          }
66  
67          public void start() throws MuleException
68          {
69              if (delegate instanceof Startable)
70              {
71                  ((Startable) delegate).start();
72              }
73  
74          }
75  
76          public void stop() throws MuleException
77          {
78              if (delegate instanceof Stoppable)
79              {
80                  ((Stoppable) delegate).stop();
81              }
82  
83          }
84  
85          public void dispose()
86          {
87              if (delegate instanceof Disposable)
88              {
89                  ((Disposable) delegate).dispose();
90              }
91  
92          }
93  
94          public void setFlowConstruct(FlowConstruct flowConstruct)
95          {
96              if (delegate instanceof FlowConstructAware)
97              {
98                  ((FlowConstructAware) delegate).setFlowConstruct(flowConstruct);
99              }
100 
101         }
102 
103         public void setMuleContext(MuleContext context)
104         {
105             if (delegate instanceof MuleContextAware)
106             {
107                 ((MuleContextAware) delegate).setMuleContext(context);
108             }
109 
110         }
111 
112         public MuleEvent process(MuleEvent event) throws MuleException
113         {
114             return delegate.process(event);
115         }
116     }
117 }