From bf1e976d1f13ff251e75bb7d7e71e3485aa66825 Mon Sep 17 00:00:00 2001 From: shaofengshi Date: Fri, 28 Nov 2014 17:30:32 +0800 Subject: [PATCH 1/2] Move the mail related settings to kylin.properties --- common/pom.xml | 6 +- .../java/com/kylinolap/common/KylinConfig.java | 10 +++ .../com/kylinolap/common/util/MailService.java | 91 ++++++++++++++++++++++ examples/test_case_data/sandbox/kylin.properties | 11 ++- .../com/kylinolap/job/flow/JobFlowListener.java | 3 +- .../java/com/kylinolap/job/tools/MailService.java | 91 ---------------------- 6 files changed, 118 insertions(+), 94 deletions(-) create mode 100644 common/src/main/java/com/kylinolap/common/util/MailService.java delete mode 100644 job/src/main/java/com/kylinolap/job/tools/MailService.java diff --git a/common/pom.xml b/common/pom.xml index 654df7e..32a46c6 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -45,7 +45,11 @@ - + + org.apache.commons + commons-email + 1.1 + org.apache.hadoop hadoop-common diff --git a/common/src/main/java/com/kylinolap/common/KylinConfig.java b/common/src/main/java/com/kylinolap/common/KylinConfig.java index 2e834d5..34b348d 100644 --- a/common/src/main/java/com/kylinolap/common/KylinConfig.java +++ b/common/src/main/java/com/kylinolap/common/KylinConfig.java @@ -123,6 +123,16 @@ */ public static final String KYLIN_CONF_PROPERTIES_FILE = "kylin.properties"; + public static final String MAIL_ENABLED = "mail.enabled"; + + public static final String MAIL_HOST = "mail.host"; + + public static final String MAIL_USERNAME = "mail.username"; + + public static final String MAIL_PASSWORD = "mail.password"; + + public static final String MAIL_SENDER = "mail.sender"; + private static final Logger logger = LoggerFactory.getLogger(KylinConfig.class); // static cached instances diff --git a/common/src/main/java/com/kylinolap/common/util/MailService.java b/common/src/main/java/com/kylinolap/common/util/MailService.java new file mode 100644 index 0000000..5bb0ccb --- /dev/null +++ b/common/src/main/java/com/kylinolap/common/util/MailService.java @@ -0,0 +1,91 @@ +/* + * Copyright 2013-2014 eBay Software Foundation + * + * 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 com.kylinolap.common.util; + +import java.io.IOException; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.mail.Email; +import org.apache.commons.mail.EmailException; +import org.apache.commons.mail.HtmlEmail; + +import com.kylinolap.common.KylinConfig; + +/** + * @author xduo + * + */ +public class MailService { + + private Boolean enabled = Boolean.TRUE; + private String host; + private String username; + private String password; + private String sender; + + private static final Log logger = LogFactory.getLog(MailService.class); + + public MailService() { + this(KylinConfig.getInstanceFromEnv()); + } + + public MailService(KylinConfig config) { + enabled = "true".equalsIgnoreCase(config.getProperty(KylinConfig.MAIL_ENABLED, "true")); + host = config.getProperty(KylinConfig.MAIL_HOST, ""); + username = config.getProperty(KylinConfig.MAIL_USERNAME, ""); + password = config.getProperty(KylinConfig.MAIL_PASSWORD, ""); + sender = config.getProperty(KylinConfig.MAIL_SENDER, ""); + + if (enabled) { + assert !host.isEmpty(); + } + } + + public void sendMail(List receivers, String subject, String content) throws IOException { + + if (!enabled) { + logger.info("Email service is disabled; this mail will not be delivered: " + subject); + logger.info("To enable mail service, set 'mail.enabled=true' in kylin.properties"); + return; + } + + Email email = new HtmlEmail(); + email.setHostName(host); + if (username != null && username.trim().length() > 0) { + email.setAuthentication(username, password); + } + + email.setDebug(true); + try { + for (String receiver : receivers) { + email.addTo(receiver); + } + + email.setFrom(sender); + email.setSubject(subject); + email.setCharset("UTF-8"); + ((HtmlEmail) email).setHtmlMsg(content); + email.send(); + email.getMailSession(); + + } catch (EmailException e) { + logger.error(e); + } + } +} diff --git a/examples/test_case_data/sandbox/kylin.properties b/examples/test_case_data/sandbox/kylin.properties index 4cf4f07..0675b21 100644 --- a/examples/test_case_data/sandbox/kylin.properties +++ b/examples/test_case_data/sandbox/kylin.properties @@ -63,4 +63,13 @@ ldap.service.groupSearchBase= acl.adminRole= acl.defaultRole= ganglia.group= -ganglia.port=8664 \ No newline at end of file +ganglia.port=8664 + +## Config for mail service + +# If true, will send email notification; +mail.enabled=true +mail.host=atom.corp.ebay.com +mail.username=_kylin_ldap +mail.password= +mail.sender=DL-eBay-Kylin@corp.ebay.com \ No newline at end of file diff --git a/job/src/main/java/com/kylinolap/job/flow/JobFlowListener.java b/job/src/main/java/com/kylinolap/job/flow/JobFlowListener.java index 5c35879..db0be3d 100644 --- a/job/src/main/java/com/kylinolap/job/flow/JobFlowListener.java +++ b/job/src/main/java/com/kylinolap/job/flow/JobFlowListener.java @@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.kylinolap.cube.CubeSegmentStatusEnum; + import org.apache.commons.lang.exception.ExceptionUtils; import org.quartz.JobDataMap; import org.quartz.JobDetail; @@ -38,6 +39,7 @@ import org.slf4j.LoggerFactory; import com.kylinolap.common.KylinConfig; +import com.kylinolap.common.util.MailService; import com.kylinolap.cube.CubeInstance; import com.kylinolap.cube.CubeManager; import com.kylinolap.cube.CubeSegment; @@ -49,7 +51,6 @@ import com.kylinolap.job.constant.JobStatusEnum; import com.kylinolap.job.constant.JobStepStatusEnum; import com.kylinolap.job.engine.JobEngineConfig; -import com.kylinolap.job.tools.MailService; /** * Handle kylin job and cube change update. diff --git a/job/src/main/java/com/kylinolap/job/tools/MailService.java b/job/src/main/java/com/kylinolap/job/tools/MailService.java deleted file mode 100644 index 5735dbb..0000000 --- a/job/src/main/java/com/kylinolap/job/tools/MailService.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2013-2014 eBay Software Foundation - * - * 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 com.kylinolap.job.tools; - -import java.io.IOException; -import java.util.List; - -import org.apache.commons.mail.Email; -import org.apache.commons.mail.EmailException; -import org.apache.commons.mail.HtmlEmail; - -/** - * @author xduo - * - */ -public class MailService { - - private String host = "atom.corp.ebay.com"; - private String username = "_kylin_ldap"; - private String password; - private String sender = "DL-eBay-Kylin@corp.ebay.com"; - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getSender() { - return sender; - } - - public void setSender(String sender) { - this.sender = sender; - } - - public void sendMail(List receivers, String subject, String content) throws IOException { - - Email email = new HtmlEmail(); - email.setHostName(host); - email.setDebug(true); - try { - for (String receiver : receivers) { - email.addTo(receiver); - } - - email.setFrom(sender); - email.setSubject(subject); - email.setCharset("UTF-8"); - ((HtmlEmail) email).setHtmlMsg(content); - email.send(); - email.getMailSession(); - - System.out.println("!!"); - } catch (EmailException e) { - e.printStackTrace(); - } - } -} From 8598056970f715336cc81b47c81c63764f30f65c Mon Sep 17 00:00:00 2001 From: shaofengshi Date: Fri, 28 Nov 2014 17:51:17 +0800 Subject: [PATCH 2/2] Add a test case for MailService --- .../com/kylinolap/common/util/MailService.java | 17 +++++-- .../com/kylinolap/common/util/MailServiceTest.java | 59 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 common/src/test/java/com/kylinolap/common/util/MailServiceTest.java diff --git a/common/src/main/java/com/kylinolap/common/util/MailService.java b/common/src/main/java/com/kylinolap/common/util/MailService.java index 5bb0ccb..1d76ffe 100644 --- a/common/src/main/java/com/kylinolap/common/util/MailService.java +++ b/common/src/main/java/com/kylinolap/common/util/MailService.java @@ -57,12 +57,20 @@ public MailService(KylinConfig config) { } } - public void sendMail(List receivers, String subject, String content) throws IOException { + /** + * + * @param receivers + * @param subject + * @param content + * @return true or false indicating whether the email was delivered successfully + * @throws IOException + */ + public boolean sendMail(List receivers, String subject, String content) throws IOException { if (!enabled) { logger.info("Email service is disabled; this mail will not be delivered: " + subject); logger.info("To enable mail service, set 'mail.enabled=true' in kylin.properties"); - return; + return false; } Email email = new HtmlEmail(); @@ -71,7 +79,7 @@ public void sendMail(List receivers, String subject, String content) thr email.setAuthentication(username, password); } - email.setDebug(true); + //email.setDebug(true); try { for (String receiver : receivers) { email.addTo(receiver); @@ -86,6 +94,9 @@ public void sendMail(List receivers, String subject, String content) thr } catch (EmailException e) { logger.error(e); + return false; } + + return true; } } diff --git a/common/src/test/java/com/kylinolap/common/util/MailServiceTest.java b/common/src/test/java/com/kylinolap/common/util/MailServiceTest.java new file mode 100644 index 0000000..33a51e8 --- /dev/null +++ b/common/src/test/java/com/kylinolap/common/util/MailServiceTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2013-2014 eBay Software Foundation + * + * 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 com.kylinolap.common.util; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.kylinolap.common.KylinConfig; + +public class MailServiceTest { + + @Test + public void testSendEmail() throws IOException { + + KylinConfig config = KylinConfig.getInstanceForTest(AbstractKylinTestCase.SANDBOX_TEST_DATA); + + MailService mailservice = new MailService(config); + boolean sent = sendTestEmail(mailservice); + assert sent; + + // set mail.enabled=false, and run again, this time should be no mail delviered + config.setProperty(KylinConfig.MAIL_ENABLED, "false"); + mailservice = new MailService(config); + sent = sendTestEmail(mailservice); + assert !sent; + + } + + private boolean sendTestEmail(MailService mailservice) { + + List receivers = new ArrayList(1); + receivers.add("shaoshi@ebay.com"); + try { + return mailservice.sendMail(receivers, "A test email from Kylin", "Hello!"); + } catch (IOException e) { + e.printStackTrace(); + } + + return false; + } +}