/** * Constructor for debugging and testing. */ MessageCache(int size, boolean debug) { this.folder = null; logger = new MailLogger( this.getClass(), "messagecache", "DEBUG IMAP MC", debug, System.out); if (logger.isLoggable(Level.CONFIG)) logger.config("create DEBUG cache of size " + size); ensureCapacity(size, 1); }
/** * Report whether the connection pool is full. */ boolean isConnectionPoolFull() { synchronized (pool) { if (pool.logger.isLoggable(Level.FINE)) pool.logger.fine("connection pool current size: " + pool.authenticatedConnections.size() + " pool size: " + pool.poolSize); return (pool.authenticatedConnections.size() >= pool.poolSize); } }
/** * Abort an IDLE command. While one thread is blocked in * readIdleResponse(), another thread will use this method * to abort the IDLE command, which will cause the server * to send the closing tag for the IDLE command, which * readIdleResponse() and processIdleResponse() will see * and terminate the IDLE state. * * @since JavaMail 1.4.1 */ public void idleAbort() { OutputStream os = getOutputStream(); try { os.write(DONE); os.flush(); } catch (Exception ex) { // nothing to do, hope to detect it again later logger.log(Level.FINEST, "Exception aborting IDLE", ex); } }
if (noauthdebug && isTracing()) { logger.fine("AUTHENTICATE PLAIN command trace suppressed"); suspendTracing(); tag = writeCommand("AUTHENTICATE PLAIN", null); } catch (Exception ex) { r = Response.byeResponse(ex); done = true; try { r = readResponse(); if (r.isContinuation()) { b64os.write(ASCIIUtility.getBytes(s)); b64os.flush(); // complete the encoding os.write(bos.toByteArray()); // write out line os.flush(); // flush the stream } else if (r.isTagged() && r.getTag().equals(tag)) if (noauthdebug && isTracing()) logger.fine("AUTHENTICATE PLAIN command result: " + r); handleLoginResult(r);
String authzid = getAuthorizationId(); if (authzid == null) authzid = user; if (enableSASL) { logger.fine("Authenticate with SASL"); try { if (sasllogin(getSASLMechanisms(), getSASLRealm(), authzid, user, passwd)) { return true; // success } else { logger.fine("SASL authentication failed"); return false; logger.log(Level.FINE, "SASL support failed", ex); if (logger.isLoggable(Level.FINE)) logger.fine("Attempt to authenticate using mechanisms: " + mechs); StringTokenizer st = new StringTokenizer(mechs); while (st.hasMoreTokens()) { String m = st.nextToken(); m = m.toUpperCase(Locale.ENGLISH); Authenticator a = authenticators.get(m); if (a == null) { logger.log(Level.FINE, "no authenticator for mechanism {0}", m); continue; logger.log(Level.FINE, "mechanism {0} not supported by server", m);
this, name, session.getProperties(), logger, serviceHost }); } catch (Exception ex) { logger.log(Level.FINE, "Can't load SASL authenticator", ex); if (supportsAuthentication(allowed[i])) // XXX - case must match v.add(allowed[i]); } else { String a = extMap.get("AUTH"); if (a != null) { StringTokenizer st = new StringTokenizer(a); while (st.hasMoreTokens()) v.add(st.nextToken()); if (noauthdebug && isTracing()) { logger.fine("SASL AUTH command trace suppressed"); suspendTracing();
/** * Get the list of SASL mechanisms to consider if SASL authentication * is enabled. If the list is empty or null, all available SASL mechanisms * are considered. * * @return the array of SASL mechanisms to consider * * @since JavaMail 1.4.4 */ public synchronized String[] getSASLMechanisms() { if (saslMechanisms == UNKNOWN_SA) { List<String> v = new ArrayList<String>(5); String s = session.getProperty("mail." + name + ".sasl.mechanisms"); if (s != null && s.length() > 0) { if (logger.isLoggable(Level.FINE)) logger.fine("SASL mechanisms allowed: " + s); StringTokenizer st = new StringTokenizer(s, " ,"); while (st.hasMoreTokens()) { String m = st.nextToken(); if (m.length() > 0) v.add(m); } } saslMechanisms = new String[v.size()]; v.toArray(saslMechanisms); } if (saslMechanisms == null) return null; return saslMechanisms.clone(); }
/** * Does the server we're connected to support the specified * authentication mechanism? Uses the extension information * returned by the server from the EHLO command. * * @param auth the authentication mechanism * @return true if the authentication mechanism is supported * * @since JavaMail 1.4.1 */ protected boolean supportsAuthentication(String auth) { assert Thread.holdsLock(this); if (extMap == null) return false; String a = extMap.get("AUTH"); if (a == null) return false; StringTokenizer st = new StringTokenizer(a); while (st.hasMoreTokens()) { String tok = st.nextToken(); if (tok.equalsIgnoreCase(auth)) return true; } // hack for buggy servers that advertise capability incorrectly if (auth.equalsIgnoreCase("LOGIN") && supportsExtension("AUTH=LOGIN")) { logger.fine("use AUTH=LOGIN hack"); return true; } return false; }
StringTokenizer tuples = new StringTokenizer(currLine,";"); while (tuples.hasMoreTokens()) { String currTuple = tuples.nextToken().trim(); || protocol.length() <= 0 || className.length() <= 0) { logger.log(Level.CONFIG, "Bad provider entry: {0}", currLine); continue; addProvider(provider);
/** * Release the protocol object back to the connection pool. */ void releaseProtocol(IMAPFolder folder, IMAPProtocol protocol) { synchronized (pool) { if (protocol != null) { // If the pool is not full, add the store as a response handler // and return the protocol object to the connection pool. if (!isConnectionPoolFull()) { protocol.addResponseHandler(this); pool.authenticatedConnections.addElement(protocol); if (logger.isLoggable(Level.FINE)) logger.fine( "added an Authenticated connection -- size: " + pool.authenticatedConnections.size()); } else { logger.fine( "pool is full, not adding an Authenticated connection"); try { protocol.logout(); } catch (ProtocolException pex) {}; } } if (pool.folders != null) pool.folders.removeElement(folder); timeoutConnections(); } }
/** * Empty the connection pool. */ private void emptyConnectionPool(boolean force) { synchronized (pool) { for (int index = pool.authenticatedConnections.size() - 1; index >= 0; --index) { try { IMAPProtocol p = pool.authenticatedConnections.elementAt(index); p.removeResponseHandler(this); if (force) p.disconnect(); else p.logout(); } catch (ProtocolException pex) {}; } pool.authenticatedConnections.removeAllElements(); } pool.logger.fine("removed all authenticated connections from pool"); }
pool.authenticatedConnections.size() > 1) { if (pool.logger.isLoggable(Level.FINE)) { pool.logger.fine("checking for connections to prune: " + (System.currentTimeMillis() - pool.lastTimePruned)); pool.logger.fine("clientTimeoutInterval: " + pool.clientTimeoutInterval); for (int index = pool.authenticatedConnections.size() - 1; index > 0; index--) { p = pool.authenticatedConnections. elementAt(index); if (pool.logger.isLoggable(Level.FINE)) pool.logger.fine("protocol last used: " + (System.currentTimeMillis() - p.getTimestamp())); if (System.currentTimeMillis() - p.getTimestamp() > pool.clientTimeoutInterval) { pool.logger.fine( "authenticated connection timed out, " + "logging out the connection"); p.removeResponseHandler(this); pool.authenticatedConnections.removeElementAt(index);
if (logger.isLoggable(Level.FINE)) logger.fine("protocolConnect returning false" + ", host=" + host + ", user=" + traceUser(user) + boolean poolEmpty; synchronized (pool) { poolEmpty = pool.authenticatedConnections.isEmpty(); if (logger.isLoggable(Level.FINE)) logger.fine("trying to connect to host \"" + host + "\", port " + port + ", isSSL " + isSSL); protocol = newIMAPProtocol(host, port); if (logger.isLoggable(Level.FINE)) logger.fine("protocolConnect login" + ", host=" + host + ", user=" + traceUser(user) + ", password=" + tracePassword(password)); protocol.addResponseHandler(nonStoreResponseHandler); login(protocol, user, password); protocol.removeResponseHandler(nonStoreResponseHandler); protocol.addResponseHandler(this); pool.authenticatedConnections.addElement(protocol);
isEmpty = pool.authenticatedConnections.isEmpty(); pool.logger.fine("close() - no connections "); cleanup(); return; pool.authenticatedConnections.removeElement(protocol); protocol.logout(); } catch (ProtocolException pex) { throw new MessagingException(pex.getMessage(), pex); } finally { releaseStoreProtocol(protocol);
port = serverSocket.getPort(); host = serverSocket.getInetAddress().getHostName(); if (logger.isLoggable(Level.FINE)) logger.fine("starting protocol to host \"" + host + "\", port " + port); initStreams(); if ((r = readServerResponse()) != 220) { serverSocket.close(); serverSocket = null; serverInput = null; lineInputStream = null; if (logger.isLoggable(Level.FINE)) logger.fine("got bad greeting from host \"" + host + "\", port: " + port + ", response: " + r + "\n"); throw new MessagingException( "Got bad greeting from SMTP host: " + host + ", port: " + port + ", response: " + r); } else { if (logger.isLoggable(Level.FINE)) logger.fine("protocol started to host \"" + host + "\", port: " + port + "\n");
Exception ex = r.getException(); if (ex instanceof InterruptedIOException && ((InterruptedIOException)ex). bytesTransferred == 0) { if (ex instanceof SocketTimeoutException) { logger.finest( "handleIdle: ignoring socket timeout"); logger.finest("handleIdle: interrupting IDLE"); IdleManager im = idleManager; if (im != null) { logger.finest( "handleIdle: request IdleManager to abort"); im.requestAbort(this); } else { logger.finest("handleIdle: abort IDLE"); protocol.idleAbort(); idleState = ABORTING; try { if (protocol == null || !protocol.processIdleResponse(r)) return false; // done done = false; } finally { if (done) { logger.finest("handleIdle: set to RUNNING"); idleState = RUNNING; idleManager = null;
lastServerResponse = serverResponse; lastReturnCode = -1; logger.log(Level.FINE, "EOF: {0}", serverResponse); return -1; } while (isNotLastLine(line)); logger.log(Level.FINE, "exception reading response", ioex); throw new MessagingException("Exception reading response", ioex); } catch (NumberFormatException nfe) { try { close(); } catch (MessagingException mex) { logger.log(Level.FINE, "close failed", mex); close(); } catch (MessagingException mex) { logger.log(Level.FINE, "close failed", mex); logger.log(Level.FINE, "bad server response: {0}", serverResponse);
waitIfIdle(); if (force) { logger.log(Level.FINE, "forcing folder {0} to close", fullName); if (protocol != null) protocol.disconnect(); } else if (((IMAPStore)store).isConnectionPoolFull()) { logger.fine( "pool is full, not adding an Authenticated connection"); protocol.close(); protocol.logout(); } else { throw new MessagingException(pex.getMessage(), pex); } finally {
throws MessagingException { if (die) // XXX - should be IllegalStateException? throw new MessagingException("IdleManager is not running"); if (!(folder instanceof IMAPFolder)) throw new MessagingException("Can only watch IMAP folders"); IMAPFolder ifolder = (IMAPFolder)folder; SocketChannel sc = ifolder.getChannel(); if (sc == null) throw new MessagingException("Folder is not using SocketChannels"); if (logger.isLoggable(Level.FINEST)) logger.log(Level.FINEST, "IdleManager watching {0}", folderName(ifolder)); if (logger.isLoggable(Level.FINEST)) logger.log(Level.FINEST, "IdleManager.watch startIdle failed for {0}", folderName(ifolder)); tries++; if (logger.isLoggable(Level.FINEST)) { if (tries > 0) logger.log(Level.FINEST, "IdleManager.watch startIdle succeeded for {0}" + " after " + tries + " tries", folderName(ifolder)); else logger.log(Level.FINEST, "IdleManager.watch startIdle succeeded for {0}", folderName(ifolder));