KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > pipelines > impl > GenericPipeline


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jahia.pipelines.impl;
17
18 import java.util.ArrayList JavaDoc;
19
20 import org.jahia.pipelines.Pipeline;
21 import org.jahia.pipelines.PipelineDescriptor;
22 import org.jahia.pipelines.PipelineException;
23 import org.jahia.pipelines.valves.Valve;
24 import org.jahia.pipelines.valves.ValveContext;
25 import org.jahia.pipelines.valves.ValveDescriptor;
26
27 /**
28  * Flexible implementation of a {@link org.jahia.pipelines.Pipeline}.
29  *
30  * @author <a HREF="mailto:jason@zenplex.com">Jason van Zyl</a>
31  * @author <a HREF="mailto:david@bluesunrise.com">David Sean Taylor</a>
32  * @version $Id: GenericPipeline.java 9810 2005-07-07 10:12:18Z tdraier $
33  */

34 public class GenericPipeline implements Pipeline, ValveContext {
35
36     /** Name of this pipeline. */
37     protected String JavaDoc name;
38
39     private Valve[] valves = new Valve[0];
40
41     /**
42      * The per-thread execution state for processing through this
43      * pipeline. The actual value is a java.lang.Integer object
44      * containing the subscript into the <code>values</code> array, or
45      * a subscript equal to <code>values.length</code> if the basic
46      * Valve is currently being processed.
47      *
48      */

49     protected ThreadLocal JavaDoc state = new ThreadLocal JavaDoc();
50
51     /**
52      * Descriptor for this pipeline
53      */

54     protected PipelineDescriptor descriptor;
55
56     public GenericPipeline () {
57
58     }
59
60     /**
61      * <p>Set the descriptor used to create this pipeline.</p>
62      */

63     public void setDescriptor (PipelineDescriptor descriptor) {
64         this.descriptor = descriptor;
65     }
66
67     /**
68      * <p>Get the descriptor used to create this pipeline.</p>
69      */

70     public PipelineDescriptor getDescriptor () {
71         return descriptor;
72     }
73
74     /**
75      * @see org.apache.plexus.summit.Pipeline#init()
76      */

77     public void initialize ()
78         throws PipelineException {
79         setName(getDescriptor().getName());
80         ArrayList JavaDoc valveDescriptors = (ArrayList JavaDoc) getDescriptor().
81                                      getValveDescriptors();
82
83         for (int i = 0; i < valveDescriptors.size(); i++) {
84             ValveDescriptor vDescriptor = (ValveDescriptor) valveDescriptors.
85                                           get(i);
86             String JavaDoc className = vDescriptor.getClassName();
87
88             Valve valve;
89
90             try {
91                 valve = (Valve) Class.forName(className).newInstance();
92             } catch (Exception JavaDoc e) {
93                 throw new PipelineException("Failed to create valve: " +
94                                             className, e);
95             }
96
97             addValve(valve);
98         }
99
100         // Valve implementations are added to this Pipeline using the
101
// Mapper.
102

103         // Initialize the valves
104
for (int i = 0; i < valves.length; i++) {
105             //valves[i].setApplicationView(getApplicationView());
106
valves[i].initialize();
107         }
108     }
109
110     /**
111      * Set the name of this pipeline.
112      *
113      * @param name Name of this pipeline.
114      */

115     public void setName (String JavaDoc name) {
116         this.name = name;
117     }
118
119     /**
120      * Get the name of this pipeline.
121      *
122      * @return String Name of this pipeline.
123      */

124     public String JavaDoc getName () {
125         return name;
126     }
127
128     public Valve[] getValves () {
129         synchronized (valves) {
130             Valve[] results = new Valve[valves.length];
131             System.arraycopy(valves, 0, results, 0, valves.length);
132             return results;
133         }
134     }
135
136     public void addValve (Valve valve) {
137         // Add this Valve to the set associated with this Pipeline
138
synchronized (valves) {
139             Valve[] results = new Valve[valves.length + 1];
140             System.arraycopy(valves, 0, results, 0, valves.length);
141             results[valves.length] = valve;
142             valves = results;
143         }
144     }
145
146     public void removeValve (Valve valve) {
147         synchronized (valves) {
148             // Locate this Valve in our list
149
int index = -1;
150             for (int i = 0; i < valves.length; i++) {
151                 if (valve == valves[i]) {
152                     index = i;
153                     break;
154                 }
155             }
156             if (index < 0) {
157                 return;
158             }
159
160             // Remove this valve from our list
161
Valve[] results = new Valve[valves.length - 1];
162             int n = 0;
163             for (int i = 0; i < valves.length; i++) {
164                 if (i == index) {
165                     continue;
166                 }
167                 results[n++] = valves[i];
168             }
169             valves = results;
170         }
171     }
172
173     public void invoke (Object JavaDoc context)
174         throws PipelineException {
175         // Initialize the per-thread state for this thread
176
state.set(new Integer JavaDoc(0));
177
178         // Invoke the first Valve in this pipeline for this request
179
invokeNext(context);
180     }
181
182     public void invokeNext (Object JavaDoc context)
183         throws PipelineException {
184         // Identify the current subscript for the current request thread
185
Integer JavaDoc current = (Integer JavaDoc) state.get();
186         int valvePos = current.intValue();
187
188         if (valvePos < valves.length) {
189             // Invoke the requested Valve for the current request
190
// thread and increment its thread-local state.
191
state.set(new Integer JavaDoc(valvePos + 1));
192             valves[valvePos].invoke(context, this);
193         }
194
195     }
196
197     // BEGIN [added by Pascal Aubry for CAS authentication]
198
/**
199      * @see org.jahia.pipelines.Pipeline#hasValveOfClass(java.lang.Class)
200      */

201     public boolean hasValveOfClass(Class JavaDoc c) {
202         return getFirstValveOfClass(c) != null;
203     }
204
205     /**
206      * @see org.jahia.pipelines.Pipeline#getFirstValveOfClass(java.lang.Class)
207      */

208     public Valve getFirstValveOfClass(Class JavaDoc c) {
209         for (int i = 0; i < this.valves.length; i++) {
210             if (c.isInstance(valves[i])) {
211                 return valves[i];
212             }
213         }
214         return null;
215     }
216     // END [added by Pascal Aubry for CAS authentication]
217

218
219 }
220
Popular Tags