(IMAPFolder folder)
| 1029 | * open folders. |
| 1030 | */ |
| 1031 | IMAPProtocol getProtocol(IMAPFolder folder) |
| 1032 | throws MessagingException { |
| 1033 | IMAPProtocol p = null; |
| 1034 | |
| 1035 | // keep looking for a connection until we get a good one |
| 1036 | while (p == null) { |
| 1037 | |
| 1038 | // New authenticated protocol objects are either acquired |
| 1039 | // from the connection pool, or created when the pool is |
| 1040 | // empty or no connections are available. None are available |
| 1041 | // if the current pool size is one and the separate store |
| 1042 | // property is set or the connection is in use. |
| 1043 | |
| 1044 | synchronized (pool) { |
| 1045 | |
| 1046 | // If there's none available in the pool, |
| 1047 | // create a new one. |
| 1048 | if (pool.authenticatedConnections.isEmpty() || |
| 1049 | (pool.authenticatedConnections.size() == 1 && |
| 1050 | (pool.separateStoreConnection || pool.storeConnectionInUse))) { |
| 1051 | |
| 1052 | logger.fine("no connections in the pool, creating a new one"); |
| 1053 | try { |
| 1054 | if (forcePasswordRefresh) |
| 1055 | refreshPassword(); |
| 1056 | // Use cached host, port and timeout values. |
| 1057 | p = newIMAPProtocol(host, port); |
| 1058 | p.addResponseHandler(nonStoreResponseHandler); |
| 1059 | // Use cached auth info |
| 1060 | login(p, user, password); |
| 1061 | p.removeResponseHandler(nonStoreResponseHandler); |
| 1062 | } catch(Exception ex1) { |
| 1063 | if (p != null) |
| 1064 | try { |
| 1065 | p.disconnect(); |
| 1066 | } catch (Exception ex2) { } |
| 1067 | p = null; |
| 1068 | eu.faircode.email.Log.e(new MessagingException("IMAP connection failure", ex1)); |
| 1069 | throw new MessagingException("connection failure", ex1); |
| 1070 | } |
| 1071 | |
| 1072 | if (p == null) |
| 1073 | throw new MessagingException("connection failure"); |
| 1074 | } else { |
| 1075 | if (logger.isLoggable(Level.FINE)) |
| 1076 | logger.fine("connection available -- size: " + |
| 1077 | pool.authenticatedConnections.size()); |
| 1078 | |
| 1079 | // remove the available connection from the Authenticated queue |
| 1080 | p = pool.authenticatedConnections.lastElement(); |
| 1081 | pool.authenticatedConnections.removeElement(p); |
| 1082 | |
| 1083 | // check if the connection is still live |
| 1084 | long lastUsed = System.currentTimeMillis() - p.getTimestamp(); |
| 1085 | if (lastUsed > pool.serverTimeoutInterval) { |
| 1086 | try { |
| 1087 | /* |
| 1088 | * Swap in a special response handler that will handle |
no test coverage detected