KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > InvokeContext


1 /*
2  * Copyright 1999-2005 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.apache.cocoon.components.treeprocessor;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.ComponentException;
20 import org.apache.avalon.framework.component.ComponentManager;
21 import org.apache.avalon.framework.component.ComponentSelector;
22 import org.apache.avalon.framework.component.Recomposable;
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24 import org.apache.cocoon.components.CocoonComponentManager;
25 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
26 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver;
27 import org.apache.cocoon.environment.Redirector;
28 import org.apache.cocoon.sitemap.SitemapErrorHandler;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * The invocation context of <code>ProcessingNode</code>s.
38  *
39  * <p>This class serves two purposes:
40  * <ul>
41  * <li>Avoid explicit enumeration of all needed parameters in
42  * {@link ProcessingNode#invoke(org.apache.cocoon.environment.Environment, InvokeContext)},
43  * thus allowing easier addition of new parameters,</li>
44  * <li>Hold pipelines, and provide "just in time" lookup for them.</li>
45  * </ul>
46  *
47  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
48  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
49  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
50  * @version $Id: InvokeContext.java 280810 2005-09-14 09:49:38Z cziegeler $
51  */

52 public class InvokeContext extends AbstractLogEnabled
53                            implements Recomposable, Disposable {
54
55     private List JavaDoc mapStack = new ArrayList JavaDoc();
56     private HashMap JavaDoc nameToMap = new HashMap JavaDoc();
57     private HashMap JavaDoc mapToName = new HashMap JavaDoc();
58
59     /** True if building pipeline only, not processing it. */
60     private boolean isBuildingPipelineOnly;
61
62     /** The redirector */
63     protected Redirector redirector;
64
65
66     /** The current component manager, as set by the last call to compose() or recompose() */
67     private ComponentManager currentManager;
68
69     /** The component manager that was used to get the pipelines */
70     private ComponentManager pipelinesManager;
71
72     /** The name of the processing pipeline component */
73     protected String JavaDoc processingPipelineName;
74
75     /** The parameters for the processing pipeline */
76     protected Map JavaDoc processingPipelineParameters;
77
78     /** The object model used to resolve processingPipelineParameters */
79     protected Map JavaDoc processingPipelineObjectModel;
80
81     /** The Selector for the processing pipeline */
82     protected ComponentSelector pipelineSelector;
83
84     /** The ProcessingPipeline used */
85     protected ProcessingPipeline processingPipeline;
86
87     /** The error handler for the pipeline. */
88     protected SitemapErrorHandler errorHandler;
89
90     /**
91      * Create an <code>InvokeContext</code> without existing pipelines. This also means
92      * the current request is external.
93      */

94     public InvokeContext() {
95         this.isBuildingPipelineOnly = false;
96     }
97
98     /**
99      * Determines if the Pipeline been set for this context
100      */

101     public boolean pipelineIsSet() {
102         return (this.processingPipeline != null);
103     }
104
105     /**
106      * Create an <code>InvokeContext</code>
107      */

108     public InvokeContext(boolean isBuildingPipelineOnly) {
109         this.isBuildingPipelineOnly = isBuildingPipelineOnly;
110     }
111
112     /**
113      * Composable Interface
114      */

115     public void compose(ComponentManager manager) throws ComponentException {
116         this.currentManager = manager;
117     }
118
119     /**
120      * Recomposable interface
121      */

122     public void recompose(ComponentManager manager) throws ComponentException {
123         this.currentManager = manager;
124         if (this.processingPipeline != null) {
125             this.processingPipeline.recompose(manager);
126         }
127     }
128
129     /**
130      * Informs the context about a new pipeline section
131      */

132     public void inform(String JavaDoc pipelineName,
133                        Map JavaDoc parameters,
134                        Map JavaDoc objectModel) {
135         this.processingPipelineName = pipelineName;
136         this.processingPipelineParameters = parameters;
137         this.processingPipelineObjectModel = objectModel;
138     }
139
140     /**
141      * Get the current <code>ProcessingPipeline</code>
142      */

143     public ProcessingPipeline getProcessingPipeline()
144     throws Exception JavaDoc {
145         if (this.processingPipeline == null) {
146             // Keep current manager for proper release
147
this.pipelinesManager = this.currentManager;
148
149             this.pipelineSelector = (ComponentSelector)this.pipelinesManager.lookup(ProcessingPipeline.ROLE+"Selector");
150             this.processingPipeline = (ProcessingPipeline)this.pipelineSelector.select(this.processingPipelineName);
151             this.processingPipeline.recompose( this.pipelinesManager );
152             this.processingPipeline.setup(
153                   VariableResolver.buildParameters(this.processingPipelineParameters,
154                                                    this,
155                                                    this.processingPipelineObjectModel)
156             );
157             if (this.isBuildingPipelineOnly) {
158                 CocoonComponentManager.addComponentForAutomaticRelease(this.pipelineSelector,
159                                                                        this.processingPipeline,
160                                                                        this.pipelinesManager);
161             }
162             this.processingPipeline.setErrorHandler(this.errorHandler);
163         }
164         return this.processingPipeline;
165     }
166
167     /**
168      * Set the processing pipeline for sub-sitemaps
169      */

170     public void setProcessingPipeline(ProcessingPipeline pipeline) {
171         this.processingPipeline = pipeline;
172     }
173
174     /**
175      * Are we building a pipeline (and not executing it) ?
176      */

177     public final boolean isBuildingPipelineOnly() {
178         return this.isBuildingPipelineOnly;
179     }
180
181     /**
182      * Get the current Map stack used to resolve expressions.
183      */

184     public final List JavaDoc getMapStack() {
185         return this.mapStack;
186     }
187
188     /**
189      * Get the result Map by anchor name
190      */

191     public final Map JavaDoc getMapByAnchor(String JavaDoc anchor) {
192         return (Map JavaDoc) this.nameToMap.get(anchor);
193     }
194
195     /**
196      * Push a Map on top of the current Map stack.
197      */

198     public final void pushMap(String JavaDoc anchorName, Map JavaDoc map) {
199         this.mapStack.add(map);
200
201         if (getLogger().isDebugEnabled()) {
202             dumpParameters();
203         }
204
205         if (anchorName != null) {
206             if (!this.nameToMap.containsKey(anchorName)) {
207                 this.nameToMap.put(anchorName,map);
208                 this.mapToName.put(map,anchorName);
209             } else {
210                 if (getLogger().isErrorEnabled()) {
211                     getLogger().error("name [" + anchorName + "] clashes");
212                 }
213             }
214         }
215     }
216
217     /**
218      * Dumps all sitemap parameters to log
219      */

220     protected void dumpParameters() {
221         if (!this.mapStack.isEmpty()) {
222             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
223
224             sb.append("\nCurrent Sitemap Parameters:\n");
225             String JavaDoc path = "";
226
227             for (int i = this.mapStack.size() - 1; i >= 0; i--) {
228                 Map JavaDoc map = (Map JavaDoc) this.mapStack.get(i);
229                 sb.append("LEVEL ").append(i+1);
230                 if (this.mapToName.containsKey(map)) {
231                     sb.append(" is named '").append(String.valueOf(this.mapToName.get(map))).append("'");
232                 }
233                 sb.append("\n");
234
235                 for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext(); ) {
236                     final Map.Entry JavaDoc me = (Map.Entry JavaDoc)iter.next();
237                     final Object JavaDoc key = me.getKey();
238                     sb.append("PARAM: '").append(path).append(key).append("' ");
239                     sb.append("VALUE: '").append(me.getValue()).append("'\n");
240                 }
241                 path = "../" + path;
242             }
243
244             getLogger().debug(sb.toString());
245         }
246     }
247
248     /**
249      * Pop the topmost element of the current Map stack.
250      */

251     public final void popMap() {
252         Object JavaDoc map = this.mapStack.remove(this.mapStack.size() - 1);
253         Object JavaDoc name = this.mapToName.get(map);
254         this.mapToName.remove(map);
255         this.nameToMap.remove(name);
256     }
257
258     /**
259      * Set the redirector to be used by nodes that need it.
260      *
261      * @param redirector the redirector
262      */

263     public void setRedirector(Redirector redirector) {
264         this.redirector = redirector;
265     }
266
267     /**
268      * Get the redirector to be used by nodes that need it.
269      *
270      * @return the redirector
271      */

272     public Redirector getRedirector() {
273         return this.redirector;
274     }
275
276     /**
277      * Release the pipelines, if any, if they were looked up by this context.
278      */

279     public void dispose() {
280         if (!this.isBuildingPipelineOnly && this.pipelinesManager != null) {
281             if (this.pipelineSelector != null) {
282                 this.pipelineSelector.release(this.processingPipeline);
283                 this.processingPipeline = null;
284                 this.pipelinesManager.release(this.pipelineSelector);
285                 this.pipelineSelector = null;
286             }
287             this.pipelinesManager = null;
288
289             this.processingPipelineName = null;
290             this.processingPipelineParameters = null;
291             this.processingPipelineObjectModel = null;
292
293             this.errorHandler = null;
294         }
295     }
296
297     /**
298      * Set the error handler for the pipeline.
299      */

300     public void setErrorHandler(SitemapErrorHandler handler) {
301         this.errorHandler = handler;
302     }
303 }
304
Popular Tags