de.trantor.mail
Class Connection

java.lang.Object
  |
  +--de.trantor.mail.Connection
Direct Known Subclasses:
ConnectionImpl, ConnectionImpl

public abstract class Connection
extends java.lang.Object

Is the abstract base class for socket connections used inside the SMTP, POP3 and IMAP protocols of the mail package. The rationale for using this class is the difference in how networking is handled in J2ME and J2SE: While J2SE has java.net.Socket, J2ME uses the Generic Connection Framework with its javax.io.Connector class as a central means to open sockets. Unfortunately, both methods are totally incompatible with each other, so a common abstraction has to be found to make the mail package work in both environments.

The Connection class is such an abstraction. It provides abstract versions of methods that open and close sockets, read from them and write to them. It also provides a static factory method that is able to instantiate one of two sub-classes of Connection that match the different run-time environments. These sub-classes are both called ConnectionImpl, and they are loaded "by name" from the j2me or j2se packages, respectively. This is the only way to get rid of compile-time dependencies on these classes.


Field Summary
protected  java.io.InputStream input
          Holds the input stream attached to the socket.
protected  java.io.OutputStream output
          Holds the output stream attached to the socket.
 
Constructor Summary
Connection()
           
 
Method Summary
 void close()
          Closes a connection.
 boolean connected()
          Queries the current status of the connection.
 boolean getDebug()
          Queries the current value of the debugging flag.
static Connection getInstance()
          Creates a new, concrete connection object for either J2SE or J2SE, depending on the current run-time environment.
protected abstract  void open(java.lang.String host, int port, boolean ssl)
          Opens a connection.
 java.lang.String receive()
          Receives a string from the server.
 void send(java.lang.String s)
          Sends a string to the server.
 void setDebug(boolean debug)
          Controls the output of debugging information to standard output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

input

protected java.io.InputStream input
Holds the input stream attached to the socket.

output

protected java.io.OutputStream output
Holds the output stream attached to the socket.
Constructor Detail

Connection

public Connection()
Method Detail

open

protected abstract void open(java.lang.String host,
                             int port,
                             boolean ssl)
                      throws java.io.IOException
Opens a connection. Concrete descendents of Connection override this method to actually do something useful.

close

public void close()
           throws java.io.IOException
Closes a connection. Concrete descendents of Connection override this method to actually do something useful.

getInstance

public static Connection getInstance()
Creates a new, concrete connection object for either J2SE or J2SE, depending on the current run-time environment.

send

public void send(java.lang.String s)
          throws java.io.IOException
Sends a string to the server. This method is used internally for all outgoing communication to the server. The main thing it does it terminate the line with a CR/LF. If there are occurences of CR or LF inside the string, the method ensures that proper CR/LF sequences are sent for them, since this is what most internet protocols expect.
See Also:
receive()

receive

public java.lang.String receive()
                         throws java.io.IOException
Receives a string from the server. This method is used internally for incoming communication from the server. The main thing it does is ensuring that only complete lines are returned to the application, that is, lines that were terminated at least by a CR. LFs are ignored completely. Neither CRs nor LFs are returned as part of the result.
See Also:
send(java.lang.String)

setDebug

public void setDebug(boolean debug)
Controls the output of debugging information to standard output. Set it to true to see all protocol information exchanged.
See Also:
getDebug()

getDebug

public boolean getDebug()
Queries the current value of the debugging flag.
See Also:
setDebug(boolean)

connected

public boolean connected()
Queries the current status of the connection.