KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > JManageExceptionHandler


1 /**
2  * Copyright 2004-2005 jManage.org
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
17 package org.jmanage.webui;
18
19 import org.apache.struts.action.*;
20 import org.apache.struts.config.ExceptionConfig;
21 import org.apache.struts.Globals;
22 import org.jmanage.core.services.ServiceException;
23 import org.jmanage.core.util.ErrorCodes;
24 import org.jmanage.core.util.Loggers;
25 import org.jmanage.core.util.ErrorCatalog;
26 import org.jmanage.core.management.ConnectionFailedException;
27 import org.jmanage.core.config.ApplicationConfigManager;
28 import org.jmanage.webui.util.Forwards;
29 import org.jmanage.webui.util.WebErrorCodes;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.Level JavaDoc;
36
37 /**
38  * Date: Feb 27, 2005 6:52:10 PM
39  * @author Shashank Bellary
40  */

41 public class JManageExceptionHandler extends ExceptionHandler{
42
43     private static final Logger JavaDoc logger =
44             Loggers.getLogger(JManageExceptionHandler.class);
45
46     /**
47      * Custom handler of exceptions in jManage.
48      *
49      * @param exception
50      * @param exConfig
51      * @param mapping
52      * @param form
53      * @param request
54      * @param response
55      * @return
56      * @throws ServletException
57      */

58     public ActionForward execute(Exception JavaDoc exception, ExceptionConfig exConfig,
59                                  ActionMapping mapping, ActionForm form,
60                                  HttpServletRequest JavaDoc request,
61                                  HttpServletResponse JavaDoc response)
62             throws ServletException JavaDoc {
63
64         ActionErrors errors = new ActionErrors();
65         ActionForward forward;
66
67         if(exception instanceof ServiceException){
68             errors.add(ActionErrors.GLOBAL_ERROR,
69                     new ActionError(WebErrorCodes.WEB_UI_ERROR_KEY,
70                             exception.getMessage()));
71         }else if(exception instanceof ConnectionFailedException){
72             //TODO: We need not handle this condition once all the code throwing this exception gets moved to service layer.
73
logger.log(Level.FINE, "Failed to connect", exception);
74             return mapping.findForward(Forwards.CONNECTION_FAILED);
75         }else if(exception instanceof ApplicationConfigManager.DuplicateApplicationNameException){
76             //TODO: We need not handle this exception once this exception
77
// is handled in Service layer
78
ApplicationConfigManager.DuplicateApplicationNameException ex =
79                     (ApplicationConfigManager.DuplicateApplicationNameException)exception;
80             errors.add(ActionErrors.GLOBAL_ERROR,
81                     new ActionError(WebErrorCodes.WEB_UI_ERROR_KEY,
82                             ErrorCatalog.getMessage(ErrorCodes.APPLICATION_NAME_ALREADY_EXISTS,
83                                     ex.getAppName())));
84         }else{
85             logger.log(Level.SEVERE, "unknown exception", exception);
86             errors.add(ActionErrors.GLOBAL_ERROR,
87                     new ActionError(ErrorCodes.UNKNOWN_ERROR));
88         }
89         request.setAttribute(Globals.ERROR_KEY, errors);
90         forward = mapping.getInputForward();
91         forward = forward != null && forward.getPath() != null ?
92                 forward : mapping.findForward(Forwards.FAILURE);
93         forward = forward != null ? forward : new ActionForward(Forwards.FAILURE);
94         return forward;
95     }
96 }
97
Popular Tags