1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.pipeline;
1819import org.apache.jetspeed.pipeline.valve.Valve;
20import org.apache.jetspeed.pipeline.valve.ValveContext;
21import org.apache.jetspeed.request.RequestContext;
2223import java.util.List;
2425/***26 * Flexible implementation of a {@link Pipeline}. <p/> <br/><br/> Suggested27 * order of valves:28 * <ul>29 * <li>ContainerValve</li>30 * <li>CapabilityValve</li>31 * <li>UserProfilerValve</li>32 * <li>PageProfilerValve</li>33 * <li>ActionValve</li>34 * <li>LayoutValve</li>35 * <li>ContentValve</li>36 * <li>AggregateValve</li>37 * <li>CleanupValve</li>38 * </ul>39 * 40 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>41 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>42 * @version $Id: JetspeedPipeline.java 516448 2007-03-09 16:25:47Z ate $43 */44publicclassJetspeedPipeline implements Pipeline
45 {
4647/***48 * Name of this pipeline.49 */50protected String name;
5152/***53 * The set of Valves associated with this Pipeline.54 */55protected Valve[] valves;
5657/***58 * Constructor that provides the descriptor for building the pipeline59 */60publicJetspeedPipeline(String name, List valveList) throws Exception
61 {
62 valves = (Valve[]) valveList.toArray(new Valve[valveList.size()]);
63 setName(name);
64 }
6566publicvoid initialize() throws PipelineException
67 {
6869 }
7071/***72 * Set the name of this pipeline.73 * 74 * @param name75 * Name of this pipeline.76 */77publicvoid setName(String name)
78 {
79this.name = name;
80 }
8182/***83 * Get the name of this pipeline.84 * 85 * @return String Name of this pipeline.86 */87public String getName()
88 {
89return name;
90 }
9192publicsynchronizedvoid addValve(Valve valve)
93 {
94// Add this Valve to the set associated with this Pipeline95 Valve[] results = new Valve[valves.length + 1];
96 System.arraycopy(valves, 0, results, 0, valves.length);
97 results[valves.length] = valve;
98 valves = results;
99 }
100101publicsynchronized Valve[] getValves()
102 {
103 Valve[] results = new Valve[valves.length];
104 System.arraycopy(valves, 0, results, 0, valves.length);
105return results;
106 }
107108publicsynchronizedvoid removeValve(Valve valve)
109 {
110// Locate this Valve in our list111int index = -1;
112for (int i = 0; i < valves.length; i++)
113 {
114if (valve == valves[i])
115 {
116 index = i;
117break;
118 }
119 }
120if (index < 0) { return; }
121122// Remove this valve from our list123 Valve[] results = new Valve[valves.length - 1];
124int n = 0;
125for (int i = 0; i < valves.length; i++)
126 {
127if (i == index)
128 {
129continue;
130 }
131 results[n++] = valves[i];
132 }
133 valves = results;
134 }
135136publicvoid invoke(RequestContext request) throws PipelineException
137 {
138139 Invocation invocation;
140// TODO use java 5 locks or compare and swap if possible141synchronized (this)
142 {
143 invocation = new Invocation(valves);
144 }
145// Invoke the first Valve in this pipeline for this request146 invocation.invokeNext(request);
147 }
148149privatestaticfinalclass Invocation implements ValveContext
150 {
151152privatefinal Valve[] valves;
153154privateint at = 0;
155156public Invocation(Valve[] valves)
157 {
158this.valves = valves;
159 }
160161publicvoid invokeNext(RequestContext request) throws PipelineException
162 {
163if (at < valves.length)
164 {
165 Valve next = valves[at];
166 at++;
167 next.invoke(request, this);
168 }
169 }
170 }
171172 }