FileDocCategorySizeDatePackage
POP3Store.javaAPI DocJavaMail 1.4.311608Tue Nov 17 10:38:12 GMT 2009com.sun.mail.pop3

POP3Store

public class POP3Store extends Store
A POP3 Message Store. Contains only one folder, "INBOX". See the com.sun.mail.pop3 package documentation for further information on the POP3 protocol provider.

author
Bill Shannon
author
John Mani

Fields Summary
private String
name
private int
defaultPort
private boolean
isSSL
private Protocol
port
private POP3Folder
portOwner
private String
host
private int
portNum
private String
user
private String
passwd
private boolean
useStartTLS
private boolean
requireStartTLS
private Map
capabilities
volatile Constructor
messageConstructor
volatile boolean
rsetBeforeQuit
volatile boolean
disableTop
volatile boolean
forgetTopHeaders
volatile boolean
supportsUidl
Constructors Summary
public POP3Store(Session session, URLName url)


         
	this(session, url, "pop3", false);
    
public POP3Store(Session session, URLName url, String name, boolean isSSL)

	super(session, url);
	if (url != null)
	    name = url.getProtocol();
	this.name = name;
	if (!isSSL)
	    isSSL = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".ssl.enable", false);
	if (isSSL)
	    this.defaultPort = 995;
	else
	    this.defaultPort = 110;
	this.isSSL = isSSL;

	rsetBeforeQuit = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".rsetbeforequit", false);

	disableTop = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".disabletop", false);

	forgetTopHeaders = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".forgettopheaders", false);

	// mail.pop3.starttls.enable enables use of STLS command
	useStartTLS = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".starttls.enable", false);

	// mail.pop3.starttls.required requires use of STLS command
	requireStartTLS = PropUtil.getBooleanSessionProperty(session,
				"mail." + name + ".starttls.required", false);

	String s = session.getProperty("mail." + name + ".message.class");
	if (s != null) {
	    if (session.getDebug())
		session.getDebugOut().println(
		    "DEBUG POP3: message class: " + s);
	    try {
		ClassLoader cl = this.getClass().getClassLoader();

		// now load the class
		Class messageClass = null;
		try {
		    // First try the "application's" class loader.
		    // This should eventually be replaced by
		    // Thread.currentThread().getContextClassLoader().
		    messageClass = Class.forName(s, false, cl);
		} catch (ClassNotFoundException ex1) {
		    // That didn't work, now try the "system" class loader.
		    // (Need both of these because JDK 1.1 class loaders
		    // may not delegate to their parent class loader.)
		    messageClass = Class.forName(s);
		}

		Class[] c = {javax.mail.Folder.class, int.class};
		messageConstructor = messageClass.getConstructor(c);
	    } catch (Exception ex) {
		if (session.getDebug())
		    session.getDebugOut().println(
			"DEBUG POP3: failed to load message class: " + ex);
	    }
	}
    
Methods Summary
public java.util.Mapcapabilities()
Return a Map of the capabilities the server provided, as per RFC 2449. If the server doesn't support RFC 2449, an emtpy Map is returned. The returned Map can not be modified. The key to the Map is the upper case capability name as a String. The value of the entry is the entire String capability line returned by the server.

For example, to check if the server supports the STLS capability, use: if (store.capabilities().containsKey("STLS")) ...

return
Map of capabilities
since
JavaMail 1.4.3

	Map c = capabilities;	// atomic read of pointer
	if (c != null)
	    return Collections.unmodifiableMap(c);
	else
	    return Collections.emptyMap();
    
private voidcheckConnected()

	if (!super.isConnected())
	    throw new MessagingException("Not connected");
    
public synchronized voidclose()

	try {
	    if (port != null)
		port.quit();
	} catch (IOException ioex) {
	} finally {
	    port = null;

	    // to set the state and send the closed connection event
	    super.close();
	}
    
synchronized voidclosePort(com.sun.mail.pop3.POP3Folder owner)

	if (portOwner == owner) {
	    port = null;
	    portOwner = null;
	}
    
protected voidfinalize()

	super.finalize();

	if (port != null)	// don't force a connection attempt
	    close();
    
public javax.mail.FoldergetDefaultFolder()

	checkConnected();
	return new DefaultFolder(this);
    
public javax.mail.FoldergetFolder(javax.mail.URLName url)

	checkConnected();
	return new POP3Folder(this, url.getFile());
    
public javax.mail.FoldergetFolder(java.lang.String name)
Only the name "INBOX" is supported.

	checkConnected();
	return new POP3Folder(this, name);
    
synchronized com.sun.mail.pop3.ProtocolgetPort(com.sun.mail.pop3.POP3Folder owner)

	Protocol p;

	// if we already have a port, remember who's using it
	if (port != null && portOwner == null) {
	    portOwner = owner;
	    return port;
	}

	// need a new port, create it and try to login
	p = new Protocol(host, portNum, session.getDebug(),
	    session.getDebugOut(), session.getProperties(), "mail." + name,
	    isSSL);

	if (useStartTLS || requireStartTLS) {
	    if (p.hasCapability("STLS")) {
		p.stls();
		// refresh capabilities
		p.setCapabilities(p.capa());
	    } else if (requireStartTLS) {
		if (debug)
		    session.getDebugOut().println(
			"DEBUG POP3: STLS required but not supported");
		try {
		    p.quit();
		} catch (IOException ioex) {
		} finally {
		    throw new EOFException("STLS required but not supported");
		}
	    }
	}

	capabilities = p.getCapabilities();	// save for later, may be null

	/*
	 * If we haven't explicitly disabled use of the TOP command,
	 * and the server has provided its capabilities,
	 * and the server doesn't support the TOP command,
	 * disable the TOP command.
	 */
	if (!disableTop &&
		capabilities != null && capabilities.containsKey("TOP")) {
	    disableTop = true;
	    session.getDebugOut().println(
		    "DEBUG POP3: server doesn't support TOP, disabling it");
	}

	supportsUidl = capabilities == null || capabilities.containsKey("UIDL");

	String msg = null;
	if ((msg = p.login(user, passwd)) != null) {
	    try {
		p.quit();
	    } catch (IOException ioex) {
	    } finally {
		throw new EOFException(msg);
	    }
	}

	/*
	 * If a Folder closes the port, and then a Folder
	 * is opened, the Store won't have a port.  In that
	 * case, the getPort call will come from Folder.open,
	 * but we need to keep track of the port in the Store
	 * so that a later call to Folder.isOpen, which calls
	 * Store.isConnected, will use the same port.
	 */
	if (port == null && owner != null) {
	    port = p;
	    portOwner = owner;
	}
	if (portOwner == null)
	    portOwner = owner;
	return p;
    
public synchronized booleanisConnected()
Check whether this store is connected. Override superclass method, to actually ping our server connection.

	if (!super.isConnected())
	    // if we haven't been connected at all, don't bother with
	    // the NOOP.
	    return false;
	try {
	    if (port == null)
		port = getPort(null);
	    else if (!port.noop())
		throw new IOException("NOOP failed");
	    return true;
	} catch (IOException ioex) {
	    // no longer connected, close it down
	    try {
		super.close();		// notifies listeners
	    } catch (MessagingException mex) {
		// ignore it
	    } finally {
		return false;
	    }
	}
    
protected synchronized booleanprotocolConnect(java.lang.String host, int portNum, java.lang.String user, java.lang.String passwd)

		    
	// check for non-null values of host, password, user
	if (host == null || passwd == null || user == null)
	    return false;

	// if port is not specified, set it to value of mail.pop3.port
        // property if it exists, otherwise default to 110
        if (portNum == -1)
	    portNum = PropUtil.getIntSessionProperty(session,
				"mail." + name + ".port", -1);

	if (portNum == -1)
	    portNum = defaultPort;

	this.host = host;
	this.portNum = portNum;
	this.user = user;
	this.passwd = passwd;
	try {
	    port = getPort(null);
	} catch (EOFException eex) { 
		throw new AuthenticationFailedException(eex.getMessage());
	} catch (IOException ioex) { 
	    throw new MessagingException("Connect failed", ioex);
	}

	return true;