java.lang.Object | +--javax.mail.Service
An abstract class that contains the functionality common to messaging services, such as stores and transports.
A messaging service is created from aSession
and is named using aURLName
. A service must be connected before it can be used. Connection events are sent to reflect its connection status.
debug Debug flag for this service. | |
session The session from which this service was created. | |
url The URLName of this service. |
Service(Session session,URLName urlname) Constructor. |
addConnectionListener(ConnectionListener l) Add a listener for Connection events on this service. | |
close() Close this service and terminate its connection. | |
connect() A generic connect method that takes no parameters. | |
connect(java.lang.String host, int port, java.lang.String user, java.lang.String password) Similar to connect(host, user, password) except a specific port can be specified. | |
connect(java.lang.String host, java.lang.String user, java.lang.String password) Connect to the specified address. | |
finalize() Stop the event dispatcher thread so the queue can be garbage collected. | |
getURLName() Return a URLName representing this service. | |
isConnected() Is this service currently connected? | |
notifyConnectionListeners(int type) Notify all ConnectionListeners. | |
protocolConnect(java.lang.String host, int port, java.lang.String user, java.lang.String password) The service implementation should override this method to perform the actual protocol-specific connection attempt. | |
queueEvent(MailEvent event, java.util.Vector vector) Add the event and vector of listeners to the queue to be delivered. | |
removeConnectionListener(ConnectionListener l) Remove a Connection event listener. | |
setConnected(boolean connected) Set the connection state of this service. | |
setURLName(URLName url) Set the URLName representing this service. | |
toString() Return getURLName.toString() if this service has a URLName, otherwise it will return the defaulttoString . |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
protectedSessionsession
protectedURLNameurl
URLName
of this service.protected booleandebug
protectedService(Session session,URLName urlname)
session
- Session object for this serviceurl
- URLName object to be used for this servicepublic voidconnect() throwsMessagingException
If the connection is successful, an "open"ConnectionEvent
is delivered to anyConnectionListeners
on this service.
Most clients should just call this method to connect to the service.
It is an error to connect to an already connected service.
The implementation provided here simply calls the followingconnect(String, String, String)
method with nulls.
AuthenticationFailedException
- for authentication failuresMessagingException
- for other failuresjava.lang.IllegalStateException
- if the service is already connectedConnectionEvent
public voidconnect(java.lang.String host, java.lang.String user, java.lang.String password) throwsMessagingException
If the connection is successful, an "open"ConnectionEvent
is delivered to anyConnectionListeners
on this service.
It is an error to connect to an already connected service.
The implementation in the Service class will collect defaults for the host, user, and password from the session, from theURLName
for this service, and from the supplied parameters and then call theprotocolConnect
method. If theprotocolConnect
method returnsfalse
, the user will be prompted for any missing information and theprotocolConnect
method will be called again. The subclass should override theprotocolConnect
method. The subclass should also implement thegetURLName
method, or use the implementation in this class.
On a successful connection, thesetURLName
method is called with a URLName that includes the information used to make the connection, including the password.
If the password passed in is null and this is the first successful connection to this service, the user name and the password collected from the user will be saved as defaults for subsequent connection attempts to this same service. If the password passed in is not null, it is not saved, on the assumption that the application is managing passwords explicitly.
host
- the host to connect touser
- the user namepassword
- this user's passwordAuthenticationFailedException
- for authentication failuresMessagingException
- for other failuresjava.lang.IllegalStateException
- if the service is already connectedConnectionEvent
public voidconnect(java.lang.String host, int port, java.lang.String user, java.lang.String password) throwsMessagingException
host
- the host to connect toport
- the port to connect to (-1 means the default port)user
- the user namepassword
- this user's passwordAuthenticationFailedException
- for authentication failuresMessagingException
- for other failuresjava.lang.IllegalStateException
- if the service is already connectedconnect(java.lang.String, java.lang.String, java.lang.String)
,ConnectionEvent
protected booleanprotocolConnect(java.lang.String host, int port, java.lang.String user, java.lang.String password) throwsMessagingException
connect
method calls this method as needed. TheprotocolConnect
method should returnfalse
if a user name or password is required for authentication but the corresponding parameter is null; theconnect
method will prompt the user when needed to supply missing information. This method should also returnfalse
if authentication fails for the supplied user name or password.
TheprotocolConnect
method should throw an exception to report failures not related to authentication, such as an invalid host name or port number, loss of a connection during the authentication process, unavailability of the server, etc.
host
- the name of the host to connect toport
- the port to use (-1 means use default port)user
- the name of the user to login aspassword
- the user's passwordMessagingException
- for non-authentication failurespublic booleanisConnected()
This implementation uses a private boolean field to store the connection state. This method returns the value of that field.
Subclasses may want to override this method to verify that any connection to the message store is still alive.
protected voidsetConnected(boolean connected)
connect
andclose
methods. Subclasses will need to call this method to set the state if the service was automatically disconnected. The implementation in this class merely sets the private field returned by theisConnected
method.
connected
- true if the service is connected, false if it is not connectedpublic voidclose() throwsMessagingException
This implementation usessetConnected(false)
to set this service's connected state tofalse
. It will then send a close ConnectionEvent to any registered ConnectionListeners. Subclasses overriding this method to do implementation specific cleanup should call this method as a last step to insure event notification, probably by including a call tosuper.close()
in afinally
clause.
MessagingException
- for errors while closingConnectionEvent
publicURLNamegetURLName()
Subclasses should only override this method if their URLName does not follow the standard format.
The implementation in the Service class returns (usually a copy of) theurl
field with the password and file information stripped out.
URLName
protected voidsetURLName(URLName url)
url
field after a service has successfully connected. Subclasses should only override this method if their URL does not follow the standard format. In particular, subclasses should override this method if their URL does not require all the possible fields supported byURLName
; a newURLName
should be constructed with any unneeded fields removed.
The implementation in the Service class simply sets theurl
field.
URLName
public voidaddConnectionListener(ConnectionListener l)
The default implementation provided here adds this listener to an internal list of ConnectionListeners.
l
- the Listener for Connection eventsConnectionEvent
public voidremoveConnectionListener(ConnectionListener l)
The default implementation provided here removes this listener from the internal list of ConnectionListeners.
l
- the listeneraddConnectionListener(javax.mail.event.ConnectionListener)
protected voidnotifyConnectionListeners(int type)
The provided default implementation queues the event into an internal event queue. An event dispatcher thread dequeues events from the queue and dispatches them to the registered ConnectionListeners. Note that the event dispatching occurs in a separate thread, thus avoiding potential deadlock problems.
public java.lang.StringtoString()
getURLName.toString()
if this service has a URLName, otherwise it will return the defaulttoString
.toString
in classjava.lang.Object
protected voidqueueEvent(MailEvent event, java.util.Vector vector)
protected voidfinalize() throws java.lang.Throwable
finalize
in classjava.lang.Object