KickJava   Java API By Example, From Geeks To Geeks.

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


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.parameters.Parameters;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20
21 import org.apache.cocoon.ProcessingException;
22 import org.apache.cocoon.Constants;
23 import org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode;
24 import org.apache.cocoon.components.treeprocessor.InvokeContext;
25 import org.apache.cocoon.components.treeprocessor.ProcessingNode;
26 import org.apache.cocoon.environment.Environment;
27 import org.apache.commons.lang.SystemUtils;
28
29 /**
30  * Handles <map:handle-errors>
31  *
32  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
33  * @version $Id: HandleErrorsNode.java 165524 2005-05-01 16:55:08Z antonio $
34  */

35 public final class HandleErrorsNode extends AbstractParentProcessingNode {
36
37     private ProcessingNode[] children;
38     private int statusCode;
39     private boolean internal;
40     private boolean external;
41
42     /**
43      * @param statusCode Value of the type attribute: 404 (deprecated), 500 (deprecated), or -1 (no attribute present).
44      * @param scope Value of the error handler scope attribute: external, internal, always.
45      */

46     public HandleErrorsNode(int statusCode, String JavaDoc scope)
47     throws ConfigurationException {
48         this.statusCode = statusCode;
49         if ("internal".equals(scope)) {
50             this.internal = true;
51         } else if ("external".equals(scope)) {
52             this.external = true;
53         } else if ("always".equals(scope)) {
54             this.internal = true;
55             this.external = true;
56         } else {
57             throw new ConfigurationException("Unrecognized value of when attribute on <handle-errors> at " +
58                                              getLocation());
59         }
60     }
61
62     public int getStatusCode() {
63         return this.statusCode;
64     }
65
66     public boolean isInternal() {
67         return this.internal;
68     }
69
70     public boolean isExternal() {
71         return this.external;
72     }
73
74     public void setChildren(ProcessingNode[] nodes) {
75         this.children = nodes;
76     }
77
78     public final boolean invoke(Environment env, InvokeContext context)
79     throws Exception JavaDoc {
80
81         if (getLogger().isInfoEnabled()) {
82             getLogger().info("Processing handle-errors at " + getLocation());
83         }
84
85         if (statusCode == -1) {
86             // No 'type' attribute : new Cocoon 2.1 behaviour, no implicit generator
87
try {
88                 return invokeNodes(this.children, env, context);
89
90             } catch (ProcessingException e) {
91                 // Handle the various cases related to the transition from implicit generators in handle-errors to
92
// explicit ones, in order to provide meaningful messages that will ease the migration
93
if (e.getMessage().indexOf("Must set a generator before adding") != -1) {
94
95                     env.getObjectModel().remove(Constants.NOTIFYING_OBJECT);
96                     throw new ProcessingException(
97                         "Incomplete pipeline: 'handle-error' without a 'type' must include a generator, at " +
98                         getLocation() + SystemUtils.LINE_SEPARATOR +
99                         "Either add a generator (preferred) or a type='500' attribute (deprecated) on 'handle-errors'");
100                 }
101
102                 // Rethrow the exception
103
throw e;
104             }
105         } else {
106             // A 'type' attribute is present : add the implicit generator
107
context.getProcessingPipeline().setGenerator("<notifier>", "", Parameters.EMPTY_PARAMETERS, Parameters.EMPTY_PARAMETERS);
108
109             try {
110                 return invokeNodes(this.children, env, context);
111             } catch (ProcessingException e) {
112                 if (e.getMessage().indexOf("Generator already set") != -1){
113
114                     env.getObjectModel().remove(Constants.NOTIFYING_OBJECT);
115                     throw new ProcessingException(
116                             "Error: 'handle-error' with a 'type' attribute has an implicit generator, at " +
117                             getLocation() + SystemUtils.LINE_SEPARATOR +
118                             "Please remove the 'type' attribute on 'handle-error'");
119                 }
120                 // Rethrow the exception
121
throw e;
122             }
123         }
124     }
125 }
126
Popular Tags