01: package test;
02:
03: import dalma.TimeUnit;
04: import dalma.endpoints.email.EmailEndPoint;
05: import dalma.endpoints.email.NewMailHandler;
06: import dalma.endpoints.email.TCPListener;
07: import dalma.test.Launcher;
08:
09: import javax.mail.internet.InternetAddress;
10: import javax.mail.internet.MimeMessage;
11: import java.io.Serializable;
12: import java.util.Iterator;
13: import java.util.UUID;
14:
15: /**
16: * Tests {@link EmailEndPoint#waitForMultipleReplies(MimeMessage)}.
17: *
18: * @author Kohsuke Kawaguchi
19: */
20: public class GeneratorDemo extends Launcher implements NewMailHandler {
21: public GeneratorDemo(String[] args) throws Exception {
22: super (args);
23: }
24:
25: public static void main(String[] args) throws Exception {
26: new GeneratorDemo(args);
27: }
28:
29: EmailEndPoint ep;
30:
31: protected void setUpEndPoints() throws Exception {
32: ep = new EmailEndPoint("email", new InternetAddress(
33: "kohsuke-dalma@griffon.kohsuke.org", "dalma engine"),
34: new TCPListener(9920));
35: ep.setNewMailHandler(this );
36: engine.addEndPoint(ep);
37: }
38:
39: public void onNewMail(MimeMessage mail) throws Exception {
40: System.out.println("new e-mail");
41: createConversation(ConversationImpl.class, ep, mail);
42: }
43:
44: public static final class ConversationImpl implements Runnable,
45: Serializable {
46: private final EmailEndPoint ep;
47:
48: // initial e-mail
49: private MimeMessage email;
50:
51: public ConversationImpl(EmailEndPoint ep, MimeMessage email) {
52: this .ep = ep;
53: this .email = email;
54: }
55:
56: public void run() {
57: try {
58: UUID uuid = UUID.randomUUID();
59:
60: System.out.println("started " + uuid);
61: MimeMessage msg = email;
62: int count = 0;
63:
64: msg = (MimeMessage) msg.reply(false);
65: msg.setText("Hello! " + uuid);
66:
67: Iterator<MimeMessage> itr = ep.waitForMultipleReplies(
68: msg, 15, TimeUnit.SECONDS);
69:
70: while (itr.hasNext()) {
71: msg = itr.next();
72: System.out.println("got a reply.");
73: if (msg.getContent().toString().contains("bye"))
74: break;
75:
76: // reply
77: msg = (MimeMessage) msg.reply(false);
78: msg.setText("Hello! " + (count++) + '\n'
79: + uuid.toString());
80: ep.send(msg);
81: }
82:
83: MimeMessage reply = (MimeMessage) email.reply(false);
84: reply.setText("bye bye");
85: ep.send(reply);
86: System.out.println("done");
87: } catch (Exception e) {
88: e.printStackTrace();
89: }
90: }
91:
92: private static final long serialVersionUID = 1L;
93: }
94: }
|