| 68 | 1 package fschmidt.util.mail; | 
|  | 2 | 
|  | 3 | 
|  | 4 public class SmtpServerPool implements SmtpServer { | 
|  | 5 	private SmtpServer[] servers; | 
|  | 6 	private volatile SmtpServer defaultServer; | 
|  | 7 | 
|  | 8 	SmtpServerPool(SmtpServer... servers) { | 
|  | 9 		this.servers = servers; | 
|  | 10 		defaultServer = servers[0]; | 
|  | 11 	} | 
|  | 12 | 
|  | 13 	public void send(Mail mail, MailAddress... addresses) throws MailException { | 
|  | 14 		SmtpServer server = defaultServer; | 
|  | 15 		try { | 
|  | 16 			server.send(mail, addresses); | 
|  | 17 			return; | 
|  | 18 		} catch (MailParseException e) { | 
|  | 19 			throw e; | 
|  | 20 		} catch (MailAddressException e) { | 
|  | 21 			throw e; | 
|  | 22 		} catch (MailEncodingException e) { | 
|  | 23 			throw e; | 
|  | 24 		} catch (MailException e) { | 
|  | 25 			for (SmtpServer fallbackServer : servers) { | 
|  | 26 				if (fallbackServer == server) continue; | 
|  | 27 				try { | 
|  | 28 					fallbackServer.send(mail, addresses); | 
|  | 29 					defaultServer = fallbackServer; | 
|  | 30 					return; | 
|  | 31 				} catch (MailException e2) { | 
|  | 32 					e = e2; | 
|  | 33 				} | 
|  | 34 			} | 
|  | 35 			throw new MailException("all smtp servers failed", e); | 
|  | 36 		} | 
|  | 37 	} | 
|  | 38 | 
|  | 39 	public void sendFrom(final Mail mail, final String smtpFrom) throws MailException { | 
|  | 40 		SmtpServer server = defaultServer; | 
|  | 41 		try { | 
|  | 42 			server.sendFrom(mail, smtpFrom); | 
|  | 43 			return; | 
|  | 44 		} catch (MailParseException e) { | 
|  | 45 			throw e; | 
|  | 46 		} catch (MailAddressException e) { | 
|  | 47 			throw e; | 
|  | 48 		} catch (MailEncodingException e) { | 
|  | 49 			throw e; | 
|  | 50 		} catch (MailException e) { | 
|  | 51 			for (SmtpServer fallbackServer : servers) { | 
|  | 52 				if (fallbackServer == server) continue; | 
|  | 53 				try { | 
|  | 54 					fallbackServer.sendFrom(mail, smtpFrom); | 
|  | 55 					defaultServer = fallbackServer; | 
|  | 56 					return; | 
|  | 57 				} catch (MailException e2) { | 
|  | 58 					e = e2; | 
|  | 59 				} | 
|  | 60 			} | 
|  | 61 			throw new MailException("all smtp servers failed", e); | 
|  | 62 		} | 
|  | 63 	} | 
|  | 64 | 
|  | 65 	public void useSsl() { | 
|  | 66 		for (SmtpServer server : servers) { | 
|  | 67 			server.useSsl(); | 
|  | 68 		} | 
|  | 69 	} | 
|  | 70 | 
|  | 71 	public void useStartTls() { | 
|  | 72 		for (SmtpServer server : servers) { | 
|  | 73 			server.useStartTls(); | 
|  | 74 		} | 
|  | 75 	} | 
|  | 76 | 
|  | 77 	public void setPort(int port) { | 
|  | 78 		for (SmtpServer server : servers) { | 
|  | 79 			server.setPort(port); | 
|  | 80 		} | 
|  | 81 	} | 
|  | 82 | 
|  | 83 	public void setDebug(boolean b) { | 
|  | 84 		for (SmtpServer server : servers) { | 
|  | 85 			server.setDebug(b); | 
|  | 86 		} | 
|  | 87 	} | 
|  | 88 | 
|  | 89 	public String getUserName() { | 
|  | 90 		return defaultServer.getUserName(); | 
|  | 91 	} | 
|  | 92 } |