Package de.danet.an.workflow.tools

Source Code of de.danet.an.workflow.tools.MailTool2

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: MailTool2.java 2989 2009-03-12 12:23:50Z drmlipp $
*
* $Log$
* Revision 1.1  2006/11/15 10:13:36  drmlipp
* Finished new mail tool.
*
* Revision 1.3  2006/11/14 16:47:59  drmlipp
* Upated mail tool.
*
* Revision 1.2  2006/09/29 12:32:10  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.3  2004/08/18 15:17:38  drmlipp
* Update to 1.2
*
* Revision 1.14  2004/03/31 20:15:20  lipp
* Cleaned up mail tool implementation.
*
* Revision 1.13  2004/02/21 21:31:01  lipp
* Some more refactoring to resolve cyclic dependencies.
*
* Revision 1.12  2003/07/02 11:52:00  montag
* class comment corrected.
*
* Revision 1.11  2003/06/27 08:51:44  lipp
* Fixed copyright/license information.
*
* Revision 1.10  2003/05/06 13:21:30  lipp
* Resolved cyclic dependency.
*
* Revision 1.9  2003/05/02 14:55:58  lipp
* Resolved some more package dependencies.
*
* Revision 1.8  2003/04/26 16:11:14  lipp
* Moved some classes to reduce package dependencies.
*
* Revision 1.7  2003/03/31 16:50:28  huaiyang
* Logging using common-logging.
*
* Revision 1.6  2003/03/31 09:10:38  huaiyang
* configuration for mail service changed.
*
* Revision 1.5  2003/03/21 08:55:04  huaiyang
* minor type error.
*
* Revision 1.4  2003/03/20 14:35:08  huaiyang
* comment corrected.
*
* Revision 1.3  2003/03/20 10:43:47  huaiyang
* do not need toolbase because the DefaultToolImpl.parameterMap has
* performed the function.
*
* Revision 1.2  2003/03/19 13:14:34  huaiyang
* some improvement by finding out the actual params.
*
* Revision 1.1  2003/03/18 11:00:02  huaiyang
* started mail tool.
*
*
*/
package de.danet.an.workflow.tools;

import java.io.IOException;
import java.io.Serializable;

import java.util.Date;
import java.util.Map;

import java.rmi.RemoteException;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.NamingException;

import de.danet.an.util.EJBUtil;

import de.danet.an.workflow.omgcore.InvalidDataException;
import de.danet.an.workflow.omgcore.ProcessData;

import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.ActivityUniqueKey;
import de.danet.an.workflow.api.DefaultProcessData;
import de.danet.an.workflow.api.FormalParameter;

import de.danet.an.workflow.spis.aii.ApplicationNotStoppedException;
import de.danet.an.workflow.spis.aii.CannotExecuteException;
import de.danet.an.workflow.spis.aii.ResultProvider;
import de.danet.an.workflow.spis.aii.ToolAgent;

/**
* This class provides a tool that sends mail.
*
* @author <a href="mailto:mao@danet.de"></a>
* @version $Revision: 2989 $
*/

public class MailTool2 implements ToolAgent, ResultProvider, Serializable {

    private static final org.apache.commons.logging.Log logger
  = org.apache.commons.logging.LogFactory.getLog(MailTool2.class);

    private String defaultSender = null;
    protected boolean argumentsIncludeSubject = false;

    /** The result container. */
    private ThreadLocal result = new ThreadLocal ();

    /**
     * Creates an instance of <code>MailTool</code>
     * with all attributes initialized to default values.
     */
    public MailTool2 () {
        argumentsIncludeSubject = true;
    }

    /**
     * Set the default sender. It is used if no sender is defined.
     *
     * @param defaultSender the default sender.
     */
    public void setDefaultSender(String defaultSender) {
  this.defaultSender = defaultSender;
    }

    // Implementation of de.danet.an.workflow.spis.aii.ToolAgent

    /**
     * Describe <code>invoke</code> method here.
     *
     * @param activity a <code>WfActivity</code> value
     * @param formPars the formal parameters.
     * @param map a <code>Map</code> value
     * @exception CannotExecuteException if an error occurs
     */
    public void invoke(Activity activity, FormalParameter[] formPars, Map map)
  throws RemoteException, CannotExecuteException {
  // initialize the parameters: recipient, message, sender.
  ActivityUniqueKey auk = null;
  try {
      auk = activity.uniqueKey();
      result.set (sendMail(formPars, map));
  } catch (InvalidDataException e) {
      logger.error (e.getMessage());
      logger.debug (e.getMessage(), e);
      throw new CannotExecuteException (e.getMessage(), e);
  } catch (NoSuchProviderException e) {
      logger.error (e.getMessage());
      logger.debug (e.getMessage(), e);
      throw new CannotExecuteException (e.getMessage(), e);
  } catch (NamingException e) {
      logger.error (e.getMessage(), e);
      throw new CannotExecuteException (e.getMessage(), e);
  } catch (AddressException e) {
      logger.error (e.getMessage(), e);
      throw new CannotExecuteException (e.getMessage(), e);
  } catch (MessagingException e) {
      logger.error (e.getMessage());
      logger.debug (e.getMessage(), e);
      throw new CannotExecuteException (e.getMessage(), e);
  } finally {
      if (logger.isDebugEnabled()) {
    logger.debug ("Finished invocation of " + auk);
      }
  }
    }
   
    /**
     * Sends the message as one email via the javax.mail api.
     * If the mail is sent successfully, process date is returned with the
     * status of OK.
     */
    private ProcessData sendMail (FormalParameter[] formPars, Map map)
  throws InvalidDataException, NamingException,
         MessagingException, NoSuchProviderException,
         AddressException {
  String status = null;
  for (int i = 0; i < formPars.length; i++) {
      if (formPars[i].mode() != FormalParameter.Mode.IN) {
    status = formPars[i].id();
    break;
      }
  }
        int argPos = 0;
  InternetAddress[] recipients
      = InternetAddress.parse((String)map.get(formPars[argPos++].id()));
        String subject = null;
        if (argumentsIncludeSubject) {
            subject = (String)map.get(formPars[argPos++].id());
        }
  String message = (String)map.get(formPars[argPos++].id());
  InternetAddress sender = null;
  String newSender = (String)map.get(formPars[argPos++].id());
  if ((newSender != null) && !newSender.trim().equals("")) {
      // parse the destination addresses
      sender = new InternetAddress (newSender);
  } else if (defaultSender != null ){
      sender = new InternetAddress (defaultSender);
  }
  // do sending mail
  Session mailSession = (Session) EJBUtil.lookupJNDIEntry
      ("java:comp/env/toolagents/mailtool/Mail");
  // create a message
  Message msg = new MimeMessage(mailSession);
        if (sender != null) {
            msg.setFrom(sender);
        }
  msg.setRecipients(Message.RecipientType.TO, recipients);
  msg.setSentDate(new Date())// Date: header
        if (subject != null) {
            msg.setSubject(subject);
        }
  MimeBodyPart mimeBody = new MimeBodyPart();
  mimeBody.setText(message, "ISO-8859-1");
  MimeMultipart mimeMulti = new MimeMultipart();
  mimeMulti.addBodyPart(mimeBody);     
  msg.setContent(mimeMulti);
  msg.saveChanges();
  // send the message
  Transport.send(msg);
  // build return value
  ProcessData resData = new DefaultProcessData();
  if (status != null) {
      resData.put (status, "OK");
  }
  return resData;
    }

    /**
     * Return the result evaluated during {@link ToolAgent#invoke
     * <code>invoke</code>}. The method will only be called once after
     * each invoke, i.e. the attribute holding the result be be
     * cleared in this method.
     *
     * @return the result data or <code>null</code> if the invocation
     * does not return any data.
     */
    public Object result () {
  Map res = (Map)result.get();
  result.set (null);
  return res;
    }

    /**
     * Describe <code>terminate</code> method here.
     *
     * @param activity a <code>WfActivity</code> value
     * @throws ApplicationNotStoppedException if the application could
     * not be terminated.
     */
    public void terminate(Activity activity)
  throws ApplicationNotStoppedException {
  throw new ApplicationNotStoppedException
      ("Terminate not implemented for MailTool.");
    }
}
TOP

Related Classes of de.danet.an.workflow.tools.MailTool2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.