KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > sitemap > PipelineNode


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.sitemap;
17
18 import org.apache.avalon.framework.component.ComponentManager;
19 import org.apache.avalon.framework.component.Composable;
20 import org.apache.avalon.framework.logger.Logger;
21
22 import org.apache.cocoon.ConnectionResetException;
23 import org.apache.cocoon.ResourceNotFoundException;
24 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode;
25 import org.apache.cocoon.components.treeprocessor.InvokeContext;
26 import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode;
27 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
28 import org.apache.cocoon.environment.Environment;
29 import org.apache.cocoon.sitemap.SitemapErrorHandler;
30
31 import java.util.Map JavaDoc;
32
33 /**
34  * Handles <map:pipeline>
35  *
36  * @author <a HREF="mailto:juergen.seitz@basf-it-services.com">J&uuml;rgen Seitz</a>
37  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
38  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
39  * @author <a HREF="mailto:gianugo@apache.org">Gianugo Rabellino</a>
40  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
41  * @version $Id: PipelineNode.java 280810 2005-09-14 09:49:38Z cziegeler $
42  */

43 public class PipelineNode extends AbstractParentProcessingNode
44                           implements Composable, ParameterizableProcessingNode {
45
46     // TODO: handle a 'fail-hard' environment attribute
47
// can be useful to stop off-line generation when there's an error
48

49     private ProcessingNode[] children;
50
51     private ErrorHandlerHelper errorHandlerHelper;
52
53     private boolean internalOnly;
54
55     /** Is it the last <pipeline> in the enclosing <pipelines> ? */
56     private boolean isLast;
57
58     /** The component name of the processing pipeline */
59     protected String JavaDoc processingPipeline;
60
61     /** Optional sitemap parameters */
62     protected Map JavaDoc parameters;
63
64     /**
65      * A constructor to receive the optional expires parameter
66      * and optional parameters for the processing pipeline
67      */

68     public PipelineNode(String JavaDoc name) {
69         this.processingPipeline = name;
70         this.errorHandlerHelper = new ErrorHandlerHelper();
71     }
72
73     /**
74      * The component manager is used to create error pipelines
75      */

76     public void compose(ComponentManager manager) {
77         this.errorHandlerHelper.compose(manager);
78     }
79
80     public void enableLogging(Logger logger) {
81         super.enableLogging(logger);
82         this.errorHandlerHelper.enableLogging(logger);
83     }
84
85     public void setChildren(ProcessingNode[] nodes) {
86         this.children = nodes;
87     }
88
89     public void setParameters(Map JavaDoc parameterMap) {
90         this.parameters = parameterMap;
91     }
92
93     public void setLast(boolean isLast) {
94         this.isLast = isLast;
95     }
96
97     public void set404Handler(ProcessingNode node) {
98         this.errorHandlerHelper.set404Handler(node);
99     }
100
101     public void set500Handler(ProcessingNode node) {
102         this.errorHandlerHelper.set500Handler(node);
103     }
104
105     public void setInternalOnly(boolean internalOnly) {
106         this.internalOnly = internalOnly;
107     }
108
109     public final boolean invoke(Environment env, InvokeContext context)
110     throws Exception JavaDoc {
111         boolean passThrough;
112         Object JavaDoc passThroughRaw = env.getAttribute(MountNode.COCOON_PASS_THROUGH);
113         if (passThroughRaw == null) {
114             // Use default value
115
passThrough = false;
116         } else {
117             passThrough = ((Boolean JavaDoc) passThroughRaw).booleanValue();
118         }
119
120         // Always fail on external request if pipeline is internal only.
121
if (this.internalOnly && env.isExternal()) {
122             if (!this.isLast || passThrough) {
123                 return false;
124             }
125
126             // Do not use internal-only pipeline error handler for external requests.
127
throw new ResourceNotFoundException("No pipeline matched request: " +
128                                                 env.getURIPrefix() + env.getURI());
129         }
130
131         context.inform(this.processingPipeline, this.parameters, env.getObjectModel());
132         try {
133             if (this.errorHandlerHelper.isInternal()) {
134                 // Set internal error handler in the pipeline
135
context.setErrorHandler(
136                         new SitemapErrorHandler(this.errorHandlerHelper, env, context));
137             } else {
138                 // Reset internal error handler (previous pipeline might had set it)
139
context.setErrorHandler(null);
140             }
141
142             if (invokeNodes(children, env, context)) {
143                 return true;
144             } else if (!this.isLast || passThrough) {
145                 return false;
146             }
147
148             throw new ResourceNotFoundException("No pipeline matched request: " +
149                                                 env.getURIPrefix() + env.getURI());
150
151         } catch (ConnectionResetException e) {
152             // Will be reported by CocoonServlet, rethrowing
153
throw e;
154         } catch (Exception JavaDoc e) {
155             // Invoke error handler
156
return this.errorHandlerHelper.invokeErrorHandler(e, env, context);
157         }
158     }
159 }
160
Popular Tags