public abstract class Service
extends java.lang.Object
implements java.lang.AutoCloseable
A messaging service is created from a Session
and is
named using a URLName
. A service must be connected
before it can be used. Connection events are sent to reflect
its connection status.
Modifier and Type | Method and Description |
---|---|
void |
addConnectionListener(ConnectionListener l)
Add a listener for Connection events on this service.
|
void |
close()
Close this service and terminate its connection.
|
void |
connect()
A generic connect method that takes no parameters.
|
void |
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.
|
void |
connect(java.lang.String user,
java.lang.String password)
Connect to the current host using the specified username
and password.
|
void |
connect(java.lang.String host,
java.lang.String user,
java.lang.String password)
Connect to the specified address.
|
URLName |
getURLName()
Return a URLName representing this service.
|
boolean |
isConnected()
Is this service currently connected?
|
void |
removeConnectionListener(ConnectionListener l)
Remove a Connection event listener.
|
java.lang.String |
toString()
Return
getURLName.toString() if this service has a URLName,
otherwise it will return the default toString . |
public void connect() throws MessagingException
If the connection is successful, an "open" ConnectionEvent
is delivered to any ConnectionListeners
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 following
connect(String, String, String)
method with nulls.
AuthenticationFailedException
- for authentication failuresMessagingException
- for other failuresjava.lang.IllegalStateException
- if the service is already connectedConnectionEvent
public void connect(java.lang.String host, java.lang.String user, java.lang.String password) throws MessagingException
If the connection is successful, an "open" ConnectionEvent
is delivered to any ConnectionListeners
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 the
URLName
for this service, and from the supplied
parameters and then call the protocolConnect
method.
If the protocolConnect
method returns false
,
the user will be prompted for any missing information and the
protocolConnect
method will be called again. The
subclass should override the protocolConnect
method.
The subclass should also implement the getURLName
method, or use the implementation in this class.
On a successful connection, the setURLName
method is
called with a URLName that includes the information used to make
the connection, including the password.
If the username passed in is null, a default value will be chosen
as described above.
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 when using other Service object
instances (the connection information is typically always saved within
a particular Service object instance). The password is saved using the
Session method setPasswordAuthentication
. 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
,
Session.setPasswordAuthentication(javax.mail.URLName, javax.mail.PasswordAuthentication)
public void connect(java.lang.String user, java.lang.String password) throws MessagingException
connect(host, user, password)
method with null
for the host name.user
- the user namepassword
- this user's passwordAuthenticationFailedException
- for authentication failuresMessagingException
- for other failuresjava.lang.IllegalStateException
- if the service is already connectedConnectionEvent
,
Session.setPasswordAuthentication(javax.mail.URLName, javax.mail.PasswordAuthentication)
,
connect(java.lang.String, java.lang.String, java.lang.String)
public void connect(java.lang.String host, int port, java.lang.String user, java.lang.String password) throws MessagingException
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
public boolean isConnected()
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.
public void close() throws MessagingException
This implementation uses setConnected(false)
to set
this service's connected state to false
. 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 to super.close()
in a finally
clause.
close
in interface java.lang.AutoCloseable
MessagingException
- for errors while closingConnectionEvent
public URLName getURLName()
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)
the url
field with the password and file information
stripped out.
URLName
public void addConnectionListener(ConnectionListener l)
The default implementation provided here adds this listener to an internal list of ConnectionListeners.
l
- the Listener for Connection eventsConnectionEvent
public void removeConnectionListener(ConnectionListener l)
The default implementation provided here removes this listener from the internal list of ConnectionListeners.
l
- the listeneraddConnectionListener(javax.mail.event.ConnectionListener)
public java.lang.String toString()
getURLName.toString()
if this service has a URLName,
otherwise it will return the default toString
.toString
in class java.lang.Object
Copyright © 2010 - 2020 Adobe. All Rights Reserved