Java tutorial
/* * Copyright (c) 2007-2008 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.struts.action; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.PageContext; import jp.terasoluna.fw.exception.SystemException; import jp.terasoluna.fw.util.ExceptionUtil; import jp.terasoluna.fw.web.RequestUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.Globals; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.config.ExceptionConfig; import org.apache.struts.util.MessageResources; /** * SystemException?pO??nh?B * * <p> * VXeO??O?oG?[J?s?B<br> * ANV?sVXeO??A * G?[??O?o??AYANV}bsO` * VXeG?[J?B<br> * {@\p?AStruts?t@C(struts-config.xml) * O??[oO?AANVxOnhNXw?B<br> * O??[oO?AANVxO????A * StrutsdlANVxOD??B<br> * uSystemExceptionw???AG?[?bZ?[W??s?B * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>?Oxw</legend> * v?peB<strong><u>logLevel</u></strong>L6?Ox * w\?B * <p> * <ol> * <li>trace</li> * <li>debug</li> * <li>info</li> * <li>warn</li> * <li>error</li> * <li>fatal</li> * </ol> * </p> * ?w???AftHgerror?B<br> * ?logLevelw???A<exception>^OclassName? * <strong><u>ExceptionConfigEx</u></strong>w?B * </fieldset> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>O??[oOnhO?</legend> * * Struts?t@C(struts-config.xml)L?q?B * <pre><code> * <struts-config> * ?c * <global-exceptions> * <exception key="some.key" * path="/system-error" * type="jp.terasoluna.fw.exception.SystemException" * className="jp.terasoluna.fw.web.struts.action.ExceptionConfigEx" * handler="jp.terasoluna.fw.web.struts.action.SystemExceptionHandler"> * <set-property property="module" value="/exp"/> * <set-property property="logLevel" value="fatal"/> * </exception> * </global-exceptions> * ?c * <struts-config> * </code></pre> * </fieldset> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>ANVxOnhO?</legend> * * Struts?t@C(struts-config.xml)L?q?B * <pre><code> * <struts-config> * ?c * <action path="/start" * type="jp.terasoluna.sample.xxx.SampleAction" * name="_sampleForm" * scope="session"> * <exception key="some.key" * type="jp.terasoluna.fw.exception.SystemException" * className="jp.terasoluna.fw.web.struts.action.ExceptionConfigEx" * handler="jp.terasoluna.fw.web.struts.action.SystemExceptionHandler" * path="/sub-forward.do"> * <set-property property="module" value="/sub"/> * </exception> * <forward name="success" path="/system-error"/> * </action> * ?c * <struts-config> * </code></pre> * </fieldset> * </p> * * <p> * ?A<exception>vfpath?J?pXw * ???AANV}bsOinput? * ]?\?[X?B * </p> * * <p> * ?OCX^X?Areuqest<strong>PageContext.EXCEPTION</strong>L?[i[?B * J?JSP?AIuWFNg exception ?B * </p> * * @see jp.terasoluna.fw.exception.SystemException * @see jp.terasoluna.fw.web.struts.action.ExceptionConfigEx * @see jp.terasoluna.fw.web.struts.action.DefaultExceptionHandler * @see javax.servlet.jsp.PageContext * */ public class SystemExceptionHandler extends DefaultExceptionHandler { /** * ?ONX?B */ @SuppressWarnings("unused") private static Log log = LogFactory.getLog(SystemExceptionHandler.class); /** * SystemExceptionOnhGg|Cg?B * * @param ex O * @param eConfig ORtBO * @param mapping ANV}bsO * @param formInstance ANVtH?[ * @param request HTTPNGXg * @param response HTTPX|X * * @return J? * * @throws ServletException T?[ubgO * * @see jp.terasoluna.fw.web.struts.action.DefaultExceptionHandler#execute( * java.lang.Exception, * org.apache.struts.config.ExceptionConfig, * org.apache.struts.action.ActionMapping, * org.apache.struts.action.ActionForm, * javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse * ) */ @Override public ActionForward execute(Exception ex, ExceptionConfig eConfig, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException { // ?ytH??[h???z // pathtH??[h?w???A // ANV}bsOinput?ftHg?B String path = null; if (eConfig.getPath() != null) { path = eConfig.getPath(); } else { path = mapping.getInput(); } ActionForward forward = new ActionForward(path); String logLevel = null; // ?yJ???z if (eConfig instanceof ExceptionConfigEx) { //J?W?[??AW?[? forward.setModule(((ExceptionConfigEx) eConfig).getModule()); // ?Ox logLevel = ((ExceptionConfigEx) eConfig).getLogLevel(); } // ?ySystemException???AG?[L?[G?[?bZ?[Wu?s?z if (ex instanceof SystemException) { SystemException se = (SystemException) ex; // ?yNGXg?bZ?[W\?[X?B?z MessageResources resources = null; // XR?[v?bZ?[W\?[X?ohL?[?B String bundle = eConfig.getBundle(); if (bundle == null) { // struts-config.xmlmessage-resources // bundle?w???A // ftHgohL?[? bundle = Globals.MESSAGES_KEY; } // NGXg??B resources = (MessageResources) request.getAttribute(bundle); if (resources == null) { // NGXg?AvP?[V??B resources = (MessageResources) RequestUtil.getServletContext(request).getAttribute(bundle); } // ?yG?[L?[G?[?bZ?[Wu?s?z // SystemExceptionG?[L?[G?[?bZ?[Wu?B String message = null; if (resources == null) { // \?[X??G?[L?[?bZ?[W message = se.getErrorCode(); } else { message = getErrorMessage(request, se, resources); } se.setMessage(message); // ?y\pActionMessage??z String key = eConfig.getKey(); ActionMessage error = null; if (resources != null) { // G?[?bZ?[Wu String[] options = se.getOptions(); if (options != null && options.length > 0) { error = new ActionMessage(key, options); } else { error = new ActionMessage(key); } } else { // ?bZ?[W\?[X??G?[L?[?bZ?[W error = new ActionMessage(key, false); } super.storeException(request, key, error, forward, eConfig.getScope()); // O?bZ?[W?AX^bNg?[X // ZbVnbVl?O?o String sessionHash = RequestUtil.getSessionHash(request); logException(logLevel, "sessionHash = " + sessionHash); logException(logLevel, ExceptionUtil.getStackTrace(se)); // ?yu?SystemException??z // VXeOJSPG?[y?[W exception // request ? request.setAttribute(PageContext.EXCEPTION, se); } // VXeG?[?AANV}bsO?J?B return forward; } /** * ?bZ?[W\?[X?bZ?[WL?[w * G?[?bZ?[W?B * * @param req HTTPNGXg * @param se SystemException * @param resources ?bZ?[W\?[X * * @return G?[?bZ?[W */ private String getErrorMessage(HttpServletRequest req, SystemException se, MessageResources resources) { String errorCode = se.getErrorCode(); // ?bZ?[WL?[w?Anullp?B if (errorCode == null) { return null; } String[] options = se.getOptions(); if (options == null) { return resources.getMessage(req.getLocale(), errorCode); } return resources.getMessage(req.getLocale(), errorCode, options); } /** * nh?K?[?B * * @return ?K?[ */ @Override protected Log getLogger() { return log; } }