com.clustercontrol.notify.util.SendMail.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.notify.util.SendMail.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.
    
 */

package com.clustercontrol.notify.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.bean.PriorityConstant;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.InvalidRole;
import com.clustercontrol.fault.MailTemplateNotFound;
import com.clustercontrol.fault.NotifyNotFound;
import com.clustercontrol.maintenance.HinemosPropertyTypeConstant;
import com.clustercontrol.maintenance.util.HinemosPropertyUtil;
import com.clustercontrol.notify.bean.NotifyRequestMessage;
import com.clustercontrol.notify.bean.OutputBasicInfo;
import com.clustercontrol.notify.mail.model.MailTemplateInfo;
import com.clustercontrol.notify.mail.session.MailTemplateControllerBean;
import com.clustercontrol.notify.model.NotifyMailInfo;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.HinemosTime;
import com.clustercontrol.util.MessageConstant;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.StringBinder;
import com.clustercontrol.util.apllog.AplLogger;
import com.sun.mail.smtp.SMTPAddressFailedException;

/**
 * ??<BR>
 *
 * @version 3.0.0
 * @since 3.0.0
 */
public class SendMail implements Notifier {

    /** ? */
    private static Log m_log = LogFactory.getLog(SendMail.class);

    /**  */
    private static final String SUBJECT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**  */
    public static final String _charsetAddressDefault = "UTF-8";

    /** ?? */
    public static final String _charsetSubjectDefault = "UTF-8";

    /**` */
    public static final String _charsetContentDefault = "UTF-8";

    /**
     * ?????
     *
     * @param outputInfo 
     */
    @Override
    public synchronized void notify(NotifyRequestMessage message) {

        sendMail(message.getOutputInfo(), message.getNotifyId());
    }

    /**
     * ?????
     *
     */
    private void sendMail(OutputBasicInfo outputInfo, String notifyId) {

        if (m_log.isDebugEnabled()) {
            m_log.debug("sendMail() " + outputInfo);
        }

        try {
            NotifyMailInfo mailInfo = QueryUtil.getNotifyMailInfoPK(notifyId);

            // ???
            String subject = getSubject(outputInfo, mailInfo);

            // ?
            String content = getContent(outputInfo, mailInfo);

            /**
             * ?
             */
            String address = null;
            switch (outputInfo.getPriority()) {
            case PriorityConstant.TYPE_INFO:
                address = mailInfo.getInfoMailAddress();
                break;
            case PriorityConstant.TYPE_WARNING:
                address = mailInfo.getWarnMailAddress();
                break;
            case PriorityConstant.TYPE_CRITICAL:
                address = mailInfo.getCriticalMailAddress();
                break;
            case PriorityConstant.TYPE_UNKNOWN:
                address = mailInfo.getUnknownMailAddress();
                break;
            default:
                break;
            }
            if (address == null) {
                m_log.info("address is null");
                return;
            }
            if (address.length() == 0) {
                m_log.info("address.length()==0");
                return;
            }

            String changeAddress = null;
            try {
                Map<String, String> param = NotifyUtil.createParameter(outputInfo);
                StringBinder binder = new StringBinder(param);
                changeAddress = binder.bindParam(address);
            } catch (Exception e) {
                m_log.warn("sendMail() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                changeAddress = address;
            }
            StringTokenizer t = new StringTokenizer(changeAddress, ";");
            ArrayList<String> toAddressList = new ArrayList<String>();
            ArrayList<String> ccAddressList = new ArrayList<String>();
            ArrayList<String> bccAddressList = new ArrayList<String>();
            String separator = ":";
            String ccPrefix = "CC" + separator;
            String bccPrefix = "BCC" + separator;
            while (t.hasMoreTokens()) {
                String addr = t.nextToken();
                if (addr.startsWith(ccPrefix)) {
                    ccAddressList.add(addr.substring(ccPrefix.length()));
                } else if (addr.startsWith(bccPrefix)) {
                    bccAddressList.add(addr.substring(bccPrefix.length()));
                } else {
                    toAddressList.add(addr);
                }
            }
            String[] toAddress = toAddressList.toArray(new String[0]);

            if (toAddress == null || toAddress.length <= 0) {
                m_log.debug("sendMail() : mail address is empty");
                return;
            }

            try {
                this.sendMail(toAddress, ccAddressList.toArray(new String[0]),
                        bccAddressList.toArray(new String[0]), subject, content);
            } catch (AuthenticationFailedException e) {
                String detailMsg = "cannot connect to the mail server due to an Authentication Failure";
                m_log.warn("sendMail() " + e.getMessage() + " : " + detailMsg + " : " + e.getClass().getSimpleName()
                        + ", " + e.getMessage());
                internalErrorNotify(PriorityConstant.TYPE_CRITICAL, notifyId,
                        MessageConstant.MESSAGE_SYS_007_NOTIFY, detailMsg);
            } catch (SMTPAddressFailedException e) {
                String detailMsg = e.getMessage() + "(SMTPAddressFailedException)";
                m_log.warn("sendMail() " + e.getMessage() + " : " + detailMsg + " : " + e.getClass().getSimpleName()
                        + ", " + e.getMessage());
                internalErrorNotify(PriorityConstant.TYPE_CRITICAL, notifyId,
                        MessageConstant.MESSAGE_SYS_007_NOTIFY, detailMsg);
            } catch (MessagingException e) {
                String detailMsg = e.getCause() != null ? e.getMessage() + " Cause : " + e.getCause().getMessage()
                        : e.getMessage();
                m_log.warn("sendMail() " + e.getMessage() + " : " + detailMsg + " : " + e.getClass().getSimpleName()
                        + ", " + e.getMessage());
                internalErrorNotify(PriorityConstant.TYPE_CRITICAL, notifyId,
                        MessageConstant.MESSAGE_SYS_007_NOTIFY, detailMsg);
            } catch (UnsupportedEncodingException e) {
                String detailMsg = e.getCause() != null ? e.getMessage() + " Cause : " + e.getCause().getMessage()
                        : e.getMessage();
                m_log.warn("sendMail() " + e.getMessage() + " : " + detailMsg + detailMsg + " : "
                        + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                internalErrorNotify(PriorityConstant.TYPE_CRITICAL, notifyId,
                        MessageConstant.MESSAGE_SYS_007_NOTIFY, detailMsg);
            }
        } catch (RuntimeException | NotifyNotFound e1) {
            String detailMsg = e1.getCause() != null ? e1.getMessage() + " Cause : " + e1.getCause().getMessage()
                    : e1.getMessage();
            m_log.warn("sendMail() " + e1.getMessage() + " : " + detailMsg + detailMsg + " : "
                    + e1.getClass().getSimpleName() + ", " + e1.getMessage(), e1);
            internalErrorNotify(PriorityConstant.TYPE_CRITICAL, notifyId, MessageConstant.MESSAGE_SYS_007_NOTIFY,
                    detailMsg);
        }
    }

    /**
     * ????
     *
     * <p>
     * ???????
     * <p>
     * <ul>
     * <li></li>
     * <li>??</li>
     * <li>??</li>
     * <li>????</li>
     * <li>?</li>
     * </ul>
     *
     * @param addressTo
     *            ?
     * @param source
     *            
     * @return ???????<code> true </code>
     * @throws MessagingException
     * @throws NamingException
     * @throws UnsupportedEncodingException
     */
    public void sendMail(String[] toAddressStr, String subject, String content)
            throws MessagingException, UnsupportedEncodingException {
        sendMail(toAddressStr, null, null, subject, content);
    }

    public void sendMail(String[] toAddressStr, String[] ccAddressStr, String[] bccAddressStr, String subject,
            String content) throws MessagingException, UnsupportedEncodingException {

        if (toAddressStr == null || toAddressStr.length <= 0) {
            // ??
            return;
        }

        /*
         *  https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
         */
        Properties _properties = new Properties();
        _properties.setProperty("mail.debug",
                Boolean.toString(HinemosPropertyUtil.getHinemosPropertyBool("mail.debug", false)));
        _properties.setProperty("mail.store.protocol",
                HinemosPropertyUtil.getHinemosPropertyStr("mail.store.protocol", "pop3"));
        String protocol = HinemosPropertyUtil.getHinemosPropertyStr("mail.transport.protocol", "smtp");
        _properties.setProperty("mail.transport.protocol", protocol);
        _properties.put("mail.smtp.socketFactory", javax.net.SocketFactory.getDefault());
        _properties.put("mail.smtp.ssl.socketFactory", javax.net.ssl.SSLSocketFactory.getDefault());

        setProperties(_properties, "mail." + protocol + ".user", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".host", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".port", HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".connectiontimeout",
                HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".timeout", HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".writetimeout", HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".from", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".localhost", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".localaddress", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".localport", HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".ehlo", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth.mechanisms",
                HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".auth.login.disable",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth.plain.disable",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth.digest-md5.disable",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth.ntlm.disable",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".auth.ntlm.domain",
                HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".auth.ntlm.flags",
                HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".submitter", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".dsn.notify", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".dsn.ret", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".allow8bitmime", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".sendpartial", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".sasl.enable", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".sasl.mechanisms",
                HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".sasl.authorizationid",
                HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".sasl.realm", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".sasl.usecanonicalhostname",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".quitwait", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".reportsuccess", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".socketFactory.class",
                HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".socketFactory.fallback",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".socketFactory.port",
                HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail." + protocol + ".starttls.enable", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".starttls.required",
                HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".socks.host", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".socks.port", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".mailextension", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail." + protocol + ".userset", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail." + protocol + ".noop.strict", HinemosPropertyTypeConstant.TYPE_TRUTH);

        setProperties(_properties, "mail.smtp.ssl.enable", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail.smtp.ssl.checkserveridentity", HinemosPropertyTypeConstant.TYPE_TRUTH);
        setProperties(_properties, "mail.smtp.ssl.trust", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail.smtp.ssl.socketFactory.class", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail.smtp.ssl.socketFactory.port", HinemosPropertyTypeConstant.TYPE_NUMERIC);
        setProperties(_properties, "mail.smtp.ssl.protocols", HinemosPropertyTypeConstant.TYPE_STRING);
        setProperties(_properties, "mail.smtp.ssl.ciphersuites", HinemosPropertyTypeConstant.TYPE_STRING);

        /**
         * ?DB??
         */
        String _loginUser = HinemosPropertyUtil.getHinemosPropertyStr("mail.transport.user", "nobody");
        String _loginPassword = HinemosPropertyUtil.getHinemosPropertyStr("mail.transport.password", "password");
        String _fromAddress = HinemosPropertyUtil.getHinemosPropertyStr("mail.from.address", "admin@hinemos.com");
        String _fromPersonalName = HinemosPropertyUtil.getHinemosPropertyStr("mail.from.personal.name",
                "Hinemos Admin");
        _fromPersonalName = convertNativeToAscii(_fromPersonalName);

        String _replyToAddress = HinemosPropertyUtil.getHinemosPropertyStr("mail.reply.to.address",
                "admin@hinemos.com");
        String _replyToPersonalName = HinemosPropertyUtil.getHinemosPropertyStr("mail.reply.personal.name",
                "Hinemos Admin");
        _replyToPersonalName = convertNativeToAscii(_replyToPersonalName);

        String _errorsToAddress = HinemosPropertyUtil.getHinemosPropertyStr("mail.errors.to.address",
                "admin@hinemos.com");

        int _transportTries = HinemosPropertyUtil.getHinemosPropertyNum("mail.transport.tries", Long.valueOf(1))
                .intValue();
        int _transportTriesInterval = HinemosPropertyUtil
                .getHinemosPropertyNum("mail.transport.tries.interval", Long.valueOf(10000)).intValue();

        String _charsetAddress = HinemosPropertyUtil.getHinemosPropertyStr("mail.charset.address",
                _charsetAddressDefault);
        String _charsetSubject = HinemosPropertyUtil.getHinemosPropertyStr("mail.charset.subject",
                _charsetSubjectDefault);
        String _charsetContent = HinemosPropertyUtil.getHinemosPropertyStr("mail.charset.content",
                _charsetContentDefault);

        m_log.debug("initialized mail sender : from_address = " + _fromAddress + ", From = " + _fromPersonalName
                + " <" + _replyToAddress + ">" + ", Reply-To = " + _replyToPersonalName + " <" + _replyToAddress
                + ">" + ", Errors-To = " + _errorsToAddress + ", tries = " + _transportTries + ", tries-interval = "
                + _transportTriesInterval + ", Charset [address:subject:content] = [" + _charsetAddress + ":"
                + _charsetSubject + ":" + _charsetContent + "]");

        // JavaMail Session
        Session session = Session.getInstance(_properties);

        Message mineMsg = new MimeMessage(session);

        // ?????
        if (_fromAddress != null && _fromPersonalName != null) {
            mineMsg.setFrom(new InternetAddress(_fromAddress, _fromPersonalName, _charsetAddress));
        } else if (_fromAddress != null && _fromPersonalName == null) {
            mineMsg.setFrom(new InternetAddress(_fromAddress));
        }
        // REPLY-TO
        if (_replyToAddress != null && _replyToPersonalName != null) {
            InternetAddress reply[] = {
                    new InternetAddress(_replyToAddress, _replyToPersonalName, _charsetAddress) };
            mineMsg.setReplyTo(reply);
            mineMsg.reply(true);
        } else if (_replyToAddress != null && _replyToPersonalName == null) {
            InternetAddress reply[] = { new InternetAddress(_replyToAddress) };
            mineMsg.setReplyTo(reply);
            mineMsg.reply(true);
        }

        // ERRORS-TO
        if (_errorsToAddress != null) {
            mineMsg.setHeader("Errors-To", _errorsToAddress);
        }

        // ?
        // TO
        InternetAddress[] toAddress = this.getAddress(toAddressStr);
        if (toAddress != null && toAddress.length > 0) {
            mineMsg.setRecipients(javax.mail.Message.RecipientType.TO, toAddress);
        } else {
            return; // TO?
        }
        // CC
        if (ccAddressStr != null) {
            InternetAddress[] ccAddress = this.getAddress(ccAddressStr);
            if (ccAddress != null && ccAddress.length > 0) {
                mineMsg.setRecipients(javax.mail.Message.RecipientType.CC, ccAddress);
            }
        }
        // BCC
        if (bccAddressStr != null) {
            InternetAddress[] bccAddress = this.getAddress(bccAddressStr);
            if (bccAddress != null && bccAddress.length > 0) {
                mineMsg.setRecipients(javax.mail.Message.RecipientType.BCC, bccAddress);
            }
        }
        String message = "TO=" + Arrays.asList(toAddressStr);

        if (ccAddressStr != null) {
            message += ", CC=" + Arrays.asList(ccAddressStr);
        }
        if (bccAddressStr != null) {
            message += ", BCC=" + Arrays.asList(bccAddressStr);
        }
        m_log.debug(message);

        // ???
        mineMsg.setSubject(MimeUtility.encodeText(subject, _charsetSubject, "B"));

        // ?
        mineMsg.setContent(content, "text/plain; charset=" + _charsetContent);

        // ?
        mineMsg.setSentDate(HinemosTime.getDateInstance());

        // ???true??????
        for (int i = 0; i < _transportTries; i++) {
            Transport transport = null;
            try {
                // ?
                transport = session.getTransport();
                boolean flag = HinemosPropertyUtil.getHinemosPropertyBool("mail." + protocol + ".auth", false);
                if (flag) {
                    transport.connect(_loginUser, _loginPassword);
                } else {
                    transport.connect();
                }
                transport.sendMessage(mineMsg, mineMsg.getAllRecipients());
                break;
            } catch (AuthenticationFailedException e) {
                throw e;
            } catch (SMTPAddressFailedException e) {
                throw e;
            } catch (MessagingException me) {
                //_transportTries?sleep??? 
                if (i < (_transportTries - 1)) {
                    m_log.info("sendMail() : retry sendmail. " + me.getMessage());
                    try {
                        Thread.sleep(_transportTriesInterval);
                    } catch (InterruptedException e) {
                    }
                    //_transportTries??INTERNAL????Exceptionthrow 
                } else {
                    throw me;
                }
            } finally {
                if (transport != null) {
                    transport.close();
                }
            }
        }
    }

    private String convertNativeToAscii(String nativeStr) {
        if (HinemosPropertyUtil.getHinemosPropertyBool("mail.native.to.ascii", false)) {
            final CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder();
            final StringBuilder asciiStr = new StringBuilder();
            for (final Character character : nativeStr.toCharArray()) {
                if (asciiEncoder.canEncode(character)) {
                    asciiStr.append(character);
                } else {
                    asciiStr.append("\\u");
                    asciiStr.append(Integer.toHexString(0x10000 | character).substring(1));
                }
            }

            return asciiStr.toString();
        } else {
            return nativeStr;
        }
    }

    /**
     * ?????<code> InternetAddress </code>???
     *
     * @param addressList
     *            ???
     * @return <code> InternetAddress </code>??
     */
    private InternetAddress[] getAddress(String[] addressList) {
        InternetAddress toAddress[] = null;
        Vector<InternetAddress> list = new Vector<InternetAddress>();
        if (addressList != null) {
            for (String address : addressList) {
                try {
                    list.add(new InternetAddress(address));
                } catch (AddressException e) {
                    m_log.info("getAddress() : " + e.getClass().getSimpleName() + ", " + address + ", "
                            + e.getMessage());
                }
            }
            if (list.size() > 0) {
                toAddress = new InternetAddress[list.size()];
                list.copyInto(toAddress);
            }
        }
        return toAddress;
    }

    /**
     * ?????
     *
     * @param source
     *            
     * @param mailInfo
     *            
     * @return ??
     */
    public String getSubject(OutputBasicInfo source, NotifyMailInfo mailInfo) {

        String subject = null;
        try {
            if (mailInfo != null && mailInfo.getMailTemplateInfoEntity() != null
                    && mailInfo.getMailTemplateInfoEntity().getMailTemplateId() != null) {
                MailTemplateInfo templateData = new MailTemplateControllerBean()
                        .getMailTemplateInfo(mailInfo.getMailTemplateInfoEntity().getMailTemplateId());
                Map<String, String> param = NotifyUtil.createParameter(source, mailInfo.getNotifyInfoEntity());
                StringBinder binder = new StringBinder(param);
                subject = binder.bindParam(templateData.getSubject());
            } else {
                Locale locale = NotifyUtil.getNotifyLocale();
                subject = Messages.getString("MAIL_SUBJECT", locale) + "("
                        + Messages.getString(PriorityConstant.typeToMessageCode(source.getPriority()), locale)
                        + ")";
            }
        } catch (Exception e) {
            m_log.warn("getSubject() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            // ?
            return "Hinemos Notification";
        }

        return subject;
    }

    /**
     * ???
     *
     * @param source
     *            
     * @param mailInfo
     *            
     * @return 
     */
    public String getContent(OutputBasicInfo source, NotifyMailInfo mailInfo) {

        StringBuffer buf = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat(SUBJECT_DATE_FORMAT);
        sdf.setTimeZone(HinemosTime.getTimeZone());

        try {
            if (mailInfo != null && mailInfo.getMailTemplateInfoEntity() != null
                    && mailInfo.getMailTemplateInfoEntity().getMailTemplateId() != null) {
                MailTemplateInfo mailData = new MailTemplateControllerBean()
                        .getMailTemplateInfo(mailInfo.getMailTemplateInfoEntity().getMailTemplateId());
                Map<String, String> param = NotifyUtil.createParameter(source, mailInfo.getNotifyInfoEntity());
                StringBinder binder = new StringBinder(param);
                buf.append(binder.bindParam(mailData.getBody() + "\n"));
            } else {

                Locale locale = NotifyUtil.getNotifyLocale();

                buf.append(Messages.getString("GENERATION_TIME", locale) + " : "
                        + sdf.format(source.getGenerationDate()) + "\n");
                buf.append(Messages.getString("APPLICATION", locale) + " : "
                        + HinemosMessage.replace(source.getApplication(), locale) + "\n");
                buf.append(Messages.getString("PRIORITY", locale) + " : "
                        + Messages.getString(PriorityConstant.typeToMessageCode(source.getPriority()), locale)
                        + "\n");
                buf.append(Messages.getString("MESSAGE", locale) + " : "
                        + HinemosMessage.replace(source.getMessage(), locale) + "\n");
                buf.append(Messages.getString("SCOPE", locale) + " : "
                        + HinemosMessage.replace(source.getScopeText(), locale) + "\n");
            }
        } catch (MailTemplateNotFound | InvalidRole | HinemosUnknown | RuntimeException e) {
            m_log.warn("getContent() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            // ?
            return "An error occurred creating message.";
        }

        // LF?CRLF??
        // ??JavaMail??????????????????
        // ??????
        String ret = buf.toString().replaceAll("\r\n", "\n").replaceAll("\n", "\r\n");

        return ret;
    }

    /**
     * ????
     */
    @Override
    public void internalErrorNotify(int priority, String notifyId, MessageConstant msgCode, String detailMsg) {
        String[] args = { notifyId };
        // 
        AplLogger.put(priority, HinemosModuleConstant.PLATFORM_NOTIFY, msgCode, args, detailMsg);
    }

    private void setProperties(Properties prop, String key, int type) {
        switch (type) {
        case HinemosPropertyTypeConstant.TYPE_STRING:
            String strVal = HinemosPropertyUtil.getHinemosPropertyStr(key, null);
            if (strVal != null) {
                prop.setProperty(key, strVal);
            }
            break;
        case HinemosPropertyTypeConstant.TYPE_NUMERIC:
            Long longVal = HinemosPropertyUtil.getHinemosPropertyNum(key, null);
            if (longVal != null) {
                prop.setProperty(key, longVal.toString());
            }
            break;
        case HinemosPropertyTypeConstant.TYPE_TRUTH:
            Boolean boolVal = HinemosPropertyUtil.getHinemosPropertyBool(key, null);
            if (boolVal != null) {
                prop.setProperty(key, boolVal.toString());
            }
            break;
        default:
            //??????
            break;
        }
    }
}