Rev 1453 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1452 | jmachado | 1 | package pt.estgp.estgweb.services.email.impl; |
2 | |||
3 | import org.apache.log4j.Logger; |
||
4 | import pt.estgp.estgweb.Globals; |
||
5 | |||
6 | import javax.mail.Transport; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.List; |
||
9 | |||
10 | /** |
||
11 | * Created by jorgemachado on 13/06/16. |
||
12 | */ |
||
13 | public class EmailTransportPool |
||
14 | { |
||
15 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(EmailTransportPool.class); |
||
16 | |||
17 | private List<EmailTransport> pool = new ArrayList<EmailTransport>(); |
||
18 | |||
19 | //SINGLETON INSTANCE |
||
20 | static EmailTransportPool instance = new EmailTransportPool(); |
||
21 | |||
22 | public static EmailTransportPool getInstance(){ |
||
23 | return instance; |
||
24 | } |
||
25 | |||
26 | 1.5.0/docs/api/java/lang/String.html">String[] serverEmails; |
||
27 | |||
28 | private EmailTransportPool() |
||
29 | { |
||
30 | serverEmails = new 1.5.0/docs/api/java/lang/String.html">String[Globals.EMAIL_SERVER_USER_COUNT]; |
||
31 | for(int i = 1;i<= Globals.EMAIL_SERVER_USER_COUNT;i++) |
||
32 | { |
||
33 | serverEmails[i-1] = Globals.EMAIL_SERVER_USER_PREFIX + i + Globals.EMAIL_SERVER_USER_SUFIX; |
||
34 | } |
||
35 | |||
36 | if(Globals.SYSTEM_EMAIL_USE_ROUND_ROBIN) |
||
37 | { |
||
38 | for(1.5.0/docs/api/java/lang/String.html">String emailFrom: serverEmails) |
||
39 | { |
||
40 | 1.5.0/docs/api/java/lang/String.html">String pass = Globals.EMAIL_SERVER_PASSWORD; |
||
41 | logger.info("POOL: Adding to POOL " + emailFrom + " with pass: " + pass); |
||
42 | createTransport(emailFrom,emailFrom,pass); |
||
43 | } |
||
44 | } |
||
45 | else |
||
46 | { |
||
47 | logger.info("POOL: Will use only system mail box"); |
||
48 | createTransport(Globals.SYSTEM_EMAIL_BOX,Globals.SYSTEM_EMAIL_BOX,Globals.EMAIL_SERVER_PASSWORD); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | private void createTransport(1.5.0/docs/api/java/lang/String.html">String identifier, 1.5.0/docs/api/java/lang/String.html">String emailFrom, 1.5.0/docs/api/java/lang/String.html">String emailServerPassword) |
||
53 | { |
||
54 | EmailTransport emailTransport = new EmailTransport(identifier,emailFrom,emailServerPassword); |
||
55 | pool.add(emailTransport); |
||
56 | } |
||
57 | |||
58 | int indexPool = -1; |
||
59 | |||
60 | public EmailTransport getAvailableTransportFromPool() |
||
61 | { |
||
62 | int tries = pool.size(); |
||
63 | while(tries > 0) |
||
64 | { |
||
65 | tries--; |
||
66 | |||
67 | indexPool++; |
||
68 | |||
69 | if(indexPool >= pool.size()) |
||
70 | indexPool = 0; |
||
71 | |||
72 | EmailTransport emailTransport = pool.get(indexPool); |
||
73 | indexPool++; |
||
74 | Transport transport = emailTransport.getEmailTransport(); |
||
75 | if(transport.isConnected()) |
||
76 | { |
||
77 | return emailTransport; |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | logger.warn("EMAIL POOL: Transport (" + emailTransport.getIdentifier() + ") is not connected, will try another"); |
||
82 | } |
||
83 | } |
||
84 | logger.warn("EMAIL POOL: Transport (There are no available transports)"); |
||
85 | return null; |
||
86 | } |
||
87 | |||
88 | } |