001: package net.suberic.pooka;
002:
003: import java.util.*;
004:
005: import javax.mail.*;
006: import javax.mail.internet.*;
007:
008: import net.suberic.util.*;
009:
010: /**
011: * <p>An object which manages OutgoingMailServer resources.</p>
012: *
013: * @author Allen Petersen
014: * @version $Revision: 1.5 $
015: */
016: public class OutgoingMailServerManager implements ItemCreator,
017: ItemListChangeListener {
018:
019: private ItemManager manager;
020: private LinkedList listenerList = new LinkedList();
021:
022: private DefaultOutgoingMailServer defaultServer = null;
023:
024: /**
025: * <p>Creates a new OutgoingMailServerManager.</p>
026: */
027: public OutgoingMailServerManager() {
028: createOutgoingMailServerList();
029: }
030:
031: //-----------------------
032: // public interface.
033:
034: /**
035: * This listens for ItemListChangeEvents, which result from changes to the
036: * "OutgoingMailServer" property. The result is that refreshOutgoingMailServers() is called,
037: * and then the event is passed to listeners to this object.
038: */
039: public void itemListChanged(ItemListChangeEvent e) {
040: fireItemListChanged(e);
041: }
042:
043: /**
044: * This returns a Vector with all the currently registered OutgoingMailServer
045: * objects.
046: */
047: public java.util.Vector getOutgoingMailServerList() {
048: return manager.getItems();
049: }
050:
051: /**
052: * This adds the OutgoingMailServer with the given OutgoingMailServerName to the
053: * allOutgoingMailServers list.
054: */
055: public void addOutgoingMailServer(String OutgoingMailServerName) {
056: manager.addItem(OutgoingMailServerName);
057: }
058:
059: /**
060: * This adds the OutgoingMailServers with the given OutgoingMailServerNames to the allOutgoingMailServers list.
061: */
062: public void addOutgoingMailServer(String[] OutgoingMailServerName) {
063: manager.addItem(OutgoingMailServerName);
064: }
065:
066: /**
067: * This removes the OutgoingMailServer with the given OutgoingMailServerName.
068: */
069: public void removeOutgoingMailServer(String OutgoingMailServerName) {
070: manager.removeItem(OutgoingMailServerName);
071: }
072:
073: /**
074: * This removes the OutgoingMailServers with the given OutgoingMailServerNames.
075: */
076: public void removeOutgoingMailServer(
077: String[] OutgoingMailServerNames) {
078: manager.removeItem(OutgoingMailServerNames);
079: }
080:
081: /**
082: * This removes the given OutgoingMailServer.
083: */
084: public void removeOutgoingMailServer(
085: OutgoingMailServer OutgoingMailServer) {
086: manager.removeItem(OutgoingMailServer);
087: }
088:
089: /**
090: * This removes the given OutgoingMailServers.
091: */
092: public void removeOutgoingMailServer(
093: OutgoingMailServer[] OutgoingMailServers) {
094: manager.removeItem(OutgoingMailServers);
095: }
096:
097: /**
098: * This returns the NetwordOutgoingMailServer with the given OutgoingMailServerName if it
099: * exists; otherwise, returns null.
100: */
101: public OutgoingMailServer getOutgoingMailServer(
102: String OutgoingMailServerID) {
103: return (OutgoingMailServer) manager
104: .getItem(OutgoingMailServerID);
105: }
106:
107: /**
108: * This returns the NetworkOutgoingMailServer with the given
109: * OutgoingMailServerName if it
110: * exists; otherwise, returns null.
111: */
112: public OutgoingMailServer getDefaultOutgoingMailServer() {
113: if (defaultServer == null) {
114: String defaultId = Pooka.getProperty(
115: "OutgoingServer._default", "");
116: if (defaultId != "") {
117: defaultServer = new DefaultOutgoingMailServer(defaultId);
118: }
119: }
120:
121: return defaultServer;
122: }
123:
124: /**
125: * This adds a ItemListChangeListener to the local listener list.
126: */
127: public void addItemListChangeListener(ItemListChangeListener ilcl) {
128: if (!listenerList.contains(ilcl))
129: listenerList.add(ilcl);
130: }
131:
132: /**
133: * This removes a ItemListChangeListener from the local listener list.
134: */
135: public void removeItemListChangeListener(ItemListChangeListener ilcl) {
136: listenerList.remove(ilcl);
137: }
138:
139: /**
140: * This notifies all listeners that the OutgoingMailServerList has changed.
141: */
142: public void fireItemListChanged(ItemListChangeEvent e) {
143: for (int i = 0; i < listenerList.size(); i++)
144: ((ItemListChangeListener) listenerList.get(i))
145: .itemListChanged(e);
146: }
147:
148: /**
149: * This creates a new OutgoingMailServer.
150: */
151: public Item createItem(VariableBundle sourceBundle,
152: String resourceString, String itemID) {
153: return new OutgoingMailServer(itemID);
154: }
155:
156: //---------------------------
157: // the background stuff.
158:
159: /**
160: * This loads and creates all the OutgoingMailServers using the "OutgoingServer"
161: * property of the main Pooka VariableBundle.
162: */
163: private void createOutgoingMailServerList() {
164: manager = new ItemManager("OutgoingServer", Pooka
165: .getResources(), this );
166:
167: manager.addItemListChangeListener(this );
168: }
169:
170: /**
171: * Tells all of the FolderInfos that are outboxes that they are, in fact,
172: * outboxes.
173: */
174: public void loadOutboxFolders() {
175: Vector v = getOutgoingMailServerList();
176:
177: for (int i = 0; i < v.size(); i++) {
178: OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
179: oms.loadOutboxFolder();
180: }
181: }
182:
183: /**
184: * Stops all mail server threads.
185: */
186: public void stopServers() {
187: Vector v = getOutgoingMailServerList();
188: VariableBundle resources = Pooka.getResources();
189: for (int i = 0; i < v.size(); i++) {
190: OutgoingMailServer oms = (OutgoingMailServer) v.get(i);
191: oms.stopThread();
192: resources.removeValueChangeListener(oms);
193: }
194: resources.removeValueChangeListener(manager);
195: }
196:
197: /**
198: * A special OutgoingMailServer that represents the 'default' value.
199: */
200: class DefaultOutgoingMailServer extends OutgoingMailServer {
201: String defaultServerId = null;
202: OutgoingMailServer underlyingServer;
203: String outboxID = null;
204:
205: /**
206: * <p>Creates a new DefaultOutgoingMailServer using the given
207: * String as the default server id.
208: */
209: public DefaultOutgoingMailServer(String newDefaultServerId) {
210: super (newDefaultServerId);
211: }
212:
213: /**
214: * <p>Configures this mail server.</p>
215: */
216: protected void configure() {
217: defaultServerId = id;
218: id = Pooka.getProperty("OutgoingServer._default.label",
219: "*default*");
220: propertyName = "OutgoingServer._default";
221:
222: // changing because we get a NPE here for some reason...
223: //underlyingServer = getOutgoingMailServer(defaultServerId);
224:
225: underlyingServer = Pooka.getOutgoingMailManager()
226: .getOutgoingMailServer(defaultServerId);
227: outboxID = Pooka.getProperty("OutgoingServer."
228: + defaultServerId + ".outbox", "");
229:
230: mailServerThread = new net.suberic.util.thread.ActionThread(
231: "default - smtp thread");
232: mailServerThread.start();
233: }
234:
235: /**
236: * Sends all available messages.
237: *
238: * What this will do actually is send all of the default server's
239: * messages, and then send the underlying server's messages, if any.
240: */
241: protected void internal_sendAll()
242: throws javax.mail.MessagingException {
243:
244: NetworkConnection connection = getConnection();
245:
246: if (connection.getStatus() == NetworkConnection.DISCONNECTED) {
247: connection.connect();
248: }
249:
250: if (connection.getStatus() != NetworkConnection.CONNECTED) {
251: throw new MessagingException(Pooka.getProperty(
252: "error.connectionDown",
253: "Connection down for Mail Server: ")
254: + getItemID());
255: }
256:
257: Transport sendTransport = Pooka.getDefaultSession()
258: .getTransport(underlyingServer.getSendMailURL());
259: try {
260: sendTransport.connect();
261:
262: FolderInfo outbox = getOutbox();
263:
264: Message[] msgs;
265:
266: if (outbox != null) {
267: msgs = outbox.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
274: .getAllRecipients());
275: m.setFlag(Flags.Flag.DELETED, true);
276: }
277: }
278: } finally {
279: outbox.getFolder().expunge();
280: }
281: }
282:
283: FolderInfo underlyingOutbox = underlyingServer
284: .getOutbox();
285:
286: if (underlyingOutbox != null
287: && underlyingOutbox != outbox) {
288: msgs = underlyingOutbox.getFolder().getMessages();
289:
290: try {
291: for (int i = 0; i < msgs.length; i++) {
292: Message m = msgs[i];
293: if (!m.isSet(Flags.Flag.DRAFT)) {
294: sendTransport.sendMessage(m, m
295: .getAllRecipients());
296: m.setFlag(Flags.Flag.DELETED, true);
297: }
298: }
299: } finally {
300: underlyingOutbox.getFolder().expunge();
301: }
302: }
303: } finally {
304: sendTransport.close();
305: }
306: }
307:
308: /**
309: * <p>The NetworkConnection that this OutgoingMailServer depends on.
310: */
311: public NetworkConnection getConnection() {
312: return underlyingServer.getConnection();
313: }
314:
315: /**
316: * <p>The FolderInfo where messages for this MailServer are
317: * stored until they're ready to be sent.
318: */
319: public FolderInfo getOutbox() {
320: if (outboxID != null && outboxID != "") {
321: return Pooka.getStoreManager().getFolder(outboxID);
322: } else {
323: return null;
324: }
325: }
326:
327: /**
328: * Returns the sendMailURL.
329: */
330: public URLName getSendMailURL() {
331: return underlyingServer.getSendMailURL();
332: }
333: }
334:
335: }
|