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.interceptor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.interceptor.Interceptor;
12  import org.mule.api.lifecycle.Disposable;
13  import org.mule.api.lifecycle.Initialisable;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.processor.AbstractInterceptingMessageProcessor;
17  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
18  
19  import java.util.List;
20  
21  /**
22   * Maintains a list of interceptors that can be applied to components.
23   */
24  public class InterceptorStack extends AbstractInterceptingMessageProcessor
25      implements Interceptor, Initialisable, Disposable
26  {
27  
28      private List<Interceptor> interceptors;
29      private MessageProcessor chain;
30  
31      public InterceptorStack()
32      {
33          // For spring
34      }
35  
36      public InterceptorStack(List<Interceptor> interceptors)
37      {
38          this.interceptors = interceptors;
39      }
40  
41      public MuleEvent process(MuleEvent event) throws MuleException
42      {
43          return chain.process(event);
44      }
45  
46      public List<Interceptor> getInterceptors()
47      {
48          return interceptors;
49      }
50  
51      public void setInterceptors(List<Interceptor> interceptors)
52      {
53          this.interceptors = interceptors;
54      }
55  
56      public void initialise() throws InitialisationException
57      {
58          DefaultMessageProcessorChainBuilder chainBuilder = new DefaultMessageProcessorChainBuilder();
59          chainBuilder.setName("interceptor stack");
60          for (Interceptor interceptor : interceptors)
61          {
62              if (interceptor instanceof Initialisable)
63              {
64                  ((Initialisable) interceptor).initialise();
65              }
66              chainBuilder.chain(interceptor);
67          }
68          if (next != null)
69          {
70              chainBuilder.chain(next);
71          }
72          try
73          {
74              chain = chainBuilder.build();
75          }
76          catch (MuleException e)
77          {
78              throw new InitialisationException(e, this);
79          }
80      }
81  
82      public void dispose()
83      {
84          for (Interceptor interceptor : interceptors)
85          {
86              if (interceptor instanceof Disposable)
87              {
88                  ((Disposable) interceptor).dispose();
89              }
90          }
91      }
92  
93      @Override
94      public int hashCode()
95      {
96          final int prime = 31;
97          int result = 1;
98          result = prime * result + ((interceptors == null) ? 0 : interceptors.hashCode());
99          return result;
100     }
101 
102     @Override
103     public boolean equals(Object obj)
104     {
105         if (this == obj) return true;
106         if (obj == null) return false;
107         if (getClass() != obj.getClass()) return false;
108         InterceptorStack other = (InterceptorStack) obj;
109         if (interceptors == null)
110         {
111             if (other.interceptors != null) return false;
112         }
113         else if (!interceptors.equals(other.interceptors)) return false;
114         return true;
115     }
116 
117 }