68
|
1 package fschmidt.util.mail;
|
|
2
|
|
3 // not thread safe
|
|
4 public class Pop3ServerPool implements Pop3Server {
|
|
5 private Pop3Server[] servers;
|
|
6 private int current = 0;
|
|
7
|
|
8 Pop3ServerPool(Pop3Server... servers) {
|
|
9 this.servers = servers;
|
|
10 setMailLimit(100);
|
|
11 }
|
|
12
|
|
13 public void setLeaveOnServer(boolean b) {
|
|
14 for (Pop3Server server : servers) {
|
|
15 server.setLeaveOnServer(b);
|
|
16 }
|
|
17 }
|
|
18
|
|
19 public void setMailLimit(int maxMailCount) {
|
|
20 for (Pop3Server server : servers) {
|
|
21 server.setMailLimit(maxMailCount);
|
|
22 }
|
|
23 }
|
|
24
|
|
25 // should be same for all
|
|
26 public String getUsername() {
|
|
27 return servers[current].getUsername();
|
|
28 }
|
|
29
|
|
30 public void useSsl() {
|
|
31 for (Pop3Server server : servers) {
|
|
32 server.useSsl();
|
|
33 }
|
|
34 }
|
|
35
|
|
36 public void setDebug(boolean b) {
|
|
37 for (Pop3Server server : servers) {
|
|
38 server.setDebug(b);
|
|
39 }
|
|
40 }
|
|
41
|
|
42 public MailIterator getMail() throws MailException {
|
|
43 MailException ex = null;
|
|
44 for (int failed = 0; failed < servers.length; failed++) {
|
|
45 try {
|
|
46 return mailIterator(servers[current].getMail());
|
|
47 } catch (MailException e) {
|
|
48 ex = e;
|
|
49 nextServer();
|
|
50 }
|
|
51 }
|
|
52 throw new MailException("all pop3 servers failed", ex);
|
|
53 }
|
|
54
|
|
55 private MailIterator mailIterator(final MailIterator itr) {
|
|
56 return new MailIterator() {
|
|
57 public boolean hasNext() throws MailException {
|
|
58 return itr.hasNext();
|
|
59 }
|
|
60 public Mail next() throws MailException {
|
|
61 return itr.next();
|
|
62 }
|
|
63 public void close() throws MailException {
|
|
64 try {
|
|
65 itr.close();
|
|
66 } finally {
|
|
67 nextServer(); // always switch to next server
|
|
68 }
|
|
69 }
|
|
70 };
|
|
71 }
|
|
72
|
|
73 private void nextServer() {
|
|
74 current = (current < servers.length - 1) ? current+1 : 0;
|
|
75 }
|
|
76
|
|
77 }
|