/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.util.*;
import org.apache.log4j.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.htmlcheck.*;
import com.silverwrist.venice.util.MailSend;

class MailerAgent implements Runnable
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(MailerAgent.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private MailSend msend;                   // the mail sender
  private List recipients;                  // who gets the message

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  MailerAgent(MailSend msend, List recipients)
  {
    this.msend = msend;
    this.recipients = recipients;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Runnable
   *--------------------------------------------------------------------------------
   */

  public void run()
  {
    NDC.push("MailerAgent");

    try
    { // Deliver the mail to each of the target addresses in turn.
      Iterator it = recipients.iterator();
      while (it.hasNext())
      { // get ready to deliver the mail...
	String addr = (String)(it.next());
	if (logger.isDebugEnabled())
	  logger.debug("Delivering to " + addr);

	try
	{ // set the recipient and send it out
	  msend.clearRecipients(MailSend.RECIP_TO);
	  msend.addRecipient(MailSend.RECIP_TO,addr);
	  msend.send();

	} // end try
	catch (EmailException e)
	{ // log the error and move on
	  logger.error("Caught EmailException when trying to send to " + addr,e);

	} // end catch

      } // end while

    } // end try
    finally
    { // make sure and pop the diagnostic context before we go
      NDC.pop();

    } // end finally

  } // end run

} // end class MailerAgent
