KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > pipeline > TurbinePipeline


1 package org.apache.turbine.pipeline;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58
59 import org.apache.turbine.Pipeline;
60 import org.apache.turbine.RunData;
61 import org.apache.turbine.TurbineException;
62 import org.apache.turbine.Valve;
63 import org.apache.turbine.ValveContext;
64
65 /**
66  * Flexible implementation of a {@link org.apache.turbine.Pipeline}.
67  * Originally based on code from Catalina and ideas from Apache httpd.
68  *
69  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
70  * @author <a HREF="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
71  */

72 public class TurbinePipeline
73     implements Pipeline, ValveContext
74 {
75     /**
76      * The "Turbine Classic" pipeline.
77      */

78     public static String JavaDoc CLASSIC_PIPELINE =
79         "conf/turbine-classic-pipeline.xml";
80
81     /**
82      * Name of this pipeline.
83      */

84     protected String JavaDoc name;
85
86     /**
87      * The set of Valves associated with this Pipeline.
88      */

89     protected Valve[] valves = new Valve[0];
90     
91     /**
92      * The per-thread execution state for processing through this
93      * pipeline. The actual value is a java.lang.Integer object
94      * containing the subscript into the <code>values</code> array, or
95      * a subscript equal to <code>values.length</code> if the basic
96      * Valve is currently being processed.
97      */

98     protected ThreadLocal JavaDoc state = new ThreadLocal JavaDoc();
99
100     /**
101      * @see org.apache.turbine.Pipeline#initialize()
102      */

103     public void initialize()
104         throws Exception JavaDoc
105     {
106         // Valve implementations are added to this Pipeline using XStream.
107
if (state==null){
108             state = new ThreadLocal JavaDoc();
109         }
110         
111         // Initialize the valves
112
for (int i = 0; i < valves.length; i++)
113         {
114             valves[i].initialize();
115         }
116     }
117
118     /**
119      * Set the name of this pipeline.
120      *
121      * @param name Name of this pipeline.
122      */

123     public void setName(String JavaDoc name)
124     {
125         this.name = name;
126     }
127     
128     /**
129      * Get the name of this pipeline.
130      *
131      * @return String Name of this pipeline.
132      */

133     public String JavaDoc getName()
134     {
135         return name;
136     }
137
138     /**
139      * @see org.apache.turbine.Pipeline#addValve(Valve)
140      */

141     public void addValve(Valve valve)
142     {
143         // Add this Valve to the set associated with this Pipeline
144
synchronized (valves)
145         {
146             Valve[] results = new Valve[valves.length + 1];
147             System.arraycopy(valves, 0, results, 0, valves.length);
148             results[valves.length] = valve;
149             valves = results;
150         }
151     }
152
153     /**
154      * @see org.apache.turbine.Pipeline#getValves()
155      */

156     public Valve[] getValves()
157     {
158         synchronized (valves)
159         {
160             Valve[] results = new Valve[valves.length];
161             System.arraycopy(valves, 0, results, 0, valves.length);
162             return results;
163         }
164     }
165
166     /**
167      * @see org.apache.turbine.Pipeline#removeValve(Valve)
168      */

169     public void removeValve(Valve valve)
170     {
171         synchronized (valves)
172         {
173             // Locate this Valve in our list
174
int index = -1;
175             for (int i = 0; i < valves.length; i++)
176             {
177                 if (valve == valves[i])
178                 {
179                     index = i;
180                     break;
181                 }
182             }
183             if (index < 0)
184             {
185                 return;
186             }
187
188             // Remove this valve from our list
189
Valve[] results = new Valve[valves.length - 1];
190             int n = 0;
191             for (int i = 0; i < valves.length; i++)
192             {
193                 if (i == index)
194                 {
195                     continue;
196                 }
197                 results[n++] = valves[i];
198             }
199             valves = results;
200         }
201     }
202
203     /**
204      * @see org.apache.turbine.Pipeline#invoke(RunData)
205      */

206     public void invoke(RunData data)
207         throws TurbineException, IOException JavaDoc
208     {
209         // Initialize the per-thread state for this thread
210
state.set(new Integer JavaDoc(0));
211
212         // Invoke the first Valve in this pipeline for this request
213
invokeNext(data);
214     }
215
216     /**
217      * @see org.apache.turbine.ValveContext#invokeNext(RunData)
218      */

219     public void invokeNext(RunData data)
220         throws TurbineException, IOException JavaDoc
221     {
222         // Identify the current subscript for the current request thread
223
Integer JavaDoc current = (Integer JavaDoc) state.get();
224         int subscript = current.intValue();
225
226         if (subscript < valves.length)
227         {
228             // Invoke the requested Valve for the current request
229
// thread and increment its thread-local state.
230
state.set(new Integer JavaDoc(subscript + 1));
231             valves[subscript].invoke(data, this);
232         }
233     }
234 }
235
Popular Tags