View Javadoc

1   /*
2    * $Id: InterceptorStack.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.interceptors;
12  
13  import org.mule.umo.Invocation;
14  import org.mule.umo.UMOException;
15  import org.mule.umo.UMOInterceptor;
16  import org.mule.umo.UMOInterceptorStack;
17  import org.mule.umo.UMOMessage;
18  import org.mule.umo.lifecycle.Disposable;
19  import org.mule.umo.lifecycle.Initialisable;
20  import org.mule.umo.lifecycle.InitialisationException;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /**
26   * Maintains a list of interceptors that can be applied to components.
27   */
28  public class InterceptorStack implements UMOInterceptorStack, Initialisable, Disposable
29  {
30  
31      private List interceptors;
32  
33      public InterceptorStack()
34      {
35          super();
36      }
37  
38      public InterceptorStack(List interceptors)
39      {
40          this.interceptors = interceptors;
41      }
42  
43      public UMOMessage intercept(Invocation invocation) throws UMOException
44      {
45          return new Invoc(invocation).execute();
46      }
47  
48      private class Invoc extends Invocation
49      {
50          private int cursor = 0;
51          private Invocation invocation;
52  
53          public Invoc(Invocation invocation)
54          {
55              super(invocation.getDescriptor(), invocation.getMessage(), invocation);
56              this.invocation = invocation;
57          }
58  
59          public UMOMessage execute() throws UMOException
60          {
61              if (interceptors != null && cursor < interceptors.size())
62              {
63                  UMOInterceptor interceptor = (UMOInterceptor) interceptors.get(cursor);
64                  cursor++;
65                  setMessage(interceptor.intercept(this));
66              }
67              else
68              {
69                  invocation.setMessage(getMessage());
70                  setMessage(invocation.execute());
71              }
72              return getMessage();
73          }
74  
75      }
76  
77      public List getInterceptors()
78      {
79          return interceptors;
80      }
81  
82      public void setInterceptors(List interceptors)
83      {
84          this.interceptors = interceptors;
85      }
86  
87      public void initialise() throws InitialisationException
88      {
89          for (Iterator it = interceptors.iterator(); it.hasNext();)
90          {
91              UMOInterceptor interceptor = (UMOInterceptor) it.next();
92              if (interceptor instanceof Initialisable)
93              {
94                  ((Initialisable) interceptor).initialise();
95              }
96          }
97      }
98  
99      public void dispose()
100     {
101         for (Iterator it = interceptors.iterator(); it.hasNext();)
102         {
103             UMOInterceptor interceptor = (UMOInterceptor) it.next();
104             if (interceptor instanceof Disposable)
105             {
106                 ((Disposable) interceptor).dispose();
107             }
108         }
109     }
110 
111 }