1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util; 18 19 import com.android.tradefed.config.ConfigurationException; 20 import com.android.tradefed.config.IConfiguration; 21 import com.android.tradefed.config.Option; 22 import com.android.tradefed.config.Option.Importance; 23 import com.android.tradefed.config.OptionClass; 24 import com.android.tradefed.log.LogUtil.CLog; 25 import com.android.tradefed.util.IEmail.Message; 26 27 import java.io.IOException; 28 29 /** 30 * A email sender utility that allows the following configuration: 31 * sent interval,initial burst size, recipients and the total number messages. 32 */ 33 @OptionClass(alias = "emailer") 34 public class BulkEmailer { 35 @Option(name = "total-emails", description = "total number of emails to send", 36 importance = Importance.IF_UNSET) 37 private int mEmails = 0; 38 39 @Option(name = "recipients", description = "a comma separate list of recipient email addresses", 40 importance = Importance.IF_UNSET) 41 private String mRecipients = null; 42 43 @Option(name = "send-interval", description = "email send interval in milliseconds", 44 importance = Importance.IF_UNSET) 45 private int mInterval = 0; 46 47 @Option(name = "initial-burst", description = "initial burst of email to send", 48 importance = Importance.IF_UNSET) 49 private int mInitialBurst = 0; 50 51 @Option(name = "sender", description = "the sender email.", importance = Importance.NEVER) 52 private String mSender = "android-power-lab-external@google.com"; 53 54 private static final String SUBJECT = "No emails to send"; 55 private static final String MESSAGE = "This is a test message!"; 56 sendEmailsBg()57 public void sendEmailsBg() { 58 new Thread(new Runnable() { 59 @Override 60 public void run() { 61 sendEmails(); 62 } 63 }).start(); 64 } 65 sendEmails()66 public void sendEmails() { 67 if (mEmails == 0) { 68 CLog.d("No emails to send."); 69 return; 70 } 71 72 Message msg = new Message(); 73 msg.setTos(mRecipients.split(",")); 74 msg.setBody(MESSAGE); 75 msg.setSender(mSender); 76 77 Email email = new Email(); 78 try { 79 CLog.d("Sending initial burst of %d emails\n", mInitialBurst); 80 for (int i = 1; i <= mInitialBurst; i++){ 81 msg.setSubject(SUBJECT + i); 82 email.send(msg); 83 Thread.sleep(2000); 84 } 85 for (int i = mInitialBurst; i <= mEmails; i++){ 86 msg.setSubject(SUBJECT + i); 87 email.send(msg); 88 CLog.i("Sending email %d/%d", i, mEmails); 89 Thread.sleep(mInterval); 90 } 91 } catch (IOException iox) { 92 CLog.e("exception sending email"); 93 CLog.e(iox); 94 } catch (InterruptedException ix) { 95 CLog.e("sleep interrupted"); 96 CLog.e(ix); 97 } 98 } 99 100 /** 101 * Helper method to load BulkMailer from config. The config must include the following tag 102 * 103 * <pre> 104 * <code><object type="emailer" class="com.android.tradefed.util.BulkEmailer"/></code> 105 * </pre> 106 * 107 * @param config the config 108 * @return an instance of BulkEmailer 109 * @throws ConfigurationException 110 */ loadMailer(IConfiguration config)111 public static BulkEmailer loadMailer(IConfiguration config) throws ConfigurationException { 112 Object o = config.getConfigurationObject("emailer"); 113 if (o instanceof BulkEmailer) { 114 return (BulkEmailer) o; 115 } 116 throw new ConfigurationException("Missing or invalid emailer definition in config"); 117 } 118 } 119