KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > MailToolAgent


1 package org.enhydra.shark.toolagent;
2
3 import org.enhydra.shark.api.SharkTransaction;
4 import org.enhydra.shark.api.internal.toolagent.AppParameter;
5 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
6 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
7 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
8 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
9
10
11 /**
12  * Tool agent that sends or receives e-mail.
13  * @author Paloma Trigueros
14  * @author Sasa Bojanic
15  */

16 public class MailToolAgent extends AbstractToolAgent {
17
18    public static final long APP_MODE_SEND=0;
19    public static final long APP_MODE_RECEIVE=1;
20
21    public void invokeApplication (SharkTransaction t,
22                                   long handle,
23                                   String JavaDoc applicationName,
24                                   String JavaDoc procInstId,
25                                   String JavaDoc assId,
26                                   AppParameter[] parameters,
27                                   Integer JavaDoc appMode)
28       throws ApplicationNotStarted, ApplicationNotDefined,
29       ApplicationBusy, ToolAgentGeneralException {
30
31       super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode);
32
33       try {
34          status=APP_STATUS_RUNNING;
35
36          if (appName==null || appName.trim().length()==0) {
37             readParamsFromExtAttributes((String JavaDoc)parameters[0].the_value);
38          }
39
40          if (appName==null || appName.trim().length()==0) {
41             appName="org.enhydra.shark.toolagent.DefaultMailMessageHandler";
42          }
43
44          ClassLoader JavaDoc cl=getClass().getClassLoader();
45          Class JavaDoc c = cl.loadClass(appName);
46          MailMessageHandler mch=(MailMessageHandler)c.newInstance();
47
48          // Get parameters - ignore 1. param, these are ext.attribs
49
AppParameter[] aps=new AppParameter[parameters.length-1];
50          System.arraycopy(parameters,1,aps,0,aps.length);
51
52          mch.configure(this.cus,aps);
53
54          long am=0;
55          if (appMode!=null && appMode.intValue()==APP_MODE_RECEIVE) {
56             am=1;
57          }
58
59          if (am==APP_MODE_SEND) {
60             // Send the message
61
System.out.println("Sending mail...");
62             mch.sendMail();
63             System.out.println("Mail sent.");
64          }
65             // Receive the message
66
else if (am==APP_MODE_RECEIVE){
67             System.out.println("Receiving mail...");
68             String JavaDoc mailSubject = mch.receiveMail();
69             System.out.println("Mail received:"+mailSubject);
70          }
71
72          status=APP_STATUS_FINISHED;
73
74       } catch (ClassNotFoundException JavaDoc cnf) {
75          cus.error("MailToolAgent - application "+appName+" terminated incorrectly, can't find class: "+cnf);
76          status=APP_STATUS_INVALID;
77          throw new ApplicationNotDefined("Can't find class "+appName,cnf);
78       } catch (NoClassDefFoundError JavaDoc ncdfe) {
79          cus.error("MailToolAgent - application "+appName+" terminated incorrectly, can't find class definition: "+ncdfe);
80          throw new ApplicationNotDefined("Class "+appName+" can't be executed",ncdfe);
81       } catch(Throwable JavaDoc ex) {
82          cus.error("MailToolAgent - application "+appName+" terminated incorrectly: " + ex);
83          status=APP_STATUS_INVALID;
84          throw new ToolAgentGeneralException(ex);
85       }
86    }
87
88    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
89       String JavaDoc i="Sends and receives mail messages." +
90          "\nThere is a MailMessageHandler interface defined that is used to actually "+
91          "\nhandle mails. We gave default implementation of this interface, but one can "+
92          "\ncreate its own implementation. This interface is specifically defined for this "+
93          "\ntool agent, and it is not part of Shark's APIs."+
94          "\nWhen performing mappings, you should set application name to be the full class "+
95          "\nname of the implementation class of MailMessageHandler interface."+
96          "\n"+
97          "\nTo be able to work with our DefaultMailMessageHandler, you must define some "+
98          "\nproperties, and here is a section from shark's configuration file \"Shark.conf\" "+
99          "\nthat defines these properties:"+
100          "\n# the parameters for retrieving mails, possible values for protocol are \"pop3\" and \"imap\""+
101          "\nDefaultMailMessageHandler.IncomingMailServer=someserver.co.yu"+
102          "\nDefaultMailMessageHandler.IncomingMailProtocol=pop3"+
103          "\nDefaultMailMessageHandler.StoreFolderName=INBOX"+
104          "\nDefaultMailMessageHandler.IMAPPortNo=143"+
105          "\nDefaultMailMessageHandler.POP3PortNo=110"+
106          "\n"+
107          "\n# the parameters for sending mails"+
108          "\nDefaultMailMessageHandler.SMTPMailServer=someserver.co.yu"+
109          "\nDefaultMailMessageHandler.SMTPPortNo=25"+
110          "\nDefaultMailMessageHandler.SourceAddress=shark@objectweb.org"+
111          "\n"+
112          "\n# credentials"+
113          "\nDefaultMailMessageHandler.Login=shark"+
114          "\nDefaultMailMessageHandler.Password=sharkspwd"+
115          "\n"+
116          "\nThis tool agent is able to understand the extended attributes with the following names:"+
117          "\n * AppName - value of this attribute should represent the full class name of "+
118          "\n MailMessageHandler interface implementation that will handle mails"+
119          "\n * AppMode - value of this attribute should represent the mode of execution, "+
120          "\n if set to 0 (zero), tool agent will send mails, and if set to 1 it "+
121          "\n will receive mails"+
122          "\n"+
123          "\n NOTE: Tool agent will read extended attributes only if they are called through"+
124          "\n Default tool agent (not by shark directly) and this is the case when information "+
125          "\n on which tool agent to start for XPDL application definition is not contained in mappings";
126       return i;
127    }
128
129
130 }
131
Popular Tags