View Javadoc

1   /*
2    * $Id: AbstractMessageProcessorOwner.java 22573 2011-07-29 23:52:45Z julien.eluard $
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  package org.mule.processor;
11  
12  import org.mule.api.AnnotatedObject;
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.construct.FlowConstruct;
16  import org.mule.api.construct.FlowConstructAware;
17  import org.mule.api.context.MuleContextAware;
18  import org.mule.api.lifecycle.Disposable;
19  import org.mule.api.lifecycle.Initialisable;
20  import org.mule.api.lifecycle.InitialisationException;
21  import org.mule.api.lifecycle.Lifecycle;
22  import org.mule.api.lifecycle.Startable;
23  import org.mule.api.lifecycle.Stoppable;
24  import org.mule.api.processor.MessageProcessor;
25  
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import javax.xml.namespace.QName;
32  
33  /**
34   * An object that owns message processors and delegates startup/shutdown events to them.
35   */
36  public abstract class AbstractMessageProcessorOwner implements Lifecycle, MuleContextAware, FlowConstructAware, AnnotatedObject
37  {
38      protected MuleContext muleContext;
39      protected FlowConstruct flowConstruct;
40      private final Map<QName, Object> annotations = new ConcurrentHashMap<QName, Object>();
41  
42      public void setMuleContext(MuleContext context)
43      {
44          this.muleContext = context;
45      }
46  
47      public void setFlowConstruct(FlowConstruct flowConstruct)
48      {
49          this.flowConstruct = flowConstruct;
50      }
51  
52      public void initialise() throws InitialisationException
53      {
54          for (MessageProcessor processor : getOwnedMessageProcessors())
55          {
56              if (processor instanceof MuleContextAware)
57              {
58                  ((MuleContextAware) processor).setMuleContext(muleContext);
59              }
60              if (processor instanceof FlowConstructAware)
61              {
62                  ((FlowConstructAware) processor).setFlowConstruct(flowConstruct);
63              }
64              if (processor instanceof Initialisable)
65              {
66                  ((Initialisable) processor).initialise();
67              }
68          }
69      }
70  
71      public void dispose()
72      {
73          for (MessageProcessor processor : getOwnedMessageProcessors())
74          {
75  
76              if (processor instanceof Disposable)
77              {
78                  ((Disposable) processor).dispose();
79              }
80          }
81      }
82  
83  
84      public void start() throws MuleException
85      {
86  
87          for (MessageProcessor processor : getOwnedMessageProcessors())
88          {
89              if (processor instanceof Startable)
90              {
91                  ((Startable) processor).start();
92              }
93          }
94      }
95  
96  
97      public void stop() throws MuleException
98      {
99  
100         for (MessageProcessor processor : getOwnedMessageProcessors())
101         {
102             if (processor instanceof Stoppable)
103             {
104                 ((Stoppable) processor).stop();
105             }
106 
107         }
108     }
109 
110     public final Object getAnnotation(QName name)
111     {
112         return annotations.get(name);
113     }
114 
115     public final Map<QName, Object> getAnnotations()
116     {
117         return Collections.unmodifiableMap(annotations);
118     }
119 
120     public synchronized final void setAnnotations(Map<QName, Object> newAnnotations)
121     {
122         annotations.clear();
123         annotations.putAll(newAnnotations);
124     }
125 
126     protected abstract List<MessageProcessor> getOwnedMessageProcessors();
127 
128 }
129