001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: MailTool2.java,v 1.2 2006/11/17 10:54:22 drmlipp Exp $
021: *
022: * $Log: MailTool2.java,v $
023: * Revision 1.2 2006/11/17 10:54:22 drmlipp
024: * Added reporting of root exception.
025: *
026: * Revision 1.1 2006/11/15 10:13:36 drmlipp
027: * Finished new mail tool.
028: *
029: * Revision 1.3 2006/11/14 16:47:59 drmlipp
030: * Upated mail tool.
031: *
032: * Revision 1.2 2006/09/29 12:32:10 drmlipp
033: * Consistently using WfMOpen as projct name now.
034: *
035: * Revision 1.1.1.3 2004/08/18 15:17:38 drmlipp
036: * Update to 1.2
037: *
038: * Revision 1.14 2004/03/31 20:15:20 lipp
039: * Cleaned up mail tool implementation.
040: *
041: * Revision 1.13 2004/02/21 21:31:01 lipp
042: * Some more refactoring to resolve cyclic dependencies.
043: *
044: * Revision 1.12 2003/07/02 11:52:00 montag
045: * class comment corrected.
046: *
047: * Revision 1.11 2003/06/27 08:51:44 lipp
048: * Fixed copyright/license information.
049: *
050: * Revision 1.10 2003/05/06 13:21:30 lipp
051: * Resolved cyclic dependency.
052: *
053: * Revision 1.9 2003/05/02 14:55:58 lipp
054: * Resolved some more package dependencies.
055: *
056: * Revision 1.8 2003/04/26 16:11:14 lipp
057: * Moved some classes to reduce package dependencies.
058: *
059: * Revision 1.7 2003/03/31 16:50:28 huaiyang
060: * Logging using common-logging.
061: *
062: * Revision 1.6 2003/03/31 09:10:38 huaiyang
063: * configuration for mail service changed.
064: *
065: * Revision 1.5 2003/03/21 08:55:04 huaiyang
066: * minor type error.
067: *
068: * Revision 1.4 2003/03/20 14:35:08 huaiyang
069: * comment corrected.
070: *
071: * Revision 1.3 2003/03/20 10:43:47 huaiyang
072: * do not need toolbase because the DefaultToolImpl.parameterMap has
073: * performed the function.
074: *
075: * Revision 1.2 2003/03/19 13:14:34 huaiyang
076: * some improvement by finding out the actual params.
077: *
078: * Revision 1.1 2003/03/18 11:00:02 huaiyang
079: * started mail tool.
080: *
081: *
082: */
083: package de.danet.an.workflow.tools;
084:
085: import java.io.IOException;
086: import java.io.Serializable;
087:
088: import java.util.Date;
089: import java.util.Map;
090:
091: import java.rmi.RemoteException;
092:
093: import javax.mail.Message;
094: import javax.mail.MessagingException;
095: import javax.mail.NoSuchProviderException;
096: import javax.mail.Session;
097: import javax.mail.Transport;
098: import javax.mail.internet.AddressException;
099: import javax.mail.internet.InternetAddress;
100: import javax.mail.internet.MimeBodyPart;
101: import javax.mail.internet.MimeMessage;
102: import javax.mail.internet.MimeMultipart;
103: import javax.naming.NamingException;
104:
105: import de.danet.an.util.EJBUtil;
106:
107: import de.danet.an.workflow.omgcore.InvalidDataException;
108: import de.danet.an.workflow.omgcore.ProcessData;
109:
110: import de.danet.an.workflow.api.Activity;
111: import de.danet.an.workflow.api.ActivityUniqueKey;
112: import de.danet.an.workflow.api.DefaultProcessData;
113: import de.danet.an.workflow.api.FormalParameter;
114:
115: import de.danet.an.workflow.spis.aii.ApplicationNotStoppedException;
116: import de.danet.an.workflow.spis.aii.CannotExecuteException;
117: import de.danet.an.workflow.spis.aii.ResultProvider;
118: import de.danet.an.workflow.spis.aii.ToolAgent;
119:
120: /**
121: * This class provides a tool that sends mail.
122: *
123: * @author <a href="mailto:mao@danet.de"></a>
124: * @version $Revision: 1.2 $
125: */
126:
127: public class MailTool2 implements ToolAgent, ResultProvider,
128: Serializable {
129:
130: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
131: .getLog(MailTool2.class);
132:
133: private String defaultSender = null;
134: protected boolean argumentsIncludeSubject = false;
135:
136: /** The result container. */
137: private ThreadLocal result = new ThreadLocal();
138:
139: /**
140: * Creates an instance of <code>MailTool</code>
141: * with all attributes initialized to default values.
142: */
143: public MailTool2() {
144: argumentsIncludeSubject = true;
145: }
146:
147: /**
148: * Set the default sender. It is used if no sender is defined.
149: *
150: * @param defaultSender the default sender.
151: */
152: public void setDefaultSender(String defaultSender) {
153: this .defaultSender = defaultSender;
154: }
155:
156: // Implementation of de.danet.an.workflow.spis.aii.ToolAgent
157:
158: /**
159: * Describe <code>invoke</code> method here.
160: *
161: * @param activity a <code>WfActivity</code> value
162: * @param formPars the formal parameters.
163: * @param map a <code>Map</code> value
164: * @exception CannotExecuteException if an error occurs
165: */
166: public void invoke(Activity activity, FormalParameter[] formPars,
167: Map map) throws RemoteException, CannotExecuteException {
168: // initialize the parameters: recipient, message, sender.
169: ActivityUniqueKey auk = null;
170: try {
171: auk = activity.uniqueKey();
172: result.set(sendMail(formPars, map));
173: } catch (InvalidDataException e) {
174: logger.error(e.getMessage());
175: logger.debug(e.getMessage(), e);
176: throw new CannotExecuteException(e.getMessage(), e);
177: } catch (NoSuchProviderException e) {
178: logger.error(e.getMessage());
179: logger.debug(e.getMessage(), e);
180: throw new CannotExecuteException(e.getMessage(), e);
181: } catch (NamingException e) {
182: logger.error(e.getMessage(), e);
183: throw new CannotExecuteException(e.getMessage(), e);
184: } catch (AddressException e) {
185: logger.error(e.getMessage(), e);
186: throw new CannotExecuteException(e.getMessage(), e);
187: } catch (MessagingException e) {
188: logger.error(e.getMessage());
189: logger.debug(e.getMessage(), e);
190: throw new CannotExecuteException(e.getMessage(), e);
191: } finally {
192: if (logger.isDebugEnabled()) {
193: logger.debug("Finished invocation of " + auk);
194: }
195: }
196: }
197:
198: /**
199: * Sends the message as one email via the javax.mail api.
200: * If the mail is sent successfully, process date is returned with the
201: * status of OK.
202: */
203: private ProcessData sendMail(FormalParameter[] formPars, Map map)
204: throws InvalidDataException, NamingException,
205: MessagingException, NoSuchProviderException,
206: AddressException {
207: String status = null;
208: for (int i = 0; i < formPars.length; i++) {
209: if (formPars[i].mode() != FormalParameter.Mode.IN) {
210: status = formPars[i].id();
211: continue;
212: }
213: }
214: int argPos = 0;
215: InternetAddress[] recipients = InternetAddress
216: .parse((String) map.get(formPars[argPos++].id()));
217: String subject = null;
218: if (argumentsIncludeSubject) {
219: subject = (String) map.get(formPars[argPos++].id());
220: }
221: String message = (String) map.get(formPars[argPos++].id());
222: InternetAddress sender = null;
223: String newSender = (String) map.get(formPars[argPos++].id());
224: if ((newSender != null) && !newSender.trim().equals("")) {
225: // parse the destination addresses
226: sender = new InternetAddress(newSender);
227: } else if (defaultSender != null) {
228: sender = new InternetAddress(defaultSender);
229: }
230: // do sending mail
231: Session mailSession = (Session) EJBUtil
232: .lookupJNDIEntry("java:comp/env/toolagents/mailtool/Mail");
233: // create a message
234: Message msg = new MimeMessage(mailSession);
235: if (sender != null) {
236: msg.setFrom(sender);
237: }
238: msg.setRecipients(Message.RecipientType.TO, recipients);
239: msg.setSentDate(new Date()); // Date: header
240: if (subject != null) {
241: msg.setSubject(subject);
242: }
243: MimeBodyPart mimeBody = new MimeBodyPart();
244: mimeBody.setText(message, "ISO-8859-1");
245: MimeMultipart mimeMulti = new MimeMultipart();
246: mimeMulti.addBodyPart(mimeBody);
247: msg.setContent(mimeMulti);
248: msg.saveChanges();
249: // send the message
250: Transport.send(msg);
251: // build return value
252: ProcessData resData = new DefaultProcessData();
253: if (status != null) {
254: resData.put(status, "OK");
255: }
256: return resData;
257: }
258:
259: /**
260: * Return the result evaluated during {@link ToolAgent#invoke
261: * <code>invoke</code>}. The method will only be called once after
262: * each invoke, i.e. the attribute holding the result be be
263: * cleared in this method.
264: *
265: * @return the result data or <code>null</code> if the invocation
266: * does not return any data.
267: */
268: public Object result() {
269: Map res = (Map) result.get();
270: result.set(null);
271: return res;
272: }
273:
274: /**
275: * Describe <code>terminate</code> method here.
276: *
277: * @param activity a <code>WfActivity</code> value
278: * @throws ApplicationNotStoppedException if the application could
279: * not be terminated.
280: */
281: public void terminate(Activity activity)
282: throws ApplicationNotStoppedException {
283: throw new ApplicationNotStoppedException(
284: "Terminate not implemented for MailTool.");
285: }
286: }
|