KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > MailQueue


1 package net.suberic.pooka;
2 import net.suberic.pooka.gui.*;
3 import net.suberic.util.thread.ActionThread;
4 import javax.mail.*;
5 import java.util.*;
6
7 /**
8  * The basics for an outgoing mail queue.
9  *
10  * This class is basically a placeholder for now, since i realized that if
11  * you're connected all the time, you don't need a send queue, and if you're
12  * running in disconnected mode, this queue won't do.
13  */

14 public class MailQueue {
15   Hashtable transportTable;
16   Session session;
17   
18   ActionThread thread = new ActionThread(Pooka.getProperty("thread.sendMailThread", "Send Mail Thread"));
19   
20   public MailQueue(Session newSession) {
21     session = newSession;
22     transportTable = new Hashtable();
23     thread.start();
24   }
25   
26   /**
27    * This adds the Message to the queue associated with the given
28    * transportURL. If sendPrecommand != "", then sendPrecommand will be executed before
29    * attempting to send each message. If Message.sendImmediately is set to true, then this
30    * will also go ahead and try to send all queued Messages using
31    * sendQueued().
32    *
33    * Note that at the moment, only Message.sendImmediately is supported.
34    */

35   public void sendMessage(NewMessageInfo pNmi, URLName pTransportURL, String JavaDoc sendPrecommand) throws MessagingException {
36     /*
37       Vector transportQueue = (Vector)transportTable.get(transportURL);
38       if (transportQueue == null) {
39       transportQueue = new Vector();
40       transportTable.put(transportURL, transportQueue);
41       }
42       
43       transportQueue.add(m);
44       if (Pooka.getProperty("Message.sendImmediately", "false").equals("true")) {
45       sendQueued();
46       }
47     */

48     
49     final URLName transportURL = pTransportURL;
50     final NewMessageInfo nmi = pNmi;
51     final String JavaDoc precommand = sendPrecommand;
52     
53     if (Pooka.getProperty("Message.sendImmediately", "false").equalsIgnoreCase("true")) {
54       thread.addToQueue(new net.suberic.util.thread.ActionWrapper(new javax.swing.AbstractAction JavaDoc() {
55       
56       public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
57         try {
58           Transport sendTransport;
59           if (precommand.length() > 0) {
60                 try {
61             System.out.println("MailQueue: Running precommand: " + precommand);
62                     Process JavaDoc p = Runtime.getRuntime().exec(precommand);
63                     p.waitFor();
64                 } catch (Exception JavaDoc ex)
65                 {
66                     System.out.println("Could not run SMTP precommand:");
67                     ex.printStackTrace();
68                 }
69               }
70               System.out.println("MailQueue: Trying to connect..");
71           sendTransport = session.getTransport(transportURL);
72           sendTransport.connect();
73           
74           Message m = nmi.getMessage();
75           sendTransport.sendMessage(m, m.getAllRecipients());
76           
77           ((NewMessageProxy)nmi.getMessageProxy()).sendSucceeded(true);
78         } catch (MessagingException me) {
79           ((NewMessageProxy)nmi.getMessageProxy()).sendFailed(null,me);
80         }
81       }
82     }, thread), new java.awt.event.ActionEvent JavaDoc(nmi, 1, "message-send"));
83       
84       
85     }
86     
87   }
88   
89   /**
90    * This will try to send all the Messages currently in the queue. If
91    * it encounters any major errors (i.e being unable to connect to the
92    * mailserver), it will more or less exit out, throwing a
93    * MessagingException saying why it failed. If it manages to send some
94    * Messages but not others, it will send the ones that it can, and return
95    * a MessagingException with all the individual sub-exceptions tacked
96    * on to it using MessagingException.setNextException().
97    *
98    * Note: do not use this method. i am now just keeping it here as
99    * a placeholder until i implement a real outgoing message queue.
100    */

101   public void sendQueued() throws MessagingException {
102     Enumeration keys = transportTable.keys();
103     URLName transportURL;
104     Transport sendTransport;
105     Vector sendQueue;
106     Message msg;
107     boolean error = false;
108     
109     while (keys.hasMoreElements()) {
110       transportURL = (URLName)keys.nextElement();
111       sendQueue = (Vector)transportTable.get(transportURL);
112       
113       if (!(sendQueue.isEmpty())) {
114     MessagingException returnException = null;
115     sendTransport = session.getTransport(transportURL);
116     sendTransport.connect();
117     
118     for (int i=sendQueue.size()-1 ; i >= 0; i--) {
119       msg = (Message)sendQueue.elementAt(i);
120       try {
121         sendTransport.sendMessage(msg, msg.getAllRecipients());
122         sendQueue.removeElementAt(i);
123       } catch (SendFailedException sfe) {
124         if (returnException == null)
125           returnException = sfe;
126         else
127           returnException.setNextException(sfe);
128       } catch (MessagingException me) {
129         if (returnException == null)
130           returnException = me;
131         else
132           returnException.setNextException(me);
133       }
134     }
135     if (returnException != null)
136       throw returnException;
137       }
138     }
139   }
140 }
141     
142
143
144
145
146
Popular Tags