View Javadoc

1   package jp.sf.grizzly.pipeline;
2   
3   import java.util.List;
4   
5   import jp.sf.grizzly.pipeline.valve.Valve;
6   import jp.sf.grizzly.pipeline.valve.ValveContext;
7   import jp.sf.grizzly.storage.StreamStorage;
8   
9   public class GrizzlyPipeline
10  {
11      /***
12       * Name of this pipeline.
13       */
14      protected String name;
15  
16      /***
17       * The set of Valves associated with this Pipeline.
18       */
19      protected Valve[] valves;
20  
21      /***
22       * Constructor that provides the descriptor for building the pipeline
23       */
24      public GrizzlyPipeline(String name, List valveList) throws Exception
25      {
26          valves = (Valve[]) valveList.toArray(new Valve[valveList.size()]);
27          setName(name);
28      }
29  
30      public void initialize() throws PipelineException
31      {
32  
33      }
34  
35      /***
36       * Set the name of this pipeline.
37       * 
38       * @param name
39       *            Name of this pipeline.
40       */
41      public void setName(String name)
42      {
43          this.name = name;
44      }
45  
46      /***
47       * Get the name of this pipeline.
48       * 
49       * @return String Name of this pipeline.
50       */
51      public String getName()
52      {
53          return name;
54      }
55  
56      public synchronized void addValve(Valve valve)
57      {
58          // Add this Valve to the set associated with this Pipeline
59          Valve[] results = new Valve[valves.length + 1];
60          System.arraycopy(valves, 0, results, 0, valves.length);
61          results[valves.length] = valve;
62          valves = results;
63      }
64  
65      public synchronized Valve[] getValves()
66      {
67          Valve[] results = new Valve[valves.length];
68          System.arraycopy(valves, 0, results, 0, valves.length);
69          return results;
70      }
71  
72      public synchronized void removeValve(Valve valve)
73      {
74          // Locate this Valve in our list
75          int index = -1;
76          for (int i = 0; i < valves.length; i++)
77          {
78              if (valve == valves[i])
79              {
80                  index = i;
81                  break;
82              }
83          }
84          if (index < 0) { return; }
85  
86          // Remove this valve from our list
87          Valve[] results = new Valve[valves.length - 1];
88          int n = 0;
89          for (int i = 0; i < valves.length; i++)
90          {
91              if (i == index)
92              {
93                  continue;
94              }
95              results[n++] = valves[i];
96          }
97          valves = results;
98      }
99  
100     public void invoke(StreamStorage storage) throws PipelineException
101     {
102 
103         Invocation invocation;
104         // TODO use java 5 locks or compare and swap if possible
105         synchronized (this)
106         {
107             invocation = new Invocation(valves);
108         }
109         // Invoke the first Valve in this pipeline for this request
110         invocation.invokeNext(storage);
111     }
112 
113     private static final class Invocation implements ValveContext
114     {
115 
116         private final Valve[] valves;
117 
118         private int at = 0;
119 
120         public Invocation(Valve[] valves)
121         {
122             this.valves = valves;
123         }
124 
125         public void invokeNext(StreamStorage storage) throws PipelineException
126         {
127             if (at < valves.length)
128             {
129                 Valve next = valves[at];
130                 at++;
131                 next.invoke(storage, this);
132             }
133         }
134     }
135 
136 }