org.subethamail.smtp.server
Class SMTPServer

java.lang.Object
  extended by org.subethamail.smtp.server.SMTPServer
All Implemented Interfaces:
java.lang.Runnable

public class SMTPServer
extends java.lang.Object
implements java.lang.Runnable

Main SMTPServer class. Construct this object, set the hostName, port, and bind address if you wish to override the defaults, and call start(). This class starts opens a ServerSocket and creates a new instance of the ConnectionHandler class when a new connection comes in. The ConnectionHandler then parses the incoming SMTP stream and hands off the processing to the CommandHandler which will execute the appropriate SMTP command class. This class also manages a watchdog thread which will timeout stale connections. There are two ways of using this server. The first is to construct with a MessageHandlerFactory. This provides the lowest-level and most flexible access. The second way is to construct with a collection of MessageListeners. This is a higher, and sometimes more convenient level of abstraction. In neither case is the SMTP server (this library) responsible for deciding what recipients to accept or what to do with the incoming data. That is left to you.

Author:
Jon Stevens, Ian McFarland <ian@neo.com>, Jeff Schnitzer

Constructor Summary
SMTPServer(java.util.Collection<MessageListener> listeners)
          A convenience constructor that splits the smtp data among multiple listeners (and multiple recipients).
SMTPServer(MessageHandlerFactory handlerFactory)
          The primary constructor.
 
Method Summary
protected  java.net.ServerSocket createServerSocket()
          Override this method if you want to create your own server sockets.
 int getBacklog()
          The backlog is the Socket backlog.
 java.net.InetAddress getBindAddress()
          null means all interfaces
 CommandHandler getCommandHandler()
          The CommandHandler manages handling the SMTP commands such as QUIT, MAIL, RCPT, DATA, etc.
protected  java.lang.ThreadGroup getConnectionGroup()
           
 int getConnectionTimeout()
           
 java.lang.String getHostName()
           
 int getMaxConnections()
           
 int getMaxRecipients()
           
 MessageHandlerFactory getMessageHandlerFactory()
          All smtp data is eventually routed through the handlers.
 java.lang.String getName()
           
 java.lang.String getNameVersion()
           
 int getNumberOfConnections()
           
 int getPort()
           
 boolean hasTooManyConnections()
           
 boolean isRunning()
          Is the server running after start() has been called?
 void run()
          This method is called by this thread when it starts up.
 void setBacklog(int backlog)
          The backlog is the Socket backlog.
 void setBindAddress(java.net.InetAddress bindAddress)
          null means all interfaces
 void setConnectionTimeout(int connectionTimeout)
           
 void setHostName(java.lang.String hostName)
          The host name that will be reported to SMTP clients
 void setMaxConnections(int maxConnections)
          Set's the maximum number of connections this server instance will accept.
 void setMaxRecipients(int maxRecipients)
           
 void setPort(int port)
           
protected  void shutDownOpenConnections()
          Grabs all ThreadGroup instances of ConnectionHander's and attempts to close the socket if it is still open.
 void start()
          Call this method to get things rolling after instantiating the SMTPServer.
 void stop()
          Shut things down gracefully.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SMTPServer

public SMTPServer(MessageHandlerFactory handlerFactory)
The primary constructor.


SMTPServer

public SMTPServer(java.util.Collection<MessageListener> listeners)
A convenience constructor that splits the smtp data among multiple listeners (and multiple recipients).

Method Detail

getHostName

public java.lang.String getHostName()
Returns:
the host name that will be reported to SMTP clients

setHostName

public void setHostName(java.lang.String hostName)
The host name that will be reported to SMTP clients


getBindAddress

public java.net.InetAddress getBindAddress()
null means all interfaces


setBindAddress

public void setBindAddress(java.net.InetAddress bindAddress)
null means all interfaces


getPort

public int getPort()

setPort

public void setPort(int port)

isRunning

public boolean isRunning()
Is the server running after start() has been called?


getBacklog

public int getBacklog()
The backlog is the Socket backlog. The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.

Returns:
the backlog

setBacklog

public void setBacklog(int backlog)
The backlog is the Socket backlog. The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.


start

public void start()
Call this method to get things rolling after instantiating the SMTPServer.


stop

public void stop()
Shut things down gracefully.


shutDownOpenConnections

protected void shutDownOpenConnections()
Grabs all ThreadGroup instances of ConnectionHander's and attempts to close the socket if it is still open.


createServerSocket

protected java.net.ServerSocket createServerSocket()
                                            throws java.io.IOException
Override this method if you want to create your own server sockets. You must return a bound ServerSocket instance

Throws:
java.io.IOException

run

public void run()
This method is called by this thread when it starts up.

Specified by:
run in interface java.lang.Runnable

getName

public java.lang.String getName()

getNameVersion

public java.lang.String getNameVersion()

getMessageHandlerFactory

public MessageHandlerFactory getMessageHandlerFactory()
All smtp data is eventually routed through the handlers.


getCommandHandler

public CommandHandler getCommandHandler()
The CommandHandler manages handling the SMTP commands such as QUIT, MAIL, RCPT, DATA, etc.

Returns:
An instance of CommandHandler

getConnectionGroup

protected java.lang.ThreadGroup getConnectionGroup()

getNumberOfConnections

public int getNumberOfConnections()

hasTooManyConnections

public boolean hasTooManyConnections()

getMaxConnections

public int getMaxConnections()

setMaxConnections

public void setMaxConnections(int maxConnections)
Set's the maximum number of connections this server instance will accept.

Parameters:
maxConnections -

getConnectionTimeout

public int getConnectionTimeout()

setConnectionTimeout

public void setConnectionTimeout(int connectionTimeout)

getMaxRecipients

public int getMaxRecipients()

setMaxRecipients

public void setMaxRecipients(int maxRecipients)