Java tutorial
/* * 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. */ package egovframework.rte.fdl.cmmn.aspect; 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 Judd Cho (horanghi@gmail.com) * @since 2009.06.01 * @version 1.0 * @see * * <pre> * << ?(Modification Information) >> * * ? ? * ------- -------- --------------------------- * 2009.05.30 Judd Cho ? * * </pre> */ public class ExceptionTransfer { protected Log log = LogFactory.getLog(this.getClass()); @Resource(name = "messageSource") private MessageSource messageSource; private ExceptionHandlerService[] exceptionHandlerServices; /** * ? ANT ?. */ private PathMatcher pm = new AntPathMatcher(); /** * ExceptionHandlerService? . * @param exceptionHandlerServices array of HandlerService */ public void setExceptionHandlerService(ExceptionHandlerService[] exceptionHandlerServices) { this.exceptionHandlerServices = exceptionHandlerServices; 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 ? ?. */ //EgovBizException ? ? if (exception instanceof EgovBizException) { log.debug("Exception case :: EgovBizException "); EgovBizException be = (EgovBizException) exception; //wrapp ? Exception error ?? . if (be.getWrappedException() != null) { Throwable _throwable = be.getWrappedException(); getLog(clazz).error(be.getMessage(), _throwable); } else { getLog(clazz).error(be.getMessage(), be.getCause()); } // Exception Handler ? ?? Package Exception . (runtime ? ExceptionHandlerService ) processHandling(clazz, signature.getName(), be, 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; } throw be; // ? ? Exception (: ) :: ? ?. } else if (exception instanceof FdlException) { log.debug("FdlException case :: FdlException "); FdlException fe = (FdlException) exception; getLog(clazz).error(fe.getMessage(), fe.getCause()); throw fe; } else { //? ? Exception ? BaseException (: fail.common.msg) ?. //:: ? ?. log.debug("case :: Exception "); getLog(clazz).error(exception.getMessage(), exception.getCause()); throw processException(clazz, "fail.common.msg", new String[] {}, 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); } }; } return eC.processException(messageSource); } 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) { } } }