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 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 26 public OutgoingMailServerManager() { 27 createOutgoingMailServerList(); 28 } 29 30 33 38 public void itemListChanged(ItemListChangeEvent e) { 39 fireItemListChanged(e); 40 } 41 42 46 public java.util.Vector getOutgoingMailServerList() { 47 return manager.getItems(); 48 } 49 50 54 public void addOutgoingMailServer(String OutgoingMailServerName) { 55 manager.addItem(OutgoingMailServerName); 56 } 57 58 61 public void addOutgoingMailServer(String [] OutgoingMailServerName) { 62 manager.addItem(OutgoingMailServerName); 63 } 64 65 68 public void removeOutgoingMailServer(String OutgoingMailServerName) { 69 manager.removeItem(OutgoingMailServerName); 70 } 71 72 75 public void removeOutgoingMailServer(String [] OutgoingMailServerNames) { 76 manager.removeItem(OutgoingMailServerNames); 77 } 78 79 82 public void removeOutgoingMailServer(OutgoingMailServer OutgoingMailServer) { 83 manager.removeItem(OutgoingMailServer); 84 } 85 86 89 public void removeOutgoingMailServer(OutgoingMailServer[] OutgoingMailServers) { 90 manager.removeItem(OutgoingMailServers); 91 } 92 93 97 public OutgoingMailServer getOutgoingMailServer(String OutgoingMailServerID) { 98 return (OutgoingMailServer) manager.getItem(OutgoingMailServerID); 99 } 100 101 106 public OutgoingMailServer getDefaultOutgoingMailServer() { 107 if (defaultServer == null) { 108 String defaultId = Pooka.getProperty("OutgoingServer._default", ""); 109 if (defaultId != "") { 110 defaultServer = new DefaultOutgoingMailServer(defaultId); 111 } 112 } 113 114 return defaultServer; 115 } 116 117 120 public void addItemListChangeListener(ItemListChangeListener ilcl) { 121 if (! listenerList.contains(ilcl)) 122 listenerList.add(ilcl); 123 } 124 125 128 public void removeItemListChangeListener(ItemListChangeListener ilcl) { 129 listenerList.remove(ilcl); 130 } 131 132 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 144 public Item createItem(VariableBundle sourceBundle, String resourceString, String itemID) { 145 return new OutgoingMailServer(itemID); 146 } 147 148 151 155 private void createOutgoingMailServerList() { 156 manager = new ItemManager("OutgoingServer", Pooka.getResources(), this); 157 158 manager.addItemListChangeListener(this); 159 } 160 161 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 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 191 class DefaultOutgoingMailServer extends OutgoingMailServer { 192 String defaultServerId = null; 193 OutgoingMailServer underlyingServer; 194 String outboxID = null; 195 196 200 public DefaultOutgoingMailServer (String newDefaultServerId) { 201 super(newDefaultServerId); 202 } 203 204 207 protected void configure() { 208 defaultServerId = id; 209 id = Pooka.getProperty("OutgoingServer._default.label", "*default*"); 210 propertyName = "OutgoingServer._default"; 211 212 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 228 protected void internal_sendAll() throws javax.mail.MessagingException { 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 289 public NetworkConnection getConnection() { 290 return underlyingServer.getConnection(); 291 } 292 293 297 public FolderInfo getOutbox() { 298 if (outboxID != null && outboxID != "") { 299 return Pooka.getStoreManager().getFolder(outboxID); 300 } else { 301 return null; 302 } 303 } 304 305 308 public URLName getSendMailURL() { 309 return underlyingServer.getSendMailURL(); 310 } 311 } 312 313 } 314 | Popular Tags |