KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tutorial > pagetypes > PageTypesEngine


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package tutorial.pagetypes;
16
17 import net.sf.tapestry.*;
18 import net.sf.tapestry.engine.SimpleEngine;
19 import net.sf.tapestry.util.prop.OgnlUtils;
20
21 import javax.servlet.ServletException JavaDoc;
22
23 /**
24  * Provides the ability to handle an exception in a different manner
25  */

26 public class PageTypesEngine extends SimpleEngine {
27     private static final String JavaDoc NEW_EXCEPTION_PAGE = "NewException";
28
29     protected void activateExceptionPage(IRequestCycle cycle, ResponseOutputStream output, Throwable JavaDoc cause)
30             throws ServletException JavaDoc {
31         RequestCycleException cycleE = (RequestCycleException)cause;
32         if (cycleE.getRootCause() instanceof MyException) {
33             handleNewException(cycle, output, cycleE.getRootCause());
34         } else {
35             super.activateExceptionPage(cycle, output, cause);
36         }
37     }
38
39     /**
40      * This standard exception handling block is what is found in
41      * the SimpleEngine. This is not normal. Usually, if you are overriding
42      * the Exception pae, you just have to provide a new page - not override the engine
43      * to do it. This is done only because I want to show both the standard and new
44      * exception pages in the same application.
45      */

46     protected void handleNewException(IRequestCycle cycle, ResponseOutputStream output, Throwable JavaDoc cause) throws ServletException JavaDoc {
47         try {
48             IPage exceptionPage = cycle.getPage(NEW_EXCEPTION_PAGE);
49             OgnlUtils.set("exception", getResourceResolver(), exceptionPage, cause);
50             cycle.setPage(exceptionPage);
51             renderResponse(cycle, output);
52         } catch (Throwable JavaDoc ex) {
53             // Worst case scenario. The exception page itself is broken, leaving
54
// us with no option but to write the cause to the output.
55
reportException(Tapestry.getString("AbstractEngine.unable-to-process-client-request"), cause);
56
57             // Also, write the exception thrown when redendering the exception
58
// page, so that can get fixed as well.
59
reportException(Tapestry.getString("AbstractEngine.unable-to-present-exception-page"), ex);
60             // And throw the exception.
61
throw new ServletException JavaDoc(ex.getMessage(), ex);
62         }
63     }
64 }
65
Popular Tags