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