KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > PageFlowExceptionHandler


1 /*
2  * Copyright 2004 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  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.internal;
19
20 import org.apache.struts.action.ExceptionHandler;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionMessage;
25 import org.apache.struts.action.ActionMessages;
26 import org.apache.struts.config.ExceptionConfig;
27 import org.apache.struts.util.ModuleException;
28 import org.apache.struts.Globals;
29 import org.apache.beehive.netui.pageflow.ExpressionMessage;
30 import org.apache.beehive.netui.pageflow.config.PageFlowExceptionConfig;
31 import org.apache.beehive.netui.util.logging.Logger;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36
37 public class PageFlowExceptionHandler
38         extends ExceptionHandler
39 {
40     private static final Logger _log = Logger.getInstance( PageFlowExceptionHandler.class );
41     
42     public ActionForward execute( Exception JavaDoc ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance,
43                                   HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
44             throws ServletException JavaDoc
45     {
46         ActionForward forward = null;
47         ActionMessage error = null;
48         String JavaDoc property = null;
49         String JavaDoc path = ae.getPath();
50
51         // Build the forward from the exception mapping if it exists or from the form input
52
forward = path != null ? new ActionForward( path ) : mapping.getInputForward();
53         
54         // Figure out the error
55
if ( ex instanceof ModuleException )
56         {
57             error = ( ( ModuleException ) ex ).getError();
58             property = ( ( ModuleException ) ex ).getProperty();
59         }
60         else
61         {
62             if ( ae instanceof PageFlowExceptionConfig )
63             {
64                 String JavaDoc expressionMessage = ( ( PageFlowExceptionConfig ) ae ).getDefaultMessage();
65                 if ( expressionMessage != null )
66                 {
67                     error = new ExpressionMessage( expressionMessage, new Object JavaDoc[]{ ex.getMessage() } );
68                 }
69             }
70             
71             if ( error == null ) error = new ActionMessage( ae.getKey(), ex.getMessage() );
72             property = ae.getKey();
73         }
74
75         if ( _log.isDebugEnabled() ) _log.debug( "Handling exception", ex );
76
77         // Store the exception
78
request.setAttribute( Globals.EXCEPTION_KEY, ex );
79         storeException( request, property, error, forward, ae.getScope() );
80
81         return forward;
82     }
83     
84     // TODO: this does nothing different than the Struts 1.2 version of the same method. Remove this when we don't
85
// support Struts 1.1 anymore (or when Struts 1.1 support goes into a legacy version).
86
protected void storeException( HttpServletRequest JavaDoc request, String JavaDoc property, ActionMessage error,
87                                    ActionForward forward, String JavaDoc scope )
88     {
89         ActionMessages errors = new ActionMessages();
90         errors.add( property, error );
91
92         if ( "request".equals( scope ) )
93         {
94             request.setAttribute( Globals.ERROR_KEY, errors );
95         }
96         else
97         {
98             request.getSession().setAttribute( Globals.ERROR_KEY, errors );
99         }
100     }
101 }
102
Popular Tags