package com.dianping.cat.system.tool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dianping.cat.Cat;
import com.dianping.cat.configuration.ServerConfigManager;
import com.dianping.mailremote.remote.MailService;
import com.dianping.sms.biz.SMSService;
import com.dianping.sms.biz.SMSType;
import com.site.lookup.annotation.Inject;

public class MailSMSImpl implements MailSMS, Initializable, LogEnabled {

	private final static int DEFAULT_EMAIL_TYPE = 15;

	private final SMSType DEFAULT_MESSAGE_TYPE = SMSType.MONITOR;

	private Logger m_logger;

	private MailService m_mailService;

	@Inject
	private ServerConfigManager m_serverConfig;

	private SMSService m_smsService;

	@Override
	public void enableLogging(Logger logger) {
		m_logger = logger;
	}

	@Override
	public void initialize() throws InitializationException {
		if (m_serverConfig.isJobMachine()) {
			try {
				ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/remoteService.xml");

				m_mailService = (MailService) ctx.getBean("mailService");
				m_smsService = (SMSService) ctx.getBean("smsService");
			} catch (BeansException e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public boolean sendEmail(String title, String content, List<String> emails) {
		boolean sendResult = false;

		if (m_serverConfig.isJobMachine()) {
			if (emails.size() > 0) {
				for (String mail : emails) {
					try {
						m_mailService.send(DEFAULT_EMAIL_TYPE, mail, title, content);
						sendResult = true;
					} catch (Exception e) {
						Cat.logError(e);
					}
				}
			} else {
				m_logger.info("CAT email has no recevers ! " + title);
			}
		}
		return sendResult;
	}

	@Override
	public boolean sendSMS(String content, List<String> phones) {
		boolean sendResult = false;

		if (m_serverConfig.isJobMachine()) {
			if (phones != null && phones.size() > 0) {
				for (String phone : phones) {
					try {
						Map<String, String> pair = new HashMap<String, String>();

						pair.put("content", content);
						m_smsService.send(DEFAULT_MESSAGE_TYPE, phone, pair);
						sendResult = true;
					} catch (Exception e) {
						Cat.logError(e);
					}
				}
			} else {
				m_logger.info("CAT sms has no recevers ! " + content);
			}
		}
		return sendResult;
	}

}

@@ -987,16 +987,52 @@
 NITOR;%0A%0A
+%09private boolean m_active = false;%0A%0A
 %09private
@@ -2385,24 +2385,112 @@
 : phones) %7B%0A
+%09%09%09%09%09m_logger.info(%22CAT sms send to ! %22 + phone + %22 %22 + content);%0A%09%09%09%09%09if (m_active) %7B%0A%09
 %09%09%09%09%09try %7B%0A%09
@@ -2484,24 +2484,25 @@
 %09%09%09%09%09%09try %7B%0A
+%09
 %09%09%09%09%09%09Map%3CSt
@@ -2550,24 +2550,25 @@
 String%3E();%0A%0A
+%09
 %09%09%09%09%09%09pair.p
@@ -2593,24 +2593,25 @@
 ent);%0A%09%09%09%09%09%09
+%09
 m_smsService
@@ -2650,32 +2650,33 @@
 e, pair);%0A%09%09%09%09%09%09
+%09
 sendResult = tru
@@ -2670,32 +2670,33 @@
 dResult = true;%0A
+%09
 %09%09%09%09%09%7D catch (Ex
@@ -2706,32 +2706,33 @@
 tion e) %7B%0A%09%09%09%09%09%09
+%09
 Cat.logError(e);
@@ -2724,32 +2724,40 @@
 at.logError(e);%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09

modify the sms recever