cziegeler 2003/04/11 06:14:52
Modified: src/java/org/apache/cocoon/components/treeprocessor/sitemap PipelineNode.java PipelinesNodeBuilder.java PipelinesNode.java src/java/org/apache/cocoon/components/treeprocessor treeprocessor-builtins.xml Added: src/java/org/apache/cocoon/components/treeprocessor/sitemap ErrorHandlerHelper.java Log: Applying patch from Juergen Seitz ([EMAIL PROTECTED]) and Bjoern Luetkemeier ([EMAIL PROTECTED]): - Added ErrorHandler to PipelinesNode (hierarchical error handling) - Fixing bug when error handler does not handle the error: the exception is now rethrown Revision Changes Path 1.4 +17 -53 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelineNode.java Index: PipelineNode.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelineNode.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- PipelineNode.java 7 Apr 2003 08:21:30 -0000 1.3 +++ PipelineNode.java 11 Apr 2003 13:14:51 -0000 1.4 @@ -50,26 +50,25 @@ */ package org.apache.cocoon.components.treeprocessor.sitemap; +import java.util.Map; + import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.logger.Logger; import org.apache.cocoon.ConnectionResetException; -import org.apache.cocoon.Constants; import org.apache.cocoon.ResourceNotFoundException; -import org.apache.cocoon.components.notification.Notifying; -import org.apache.cocoon.components.notification.NotifyingBuilder; import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode; import org.apache.cocoon.components.treeprocessor.InvokeContext; import org.apache.cocoon.components.treeprocessor.ParameterizableProcessingNode; import org.apache.cocoon.components.treeprocessor.ProcessingNode; import org.apache.cocoon.environment.Environment; -import org.apache.cocoon.environment.ObjectModelHelper; - -import java.util.Map; /** * Handles <map:pipeline> * + * @author <a href="mailto:[EMAIL PROTECTED]">Jürgen Seitz</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Björn Lütkemeier</a> * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> @@ -88,6 +87,8 @@ private ProcessingNode error500; + private ErrorHandlerHelper errorHandlerHelper = new ErrorHandlerHelper(); + private ComponentManager manager; private boolean internalOnly = false; @@ -114,8 +115,15 @@ */ public void compose(ComponentManager manager) { this.manager = manager; + errorHandlerHelper.compose(manager); } + public void enableLogging(Logger logger) + { + super.enableLogging(logger); + errorHandlerHelper.enableLogging(logger); + } + public void setChildren(ProcessingNode[] nodes) { this.children = nodes; } @@ -171,11 +179,11 @@ } else if (error404 != null && ex instanceof ResourceNotFoundException) { // Invoke 404-specific handler - return invokeErrorHandler(error404, ex, env); + return errorHandlerHelper.invokeErrorHandler(error404, ex, env); } else if (error500 != null) { // Invoke global handler - return invokeErrorHandler(error500, ex, env); + return errorHandlerHelper.invokeErrorHandler(error500, ex, env); } else { // No handler : propagate @@ -183,49 +191,5 @@ } } } - - private boolean invokeErrorHandler(ProcessingNode node, Exception ex, Environment env) - throws Exception { - InvokeContext errorContext = null; - - try { - // Try to reset the response to avoid mixing already produced output - // and error page. - env.tryResetResponse(); - - // Build a new context - errorContext = new InvokeContext(); - errorContext.enableLogging(getLogger()); - errorContext.compose(this.manager); - - // Create a Notifying - NotifyingBuilder notifyingBuilder= (NotifyingBuilder)this.manager.lookup(NotifyingBuilder.ROLE); - Notifying currentNotifying = null; - try { - currentNotifying = notifyingBuilder.build(this, ex); - } finally { - this.manager.release(notifyingBuilder); - } - - Map objectModel = env.getObjectModel(); - // Add it to the object model - objectModel.put(Constants.NOTIFYING_OBJECT, currentNotifying); - - // Also add the exception - objectModel.put(ObjectModelHelper.THROWABLE_OBJECT, ex); - - // <notifier> is added in HandleErrorsNode - return node.invoke(env, errorContext); - } catch (Exception subEx) { - getLogger().error("An exception occured in while handling errors at " + node.getLocation(), subEx); - // Rethrow it : it will either be handled by the parent sitemap or by the environment (e.g. Cocoon servlet) - throw subEx; - } finally { - if (errorContext != null) { - errorContext.dispose(); - } - } - } - } 1.2 +28 -4 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelinesNodeBuilder.java Index: PipelinesNodeBuilder.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelinesNodeBuilder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PipelinesNodeBuilder.java 9 Mar 2003 00:09:22 -0000 1.1 +++ PipelinesNodeBuilder.java 11 Apr 2003 13:14:51 -0000 1.2 @@ -50,15 +50,21 @@ */ package org.apache.cocoon.components.treeprocessor.sitemap; +import java.util.ArrayList; +import java.util.List; + import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.components.treeprocessor.ContainerNodeBuilder; import org.apache.cocoon.components.treeprocessor.ProcessingNode; +import org.apache.cocoon.components.treeprocessor.ProcessingNodeBuilder; /** * Builds a <map:pipelines> * + * @author <a href="mailto:[EMAIL PROTECTED]">Jürgen Seitz</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Björn Lütkemeier</a> * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @version CVS $Id$ @@ -77,13 +83,31 @@ PipelinesNode node = new PipelinesNode(this.treeBuilder.getProcessor()); this.treeBuilder.setupNode(node, config); - ProcessingNode[] children = buildChildNodes(config); - if (children.length == 0) { + Configuration[] childConfigs = config.getChildren(); + List children = new ArrayList(); + HandleErrorsNode handler = null; + + for (int i = 0; i < childConfigs.length; i++) { + + Configuration childConfig = childConfigs[i]; + if (isChild(childConfig)) { + + ProcessingNodeBuilder builder = this.treeBuilder.createNodeBuilder(childConfig); + if (builder instanceof HandleErrorsNodeBuilder) { + handler = (HandleErrorsNode)builder.buildNode(childConfig); + } else { + // Regular builder + children.add(builder.buildNode(childConfig)); + } + } + } + if (children.size() == 0) { String msg = "There must be at least one pipeline at " + config.getLocation(); throw new ConfigurationException(msg); } - node.setChildren(children); + node.setChildren(toNodeArray(children)); + node.setErrorHandler(handler); return node; } 1.2 +32 -7 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelinesNode.java Index: PipelinesNode.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelinesNode.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PipelinesNode.java 9 Mar 2003 00:09:22 -0000 1.1 +++ PipelinesNode.java 11 Apr 2003 13:14:51 -0000 1.2 @@ -50,9 +50,12 @@ */ package org.apache.cocoon.components.treeprocessor.sitemap; +import java.util.Map; + import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; +import org.apache.avalon.framework.logger.Logger; import org.apache.cocoon.Processor; import org.apache.cocoon.components.treeprocessor.InvokeContext; import org.apache.cocoon.components.treeprocessor.ProcessingNode; @@ -61,11 +64,11 @@ import org.apache.cocoon.environment.ForwardRedirector; import org.apache.cocoon.environment.Redirector; -import java.util.Map; - /** * Handles <map:pipelines> * + * @author <a href="mailto:[EMAIL PROTECTED]">Jürgen Seitz</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Björn Lütkemeier</a> * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @version CVS $Id$ @@ -77,14 +80,18 @@ private static final String REDIRECTOR_ATTR = "sitemap:redirector"; private ComponentManager manager; + + private ErrorHandlerHelper errorHandlerHelper = new ErrorHandlerHelper(); private Processor processor; + private ProcessingNode errorHandler; + /** * Constructor - * @param processor The processor for this sitemap + * @param processor The processor for this sitemap */ - public PipelinesNode(Processor processor) { + public PipelinesNode(Processor processor) { this.processor = processor; } @@ -94,8 +101,20 @@ */ public void compose(ComponentManager manager) { this.manager = manager; + errorHandlerHelper.compose(manager); } + public void enableLogging(Logger logger) + { + super.enableLogging(logger); + errorHandlerHelper.enableLogging(logger); + } + + public void setErrorHandler(ProcessingNode node) + { + errorHandler = node; + } + public void setChildren(ProcessingNode[] nodes) { // Mark the last pipeline so that it can throw a ResourceNotFoundException @@ -141,9 +160,15 @@ // such as the URI of the mount point ? return invokeNodes(this.children, env, context); - + } catch (Exception ex) { + if (errorHandler != null) { + // Invoke pipelines handler + return errorHandlerHelper.invokeErrorHandler(errorHandler, ex, env); + } else { + // No handler : propagate + throw ex; + } } finally { - // Restore old redirector and resolver env.setAttribute(REDIRECTOR_ATTR, oldRedirector); objectModel.put(OBJECT_SOURCE_RESOLVER, oldResolver); 1.1 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/sitemap/ErrorHandlerHelper.java Index: ErrorHandlerHelper.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.components.treeprocessor.sitemap; import java.util.Map; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.cocoon.Constants; import org.apache.cocoon.components.notification.Notifying; import org.apache.cocoon.components.notification.NotifyingBuilder; import org.apache.cocoon.components.treeprocessor.InvokeContext; import org.apache.cocoon.components.treeprocessor.ProcessingNode; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.environment.ObjectModelHelper; /** * Helps to call error handlers from PipelineNode and PipelinesNode. * * @author <a href="mailto:[EMAIL PROTECTED]">Jürgen Seitz</a> * @author <a href="mailto:[EMAIL PROTECTED]">Björn Lütkemeier</a> * @version CVS $Id: ErrorHandlerHelper.java,v 1.1 2003/04/11 13:14:51 cziegeler Exp $ */ public class ErrorHandlerHelper extends AbstractLogEnabled implements Composable { private ComponentManager manager; /** * The component manager is used to create notifying builders. */ public void compose(ComponentManager manager) { this.manager = manager; } public boolean invokeErrorHandler(ProcessingNode node, Exception ex, Environment env) throws Exception { Map objectModel = env.getObjectModel(); InvokeContext errorContext = null; boolean nodeSuccesfull = false; try { if (objectModel.get(Constants.NOTIFYING_OBJECT) == null) { // error has not been processed by another handler before // Try to reset the response to avoid mixing already produced output // and error page. env.tryResetResponse(); // Create a Notifying NotifyingBuilder notifyingBuilder= (NotifyingBuilder)this.manager.lookup(NotifyingBuilder.ROLE); Notifying currentNotifying = null; try { currentNotifying = notifyingBuilder.build(this, ex); } finally { this.manager.release(notifyingBuilder); } // Add it to the object model objectModel.put(Constants.NOTIFYING_OBJECT, currentNotifying); // Also add the exception objectModel.put(ObjectModelHelper.THROWABLE_OBJECT, ex); } // Build a new context errorContext = new InvokeContext(); errorContext.enableLogging(getLogger()); errorContext.compose(this.manager); nodeSuccesfull = node.invoke(env, errorContext); } catch (Exception subEx) { getLogger().error("An exception occured in while handling errors at " + node.getLocation(), subEx); // Rethrow it : it will either be handled by the parent sitemap or by the environment (e.g. Cocoon servlet) throw subEx; } finally { if (errorContext != null) { errorContext.dispose(); } } if (nodeSuccesfull) { return true; } else { throw ex; } } } 1.2 +1 -1 cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/treeprocessor-builtins.xml Index: treeprocessor-builtins.xml =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/treeprocessor/treeprocessor-builtins.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- treeprocessor-builtins.xml 9 Mar 2003 00:09:18 -0000 1.1 +++ treeprocessor-builtins.xml 11 Apr 2003 13:14:51 -0000 1.2 @@ -148,7 +148,7 @@ <node name="components" builder="org.apache.cocoon.components.treeprocessor.sitemap.ComponentsNodeBuilder"/> <node name="pipelines" builder="org.apache.cocoon.components.treeprocessor.sitemap.PipelinesNodeBuilder"> - <allowed-children>pipeline</allowed-children> + <allowed-children>pipeline, handle-errors</allowed-children> <ignored-children>component-configurations</ignored-children> </node>