Java tutorial
package com.bbm.common.aspect; /* * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security). * * 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. */ import java.util.Locale; import javax.annotation.Resource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.dao.DataAccessException; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; import egovframework.rte.fdl.cmmn.exception.BaseException; import egovframework.rte.fdl.cmmn.exception.EgovBizException; import egovframework.rte.fdl.cmmn.exception.FdlException; import egovframework.rte.fdl.cmmn.exception.manager.ExceptionHandlerService; /** * Exception ? AOP(after-throwing) ? ? ? ? * <p><b>NOTE:</b> Exception EgovBizException, RuntimeException(DataAccessException ?), FdlException , * Exception , ?? EgovBizException, RuntimeException ? ?. * Exception ? Exception ? BaseException (: fail.common.msg) ? ?. * ? fail.common.msg Message Resource ? ? ? .</b> * * @author Byunghoon Woo * @author Judd Cho */ public class ExceptionTransfer { protected Log log = LogFactory.getLog(this.getClass()); @Resource(name = "messageSource") private MessageSource messageSource; private ExceptionHandlerService[] exceptionHandlerServices; /** * ? ANT ?. */ private final PathMatcher pm = new AntPathMatcher(); /** * ExceptionHandlerService? . * @param exceptionHandlerServices array of HandlerService */ public void setExceptionHandlerService(ExceptionHandlerService[] exceptionHandlerServices) { //this.exceptionHandlerServices = exceptionHandlerServices; // ? this.exceptionHandlerServices = new ExceptionHandlerService[exceptionHandlerServices.length]; for (int i = 0; i < exceptionHandlerServices.length; i++) { this.exceptionHandlerServices[i] = exceptionHandlerServices[i]; } if (this.exceptionHandlerServices != null) log.debug(" count of ExceptionHandlerServices = " + exceptionHandlerServices.length); } /** * ExceptionHandlerService? . * @return int ExceptionHandlerService */ public int countOfTheExceptionHandlerService() { return (exceptionHandlerServices != null) ? exceptionHandlerServices.length : 0; } /** * ? Exception ? ? ?? ?? ? . * @param thisJoinPoint joinPoint ? * @param exception ? Exception */ public void transfer(JoinPoint thisJoinPoint, Exception exception) throws Exception { log.debug("execute ExceptionTransfer.transfer "); Class clazz = thisJoinPoint.getTarget().getClass(); Signature signature = thisJoinPoint.getSignature(); Locale locale = LocaleContextHolder.getLocale(); /** * BizException ? ?? ? ?. * Exception ?? ? ?? Exception? ? ? ?. * ? . * ? ?? Handler ? ?. */ String servicename = ""; // String errorCode = ""; // ? String errorMessage = ""; // ? String classname = ""; // ?? int servicepos = clazz.getCanonicalName().lastIndexOf("."); // .? if (servicepos > 0) { String tempStr = clazz.getCanonicalName().substring(servicepos + 1); servicepos = tempStr.lastIndexOf("Impl"); // Impl? servicename = tempStr.substring(0, servicepos); } else { servicename = clazz.getCanonicalName(); } classname = exception.getClass().getName(); //EgovBizException ? ? if (exception instanceof EgovBizException) { log.debug("Exception case :: EgovBizException "); EgovBizException be = (EgovBizException) exception; getLog(clazz).error(be.getMessage(), be.getCause()); // Exception Handler ? ?? Package Exception . (runtime ? ExceptionHandlerService ) processHandling(clazz, signature.getName(), exception, pm, exceptionHandlerServices); throw be; //RuntimeException ? ? ? DataAccessException ? ?? throw . } else if (exception instanceof RuntimeException) { log.debug("RuntimeException case :: RuntimeException "); RuntimeException be = (RuntimeException) exception; getLog(clazz).error(be.getMessage(), be.getCause()); // Exception Handler ? ?? Package Exception . processHandling(clazz, signature.getName(), exception, pm, exceptionHandlerServices); if (be instanceof DataAccessException) { /* log.debug("RuntimeException case :: DataAccessException "); DataAccessException sqlEx = (DataAccessException) be; throw sqlEx; */ log.debug("RuntimeException case :: DataAccessException "); DataAccessException dataEx = (DataAccessException) be; Throwable t = dataEx.getRootCause(); String exceptionname = t.getClass().getName(); if (exceptionname.equals("java.sql.SQLException")) { java.sql.SQLException sqlException = (java.sql.SQLException) t; errorCode = String.valueOf(sqlException.getErrorCode()); errorMessage = sqlException.getMessage(); } else if (exception instanceof org.springframework.jdbc.BadSqlGrammarException) { org.springframework.jdbc.BadSqlGrammarException sqlEx = (org.springframework.jdbc.BadSqlGrammarException) exception; errorCode = String.valueOf(sqlEx.getSQLException().getErrorCode()); errorMessage = sqlEx.getSQLException().toString(); } else if (exception instanceof org.springframework.jdbc.UncategorizedSQLException) { org.springframework.jdbc.UncategorizedSQLException sqlEx = (org.springframework.jdbc.UncategorizedSQLException) exception; errorCode = String.valueOf(sqlEx.getSQLException().getErrorCode()); errorMessage = sqlEx.getSQLException().toString(); } else if (exception instanceof org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException) { org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException sqlEx = (org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException) exception; errorCode = String.valueOf(sqlEx.getActualRowsAffected()); errorMessage = sqlEx.getMessage().toString(); } else if (exception instanceof org.springframework.jdbc.SQLWarningException) { org.springframework.jdbc.SQLWarningException sqlEx = (org.springframework.jdbc.SQLWarningException) exception; errorCode = String.valueOf(sqlEx.SQLWarning().getErrorCode()); errorMessage = sqlEx.getMessage().toString(); } else if (exception instanceof org.springframework.jdbc.CannotGetJdbcConnectionException) { org.springframework.jdbc.CannotGetJdbcConnectionException sqlEx = (org.springframework.jdbc.CannotGetJdbcConnectionException) exception; errorCode = String.valueOf(sqlEx.getMessage()); errorMessage = sqlEx.getMessage().toString(); } else if (exception instanceof org.springframework.jdbc.InvalidResultSetAccessException) { org.springframework.jdbc.InvalidResultSetAccessException sqlEx = (org.springframework.jdbc.InvalidResultSetAccessException) exception; errorCode = String.valueOf(sqlEx.getSQLException().getErrorCode()); errorMessage = sqlEx.getSQLException().toString(); } else { if (exception instanceof java.lang.reflect.InvocationTargetException) { java.lang.reflect.InvocationTargetException ce = (java.lang.reflect.InvocationTargetException) exception; errorCode = ""; errorMessage = ce.getTargetException().getMessage(); //strErrorMessage = getValue(ce.getTargetException().toString(), ""); } } // , ?, ?, , String[] messages = new String[] { "DataAccessException", errorCode, errorMessage, servicename, signature.getName(), classname }; throw processException(clazz, "fail.common.msg", messages, exception, locale); } // , ?, ?, , errorMessage = exception.getMessage(); String[] messages = new String[] { "RuntimeException", errorCode, errorMessage, servicename, signature.getName(), classname }; throw processException(clazz, "fail.common.msg", messages, exception, locale); //throw be; // ? ? Exception (: ) :: ? ?. } else if (exception instanceof FdlException) { log.debug("FdlException case :: FdlException "); FdlException fe = (FdlException) exception; getLog(clazz).error(fe.getMessage(), fe.getCause()); errorMessage = exception.getMessage(); // , ?, ?, , String[] messages = new String[] { "FdlException", errorCode, errorMessage, servicename, signature.getName(), classname }; throw processException(clazz, "fail.common.msg", messages, exception, locale); //throw fe; } else { //? ? Exception ? BaseException (: fail.common.msg) ?. //:: ? ?. log.debug("case :: Exception "); getLog(clazz).error(exception.getMessage(), exception.getCause()); errorMessage = exception.getMessage(); // , ?, ?, , String[] messages = new String[] { "Exception", errorCode, errorMessage, servicename, signature.getName(), classname }; throw processException(clazz, "fail.common.msg", messages, exception, locale); } } protected Exception processException(final Class clazz, final String msgKey, final String[] msgArgs, final Exception e, Locale locale) { return processException(clazz, msgKey, msgArgs, e, locale, null); } protected Exception processException(final Class clazz, final String msgKey, final String[] msgArgs, final Exception e, final Locale locale, ExceptionCreator exceptionCreator) { // getLog(clazz).error(messageSource.getMessage(msgKey, msgArgs, locale), e); ExceptionCreator eC = null; if (exceptionCreator == null) { eC = new ExceptionCreator() { public Exception processException(MessageSource messageSource) { return new BaseException(messageSource, msgKey, msgArgs, locale, e); } }; } if (eC != null) { return eC.processException(messageSource); } else { return e; } } protected interface ExceptionCreator { Exception processException(MessageSource messageSource); } protected Log getLog(Class clazz) { return LogFactory.getLog(clazz); } /** * ? Exception ? ? ?? ?? ? . * * @param clazz Exception ? ? * @param methodName Exception ? * @param exception ? Exception * @param pm ? PathMatcher(default : AntPathMatcher) * @param exceptionHandlerServices[] ?? ExceptionHandlerService */ protected void processHandling(Class clazz, String methodName, Exception exception, PathMatcher pm, ExceptionHandlerService[] exceptionHandlerServices) { try { for (ExceptionHandlerService ehm : exceptionHandlerServices) { if (!ehm.hasReqExpMatcher()) ehm.setReqExpMatcher(pm); ehm.setPackageName(clazz.getCanonicalName() + "." + methodName); ehm.run(exception); } } catch (Exception e) { // TODO Auto-generated catch block getLog(clazz).error(e.getMessage(), e.getCause()); } } }