Merge "Email Utility"
diff --git a/src/com/android/tradefed/util/BulkEmailer.java b/src/com/android/tradefed/util/BulkEmailer.java
new file mode 100644
index 0000000..3b1708b
--- /dev/null
+++ b/src/com/android/tradefed/util/BulkEmailer.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * 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.android.tradefed.util;
+
+import com.android.tradefed.config.ConfigurationException;
+import com.android.tradefed.config.IConfiguration;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.config.Option.Importance;
+import com.android.tradefed.config.OptionClass;
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.util.IEmail.Message;
+
+import java.io.IOException;
+
+/**
+ * A email sender utility that allows the following configuration:
+ * sent interval,initial burst size, recipients and the total number messages.
+ */
+@OptionClass(alias = "emailer")
+public class BulkEmailer {
+ @Option(name = "total-emails", description = "total number of emails to send",
+ importance = Importance.IF_UNSET)
+ private final int mEmails = 0;
+
+ @Option(name = "recipients", description = "a comma separate list of recipient email addresses",
+ importance = Importance.IF_UNSET)
+ private final String mRecipients = null;
+
+ @Option(name = "send-interval", description = "email send interval in milliseconds",
+ importance = Importance.IF_UNSET)
+ private final int mInterval = 0;
+
+ @Option(name = "initial-burst", description = "initial burst of email to send",
+ importance = Importance.IF_UNSET)
+ private final int mInitialBurst = 0;
+
+ @Option(name = "sender", description = "the sender email.", importance = Importance.NEVER)
+ private final String mSender = "android.sync.battery.test@gmail.com";
+
+ private static final String SUBJECT = "No emails to send";
+ private static final String MESSAGE = "This is a test message!";
+
+ public void sendEmailsBg() {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ sendEmails();
+ }
+ }).start();
+ }
+
+ public void sendEmails() {
+ if (mEmails == 0) {
+ CLog.d("No emails to send.");
+ return;
+ }
+
+ Message msg = new Message();
+ msg.setTos(mRecipients.split(","));
+ msg.setBody(MESSAGE);
+ msg.setSender(mSender);
+
+ Email email = new Email();
+ try {
+ CLog.d("Sending initial burst of %d emails\n", mInitialBurst);
+ for (int i = 1; i <= mInitialBurst; i++){
+ msg.setSubject(SUBJECT + i);
+ email.send(msg);
+ Thread.sleep(2000);
+ }
+ for (int i = mInitialBurst; i <= mEmails; i++){
+ msg.setSubject(SUBJECT + i);
+ email.send(msg);
+ CLog.i("Sending email %d/%d", i, mEmails);
+ Thread.sleep(mInterval);
+ }
+ } catch (IOException iox) {
+ CLog.e("exception sending email");
+ CLog.e(iox);
+ } catch (InterruptedException ix) {
+ CLog.e("sleep interrupted");
+ CLog.e(ix);
+ }
+ }
+
+ /**
+ * Helper method to load BulkMailer from config.
+ * The config must include the following tag
+ * <object type="emailer" class="com.google.tradefed.util.BulkEmailer">
+ *
+ * @param config the config
+ * @return an instance of BulkEmailer
+ * @throws ConfigurationException
+ */
+ public static BulkEmailer loadMailer(IConfiguration config) throws ConfigurationException {
+ Object o = config.getConfigurationObject("emailer");
+ if (o instanceof BulkEmailer) {
+ return (BulkEmailer) o;
+ }
+ throw new ConfigurationException("Missing or invalid emailer definition in config");
+ }
+}
diff --git a/src/com/android/tradefed/util/IEmail.java b/src/com/android/tradefed/util/IEmail.java
index dd53cd4..b368815 100644
--- a/src/com/android/tradefed/util/IEmail.java
+++ b/src/com/android/tradefed/util/IEmail.java
@@ -89,6 +89,16 @@
public void setSubject(String subject) {
mSubject = subject;
}
+
+ public void setTos(String[] receipents){
+ int size = receipents.length;
+ if (mBccAddrs == null) {
+ mBccAddrs = new ArrayList<String>(size);
+ }
+ for (int i=0; i<size; i++){
+ mBccAddrs.add(receipents[i]);
+ }
+ }
public void setBody(String body) {
mBody = body;
}