View Javadoc

1   /*
2    * $Id: InterceptorStack.java 7976 2007-08-21 14:26:13Z 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   * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
29   * @version $Revision: 7976 $
30   */
31  public class InterceptorStack implements UMOInterceptorStack, Initialisable, Disposable
32  {
33  
34      private List interceptors;
35  
36      public InterceptorStack()
37      {
38          super();
39      }
40  
41      public InterceptorStack(List interceptors)
42      {
43          this.interceptors = interceptors;
44      }
45  
46      public UMOMessage intercept(Invocation invocation) throws UMOException
47      {
48          return new Invoc(invocation).execute();
49      }
50  
51      private class Invoc extends Invocation
52      {
53          private int cursor = 0;
54          private Invocation invocation;
55  
56          public Invoc(Invocation invocation)
57          {
58              super(invocation.getDescriptor(), invocation.getMessage(), invocation);
59              this.invocation = invocation;
60          }
61  
62          public UMOMessage execute() throws UMOException
63          {
64              if (interceptors != null && cursor < interceptors.size())
65              {
66                  UMOInterceptor interceptor = (UMOInterceptor) interceptors.get(cursor);
67                  cursor++;
68                  setMessage(interceptor.intercept(this));
69              }
70              else
71              {
72                  invocation.setMessage(getMessage());
73                  setMessage(invocation.execute());
74              }
75              return getMessage();
76          }
77  
78      }
79  
80      public List getInterceptors()
81      {
82          return interceptors;
83      }
84  
85      public void setInterceptors(List interceptors)
86      {
87          this.interceptors = interceptors;
88      }
89  
90      public void initialise() throws InitialisationException
91      {
92          for (Iterator it = interceptors.iterator(); it.hasNext();)
93          {
94              UMOInterceptor interceptor = (UMOInterceptor) it.next();
95              if (interceptor instanceof Initialisable)
96              {
97                  ((Initialisable) interceptor).initialise();
98              }
99          }
100     }
101 
102     public void dispose()
103     {
104         for (Iterator it = interceptors.iterator(); it.hasNext();)
105         {
106             UMOInterceptor interceptor = (UMOInterceptor) it.next();
107             if (interceptor instanceof Disposable)
108             {
109                 ((Disposable) interceptor).dispose();
110             }
111         }
112     }
113 
114 }