001: package org.enhydra.shark.toolagent;
002:
003: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
004: import org.enhydra.shark.api.client.wfservice.WMEntity;
005: import org.enhydra.shark.api.internal.toolagent.AppParameter;
006: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
007: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
008: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
009: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
010:
011: /**
012: * Tool agent that sends or receives e-mail.
013: *
014: * This tool agent now supports also sending the
015: * mail with attachments.
016: *
017: *
018: * @author Paloma Trigueros
019: * @author Sasa Bojanic
020: *
021: */
022: public class MailToolAgent extends AbstractToolAgent {
023:
024: public static final long APP_MODE_SEND = 0;
025:
026: public static final long APP_MODE_RECEIVE = 1;
027:
028: public void invokeApplication(WMSessionHandle shandle, long handle,
029: WMEntity appInfo, WMEntity toolInfo,
030: String applicationName, String procInstId, String assId,
031: AppParameter[] parameters, Integer appMode)
032: throws ApplicationNotStarted, ApplicationNotDefined,
033: ApplicationBusy, ToolAgentGeneralException {
034:
035: // for(int i=0; i<parameters.length; i++) {
036: // System.out.println("[Actual="+parameters[i].the_actual_name + "][Formal=" + parameters[i].the_formal_name+ "]");
037: // }
038:
039: super
040: .invokeApplication(shandle, handle, appInfo, toolInfo,
041: applicationName, procInstId, assId, parameters,
042: appMode);
043:
044: try {
045: status = APP_STATUS_RUNNING;
046:
047: if (appName == null || appName.trim().length() == 0) {
048: readParamsFromExtAttributes((String) parameters[0].the_value);
049: }
050:
051: if (appName == null || appName.trim().length() == 0) {
052: appName = "org.enhydra.shark.toolagent.DefaultMailMessageHandler";
053: }
054:
055: ClassLoader cl = getClass().getClassLoader();
056: Class c = cl.loadClass(appName);
057: MailMessageHandler mch = (MailMessageHandler) c
058: .newInstance();
059:
060: // Get parameters - ignore 1. param, these are ext.attribs
061: AppParameter[] aps = new AppParameter[parameters.length - 1];
062: System.arraycopy(parameters, 1, aps, 0, aps.length);
063:
064: mch.configure(this .cus, shandle, procInstId, assId, aps);
065:
066: long am = 0;
067: if (appMode != null
068: && appMode.intValue() == APP_MODE_RECEIVE) {
069: am = 1;
070: }
071:
072: if (am == APP_MODE_SEND) {
073: // Send the message
074: System.out.println("Sending mail...");
075: mch.sendMail();
076: System.out.println("Mail sent.");
077: }
078: // Receive the message
079: else if (am == APP_MODE_RECEIVE) {
080: System.out.println("Receiving mail...");
081: String mailSubject = mch.receiveMail();
082: System.out.println("Mail received:" + mailSubject);
083: }
084:
085: status = APP_STATUS_FINISHED;
086:
087: } catch (ClassNotFoundException cnf) {
088: cus.error(shandle, "MailToolAgent - application " + appName
089: + " terminated incorrectly, can't find class: "
090: + cnf);
091: status = APP_STATUS_INVALID;
092: throw new ApplicationNotDefined("Can't find class "
093: + appName, cnf);
094: } catch (NoClassDefFoundError ncdfe) {
095: cus
096: .error(
097: shandle,
098: "MailToolAgent - application "
099: + appName
100: + " terminated incorrectly, can't find class definition: "
101: + ncdfe);
102: status = APP_STATUS_INVALID;
103: throw new ApplicationNotDefined("Class " + appName
104: + " can't be executed", ncdfe);
105: } catch (Throwable ex) {
106: cus.error(shandle, "MailToolAgent - application " + appName
107: + " terminated incorrectly: " + ex);
108: status = APP_STATUS_INVALID;
109: throw new ToolAgentGeneralException(ex);
110: }
111: }
112:
113: public String getInfo(WMSessionHandle shandle)
114: throws ToolAgentGeneralException {
115: String i = "Sends and receives mail messages."
116: + "\nThere is a MailMessageHandler interface defined that is used to actually "
117: + "\nhandle mails. We gave default implementation of this interface, but one can "
118: + "\ncreate its own implementation. This interface is specifically defined for this "
119: + "\ntool agent, and it is not part of Shark's APIs."
120: + "\nWhen performing mappings, you should set application name to be the full class "
121: + "\nname of the implementation class of MailMessageHandler interface."
122: + "\n"
123: + "\nTo be able to work with our DefaultMailMessageHandler, you must define some "
124: + "\nproperties, and here is a section from shark's configuration file \"Shark.conf\" "
125: + "\nthat defines these properties:"
126: + "\n# the parameters for retrieving mails, possible values for protocol are \"pop3\" and \"imap\""
127: + "\nDefaultMailMessageHandler.IncomingMailServer=someserver.co.yu"
128: + "\nDefaultMailMessageHandler.IncomingMailProtocol=pop3"
129: + "\nDefaultMailMessageHandler.StoreFolderName=INBOX"
130: + "\nDefaultMailMessageHandler.IMAPPortNo=143"
131: + "\nDefaultMailMessageHandler.POP3PortNo=110"
132: + "\n"
133: + "\n# the parameters for sending mails"
134: + "\nDefaultMailMessageHandler.SMTPMailServer=someserver.co.yu"
135: + "\nDefaultMailMessageHandler.SMTPPortNo=25"
136: + "\nDefaultMailMessageHandler.SourceAddress=shark@objectweb.org"
137: + "\n"
138: + "\n# credentials"
139: + "\nDefaultMailMessageHandler.Login=shark"
140: + "\nDefaultMailMessageHandler.Password=sharkspwd"
141: + "\n"
142: + "\nThis tool agent is able to understand the extended attributes with the following names:"
143: + "\n * AppName - value of this attribute should represent the full class name of "
144: + "\n MailMessageHandler interface implementation that will handle mails"
145: + "\n * AppMode - value of this attribute should represent the mode of execution, "
146: + "\n if set to 0 (zero), tool agent will send mails, and if set to 1 it "
147: + "\n will receive mails"
148: + "\n"
149: + "\n NOTE: Tool agent will read extended attributes only if they are called through"
150: + "\n Default tool agent (not by shark directly) and this is the case when information "
151: + "\n on which tool agent to start for XPDL application definition is not contained in mappings";
152: return i;
153: }
154:
155: }
|