/**
 * ÀÌ ¼Ò½º´Â Spring ÇÁ·¹ÀÓ¿öÅ© ¿öÅ©ºÏ¿¡¼­ »ç¿ëÇÑ ¿¹Á¦ ¼Ò½ºÀÔ´Ï´Ù. 
 * ÀÌ ¼Ò½º´Â ¸ðµç °³¹ßÀÚµéÀÌ ÀÚÀ¯·Ó°Ô ¼öÁ¤ ¹× ¹èÆ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. 
 * ´Ü, ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î »õ·Î¿î ¾ÖÇÃ¸®ÄÉÀÌ¼ÇÀ» °³¹ßÇÒ °æ¿ì ÃâÃ³¸¦ ¸í½ÃÇØ ÁÖ½Ã¸é µË´Ï´Ù. 
 */
package net.javajigi.advice;

import net.javajigi.common.mail.ExceptionMailSender;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;

/**
 * @author ¹ÚÀç¼º(ÀÚ¹ÙÁö±â, javajigi@gmail.com)
 */
public class EmailNotificationThrowsAdvice implements ThrowsAdvice {
	protected final Log logger = LogFactory.getLog(getClass());

	private ExceptionMailSender mailSender;

	public void setMailSender(ExceptionMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void afterThrowing(RuntimeException ex) throws Throwable {
		if (logger.isDebugEnabled()) {
			logger.debug("afterThrowing() ½ÃÀÛ");
			logger.debug("Caught : " + ex.getClass().getName());
		}

		mailSender.sendMessage(ex);

		if (logger.isDebugEnabled()) {
			logger.debug("afterThrowing() Á¾·á");
		}
	}
	
	public void afterThrowing(Exception ex) throws Throwable {
		if (logger.isDebugEnabled()) {
			logger.debug("afterThrowing() ½ÃÀÛ");
			logger.debug("Caught : " + ex.getClass().getName());
		}

		if(logger.isErrorEnabled()) {
			logger.error("Checked ExceptionÀÌ ¹ß»ýÇß½À´Ï´Ù.", ex);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("afterThrowing() Á¾·á");
		}
	}	
}