KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka;
2
3 import java.util.*;
4
5 import javax.mail.*;
6 import javax.mail.internet.*;
7
8 import net.suberic.util.*;
9
10 /**
11  * <p>An object which manages OutgoingMailServer resources.</p>
12  *
13  * @author Allen Petersen
14  * @version $Revision: 1500 $
15  */

16 public class OutgoingMailServerManager implements ItemCreator, ItemListChangeListener {
17   
18   private ItemManager manager;
19   private LinkedList listenerList = new LinkedList();
20
21   private DefaultOutgoingMailServer defaultServer = null;
22
23   /**
24    * <p>Creates a new OutgoingMailServerManager.</p>
25    */

26   public OutgoingMailServerManager() {
27     createOutgoingMailServerList();
28   }
29
30   //-----------------------
31
// public interface.
32

33   /**
34    * This listens for ItemListChangeEvents, which result from changes to the
35    * "OutgoingMailServer" property. The result is that refreshOutgoingMailServers() is called,
36    * and then the event is passed to listeners to this object.
37    */

38   public void itemListChanged(ItemListChangeEvent e) {
39     fireItemListChanged(e);
40   }
41
42   /**
43    * This returns a Vector with all the currently registered OutgoingMailServer
44    * objects.
45    */

46   public java.util.Vector JavaDoc getOutgoingMailServerList() {
47     return manager.getItems();
48   }
49   
50   /**
51    * This adds the OutgoingMailServer with the given OutgoingMailServerName to the
52    * allOutgoingMailServers list.
53    */

54   public void addOutgoingMailServer(String JavaDoc OutgoingMailServerName) {
55     manager.addItem(OutgoingMailServerName);
56   }
57   
58   /**
59    * This adds the OutgoingMailServers with the given OutgoingMailServerNames to the allOutgoingMailServers list.
60    */

61   public void addOutgoingMailServer(String JavaDoc[] OutgoingMailServerName) {
62     manager.addItem(OutgoingMailServerName);
63   }
64   
65   /**
66    * This removes the OutgoingMailServer with the given OutgoingMailServerName.
67    */

68   public void removeOutgoingMailServer(String JavaDoc OutgoingMailServerName) {
69     manager.removeItem(OutgoingMailServerName);
70   }
71
72   /**
73    * This removes the OutgoingMailServers with the given OutgoingMailServerNames.
74    */

75   public void removeOutgoingMailServer(String JavaDoc[] OutgoingMailServerNames) {
76     manager.removeItem(OutgoingMailServerNames);
77   }
78
79   /**
80    * This removes the given OutgoingMailServer.
81    */

82   public void removeOutgoingMailServer(OutgoingMailServer OutgoingMailServer) {
83     manager.removeItem(OutgoingMailServer);
84   }
85   
86   /**
87    * This removes the given OutgoingMailServers.
88    */

89   public void removeOutgoingMailServer(OutgoingMailServer[] OutgoingMailServers) {
90     manager.removeItem(OutgoingMailServers);
91   }
92
93   /**
94    * This returns the NetwordOutgoingMailServer with the given OutgoingMailServerName if it
95    * exists; otherwise, returns null.
96    */

97   public OutgoingMailServer getOutgoingMailServer(String JavaDoc OutgoingMailServerID) {
98     return (OutgoingMailServer) manager.getItem(OutgoingMailServerID);
99   }
100
101   /**
102    * This returns the NetworkOutgoingMailServer with the given
103    * OutgoingMailServerName if it
104    * exists; otherwise, returns null.
105    */

106   public OutgoingMailServer getDefaultOutgoingMailServer() {
107     if (defaultServer == null) {
108       String JavaDoc defaultId = Pooka.getProperty("OutgoingServer._default", "");
109       if (defaultId != "") {
110     defaultServer = new DefaultOutgoingMailServer(defaultId);
111       }
112     }
113
114     return defaultServer;
115   }
116
117   /**
118    * This adds a ItemListChangeListener to the local listener list.
119    */

120   public void addItemListChangeListener(ItemListChangeListener ilcl) {
121     if (! listenerList.contains(ilcl))
122       listenerList.add(ilcl);
123   }
124   
125   /**
126    * This removes a ItemListChangeListener from the local listener list.
127    */

128   public void removeItemListChangeListener(ItemListChangeListener ilcl) {
129     listenerList.remove(ilcl);
130   }
131
132   /**
133    * This notifies all listeners that the OutgoingMailServerList has changed.
134    */

135   public void fireItemListChanged(ItemListChangeEvent e) {
136     for (int i = 0; i < listenerList.size(); i++)
137       ((ItemListChangeListener)listenerList.get(i)).itemListChanged(e);
138   }
139   
140
141   /**
142    * This creates a new OutgoingMailServer.
143    */

144   public Item createItem(VariableBundle sourceBundle, String JavaDoc resourceString, String JavaDoc itemID) {
145     return new OutgoingMailServer(itemID);
146   }
147
148   //---------------------------
149
// the background stuff.
150

151   /**
152    * This loads and creates all the OutgoingMailServers using the "OutgoingServer"
153    * property of the main Pooka VariableBundle.
154    */

155   private void createOutgoingMailServerList() {
156     manager = new ItemManager("OutgoingServer", Pooka.getResources(), this);
157
158     manager.addItemListChangeListener(this);
159   }
160
161   /**
162    * Tells all of the FolderInfos that are outboxes that they are, in fact,
163    * outboxes.
164    */

165   public void loadOutboxFolders() {
166     Vector v = getOutgoingMailServerList();
167
168     for (int i = 0; i < v.size(); i++) {
169       OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
170       oms.loadOutboxFolder();
171     }
172   }
173
174   /**
175    * Stops all mail server threads.
176    */

177   public void stopServers() {
178     Vector v = getOutgoingMailServerList();
179     VariableBundle resources = Pooka.getResources();
180     for (int i = 0; i < v.size(); i++) {
181       OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
182       oms.stopThread();
183       resources.removeValueChangeListener(oms);
184     }
185     resources.removeValueChangeListener(manager);
186   }
187
188   /**
189    * A special OutgoingMailServer that represents the 'default' value.
190    */

191   class DefaultOutgoingMailServer extends OutgoingMailServer {
192     String JavaDoc defaultServerId = null;
193     OutgoingMailServer underlyingServer;
194     String JavaDoc outboxID = null;
195
196     /**
197      * <p>Creates a new DefaultOutgoingMailServer using the given
198      * String as the default server id.
199      */

200     public DefaultOutgoingMailServer (String JavaDoc newDefaultServerId) {
201       super(newDefaultServerId);
202     }
203     
204     /**
205      * <p>Configures this mail server.</p>
206      */

207     protected void configure() {
208       defaultServerId = id;
209       id = Pooka.getProperty("OutgoingServer._default.label", "*default*");
210       propertyName = "OutgoingServer._default";
211
212       // changing because we get a NPE here for some reason...
213
//underlyingServer = getOutgoingMailServer(defaultServerId);
214

215       underlyingServer = Pooka.getOutgoingMailManager().getOutgoingMailServer(defaultServerId);
216       outboxID = Pooka.getProperty("OutgoingServer." + defaultServerId + ".outbox", "");
217
218       mailServerThread = new net.suberic.util.thread.ActionThread("default - smtp thread");
219       mailServerThread.start();
220     }
221     
222     /**
223      * Sends all available messages.
224      *
225      * What this will do actually is send all of the default server's
226      * messages, and then send the underlying server's messages, if any.
227      */

228     protected void internal_sendAll() throws javax.mail.MessagingException JavaDoc {
229       
230       NetworkConnection connection = getConnection();
231       
232       if (connection.getStatus() == NetworkConnection.DISCONNECTED) {
233     connection.connect();
234       }
235       
236       if (connection.getStatus() != NetworkConnection.CONNECTED) {
237     throw new MessagingException(Pooka.getProperty("error.connectionDown", "Connection down for Mail Server: ") + getItemID());
238       }
239       
240       Transport sendTransport = Pooka.getDefaultSession().getTransport(underlyingServer.getSendMailURL());
241       try {
242     sendTransport.connect();
243     
244     FolderInfo outbox = getOutbox();
245     
246     Message[] msgs;
247
248     if (outbox != null) {
249       msgs = outbox.getFolder().getMessages();
250       
251       try {
252         for (int i = 0; i < msgs.length; i++) {
253           Message m = msgs[i];
254           if (! m.isSet(Flags.Flag.DRAFT)) {
255         sendTransport.sendMessage(m, m.getAllRecipients());
256         m.setFlag(Flags.Flag.DELETED, true);
257           }
258         }
259       } finally {
260         outbox.getFolder().expunge();
261       }
262     }
263
264     FolderInfo underlyingOutbox = underlyingServer.getOutbox();
265     
266     if (underlyingOutbox != null && underlyingOutbox != outbox) {
267       msgs = underlyingOutbox.getFolder().getMessages();
268       
269       try {
270         for (int i = 0; i < msgs.length; i++) {
271           Message m = msgs[i];
272           if (! m.isSet(Flags.Flag.DRAFT)) {
273         sendTransport.sendMessage(m, m.getAllRecipients());
274         m.setFlag(Flags.Flag.DELETED, true);
275           }
276         }
277       } finally {
278         underlyingOutbox.getFolder().expunge();
279       }
280     }
281       } finally {
282     sendTransport.close();
283       }
284     }
285     
286     /**
287      * <p>The NetworkConnection that this OutgoingMailServer depends on.
288      */

289     public NetworkConnection getConnection() {
290       return underlyingServer.getConnection();
291     }
292     
293     /**
294      * <p>The FolderInfo where messages for this MailServer are
295      * stored until they're ready to be sent.
296      */

297     public FolderInfo getOutbox() {
298       if (outboxID != null && outboxID != "") {
299     return Pooka.getStoreManager().getFolder(outboxID);
300       } else {
301     return null;
302       }
303     }
304     
305     /**
306      * Returns the sendMailURL.
307      */

308     public URLName getSendMailURL() {
309       return underlyingServer.getSendMailURL();
310     }
311   }
312
313 }
314
Popular Tags