Class MailEventSource
- java.lang.Object
-
- de.aristaflow.adept2.core.eventmanager.common.AbstractEventSource<MailEvent>
-
- de.aristaflow.adept2.core.eventmanager.mailevents.MailEventSource
-
- All Implemented Interfaces:
EventSource<MailEvent>
,MailSource
,PollingMailSource
,PollingSource<MailEvent>
,Runnable
- Direct Known Subclasses:
FilteredMailEventSource
,MailEventActivitySource
public class MailEventSource extends AbstractEventSource<MailEvent> implements PollingMailSource
A source for events based on mails in a specific folder (usually the INBOX). This folder is checked regularly and any mail found is notified as event to the corresponding event handler.
-
-
Field Summary
Fields Modifier and Type Field Description static String
AUTHMECH_XOAUTH2
Configuration value for the "XOAUTH2" authentication mechanisms.protected String
authMechanism
The authentication mechanism to use.static String
CONF_AUTH_MECHANISM
The configuration key for the authentication mechanism.static String
CONF_OAUTH2_ACCESS_TOKEN_PROVIDER_ID
The configuration key for the ID (simple instance name) of theOAuth2AccessTokenProvider
to be used when the "XOAUTH2" authentication mechanism is configured.protected AbstractEventManager
eventManager
The event manager for retrieving the OAuth2 access token provider.protected URI[]
eventManagerURIs
The event manager URIs to provide to the created events.protected String
folderName
The name of the folder which causes an event when a new mail is received.protected String
host
The host to connect to and from which to use as mail source.protected boolean
keepConnection
Whether the mail storage should not be disconnected between polls.protected OAuth2AccessTokenProvider
oauth2AccessTokenProvider
The plugin instance that will be used to acquire an OAuth2 access token when the "XOAUTH2" authentication mechanisms is used.protected String
oauth2AccessTokenProviderId
The optional (simple) instance name of the OAuth2AccessTokenProvider to use when the "XOAUTH2" authentication mechanism is configured.protected String
password
The password used to connect to the host with, unless the current authentication mechanism requires something different.static String
PLUGIN_TYPE_OAUTH2_ACCESS_TOKEN_PROVIDER
The plug-in type ID for OAuth 2 access token providers.protected Long
pollTime
The time in milliseconds this event source polls its data source.protected int
port
The port of the host to which to connect to.protected String
protocol
The protocol to use for this mail events, for instance pop3, imap, ...protected String
sslProtocols
The SSL protocols used for secure protocols.protected Long
startPoint
The start point (in milliseconds) this event should start polling.protected javax.mail.Store
store
The mail server which to check for mails.protected long
timeout
The timeout in milliseconds this event source uses for the mail server.protected String
userName
The user name to connect to the host with.-
Fields inherited from class de.aristaflow.adept2.core.eventmanager.common.AbstractEventSource
eventType, handlerList, id, lock, logger, sourceType, thread, threadLock
-
Fields inherited from interface de.aristaflow.adept2.model.events.sources.MailSource
CONF_FOLDER, CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_PROTOCOL, CONF_SSL_PROTOCOLS, CONF_TIMEOUT, CONF_USER_NAME
-
Fields inherited from interface de.aristaflow.adept2.model.events.sources.PollingMailSource
CONF_KEEP_CONNECTION
-
Fields inherited from interface de.aristaflow.adept2.model.events.sources.PollingSource
CONF_POLL_TIME, CONF_START_TIME
-
-
Constructor Summary
Constructors Modifier Constructor Description MailEventSource(org.apache.commons.configuration2.Configuration conf, AbstractEventManager eventManager, Registry registry)
Default constructor for a plugin of anEventManager
called by a service registry.protected
MailEventSource(org.apache.commons.configuration2.Configuration conf, AbstractEventManager eventManager, Registry registry, Class<?> confDesc)
Constructor for subclasses allowing to set a separate class for the configuration description.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkForAndHandleEvent()
Checks for an event and handles it (usually by usingAbstractEventSource.forwardEvent(Event, boolean, boolean)
).protected void
close(javax.mail.Folder folder)
Closes and expunges the designated folder if it is open.protected void
closeStore()
Closes the message store of this event source.protected void
connectToStore()
Connects to the configured store of this event source.protected MailEvent
createMailEvent(javax.mail.Message message)
Creates aMailEvent
for the designated message.long
getPollTime()
Gets the time in milliseconds this event source polls, more precisely, the time span between two polls.long
getStartTime()
Gets the absolute time in milliseconds since midnight, January 1, 1970 UTC when this polling source should start polling.void
init()
Initialises this polling source.protected boolean
nextHandler(MailEvent event, boolean consumed, boolean concurrent)
This method is called right before an event is offered to the next handler.protected javax.mail.Folder
openFolder()
Opens and returns the folder configured for the message store in read/write mode.protected void
setConfiguration(org.apache.commons.configuration2.Configuration conf, boolean completed)
Sets the internal fields according to the values of the designated configuration.void
setConfiguration(org.apache.commons.configuration2.Configuration conf, String id)
Sets the (additional) configuration of this event source and its ID.void
terminate()
Terminates this polling source.protected boolean
timedOut(Throwable t)
Gets whether the designated throwable indicates a timeout.protected void
verifyConfigurationValues()
This method verifies the configuration values that have been set yet.-
Methods inherited from class de.aristaflow.adept2.core.eventmanager.common.AbstractEventSource
addAll, addedHandlerLocked, addedHandlerLocked, addHandler, createPattern, forwardEvent, getEventHandler, getHierarchicalSourceName, getID, getType, maskedInterrupt, maskInterrupt, removeHandler, removingHandlerLocked, replaceValueParams, run, unmaskInterrupt
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.aristaflow.adept2.model.events.sources.EventSource
addAll, addHandler, getEventHandler, getHierarchicalSourceName, getID, getType, removeHandler
-
-
-
-
Field Detail
-
PLUGIN_TYPE_OAUTH2_ACCESS_TOKEN_PROVIDER
public static final String PLUGIN_TYPE_OAUTH2_ACCESS_TOKEN_PROVIDER
The plug-in type ID for OAuth 2 access token providers.- See Also:
- Constant Field Values
-
CONF_AUTH_MECHANISM
public static final String CONF_AUTH_MECHANISM
The configuration key for the authentication mechanism. This will be used in theProperties
for connecting to the mail store.- See Also:
- Constant Field Values
-
AUTHMECH_XOAUTH2
public static final String AUTHMECH_XOAUTH2
Configuration value for the "XOAUTH2" authentication mechanisms.- See Also:
- Constant Field Values
-
CONF_OAUTH2_ACCESS_TOKEN_PROVIDER_ID
public static final String CONF_OAUTH2_ACCESS_TOKEN_PROVIDER_ID
The configuration key for the ID (simple instance name) of theOAuth2AccessTokenProvider
to be used when the "XOAUTH2" authentication mechanism is configured.- See Also:
- Constant Field Values
-
pollTime
protected Long pollTime
The time in milliseconds this event source polls its data source.
-
startPoint
protected Long startPoint
The start point (in milliseconds) this event should start polling.
-
keepConnection
protected boolean keepConnection
Whether the mail storage should not be disconnected between polls.
-
protocol
protected String protocol
The protocol to use for this mail events, for instance pop3, imap, ...
-
sslProtocols
protected String sslProtocols
The SSL protocols used for secure protocols.
-
host
protected String host
The host to connect to and from which to use as mail source.
-
port
protected int port
The port of the host to which to connect to.
-
userName
protected String userName
The user name to connect to the host with.
-
authMechanism
protected String authMechanism
The authentication mechanism to use.
-
password
protected String password
The password used to connect to the host with, unless the current authentication mechanism requires something different.
-
oauth2AccessTokenProviderId
protected String oauth2AccessTokenProviderId
The optional (simple) instance name of the OAuth2AccessTokenProvider to use when the "XOAUTH2" authentication mechanism is configured. If no ID is specified, the default instance will be used.
-
oauth2AccessTokenProvider
protected OAuth2AccessTokenProvider oauth2AccessTokenProvider
The plugin instance that will be used to acquire an OAuth2 access token when the "XOAUTH2" authentication mechanisms is used.
-
folderName
protected String folderName
The name of the folder which causes an event when a new mail is received. If not specified the usual INBOX will be used.
-
timeout
protected long timeout
The timeout in milliseconds this event source uses for the mail server.
-
eventManager
protected AbstractEventManager eventManager
The event manager for retrieving the OAuth2 access token provider.
-
eventManagerURIs
protected final URI[] eventManagerURIs
The event manager URIs to provide to the created events.
-
store
protected javax.mail.Store store
The mail server which to check for mails. This will benull
in case this source is not connected.
-
-
Constructor Detail
-
MailEventSource
public MailEventSource(org.apache.commons.configuration2.Configuration conf, AbstractEventManager eventManager, Registry registry) throws ConfigurationException
Default constructor for a plugin of anEventManager
called by a service registry.Note that this constructor calls
setConfiguration(Configuration, boolean)
; subclasses may not be fully initialised at this point.- Parameters:
conf
- The configuration as provided from the registry.eventManager
- The event manager this event source is a plugin of.registry
- The registry of this plugin.- Throws:
ConfigurationException
- If the configuration is not valid for aPollingMailSource
, aConfigurationException
will be thrown.
-
MailEventSource
protected MailEventSource(org.apache.commons.configuration2.Configuration conf, AbstractEventManager eventManager, Registry registry, Class<?> confDesc) throws ConfigurationException
Constructor for subclasses allowing to set a separate class for the configuration description.Note that this constructor calls
setConfiguration(Configuration, boolean)
; subclasses may not be fully initialised at this point.- Parameters:
conf
- The configuration as provided from the registry.eventManager
- The event manager this event source is a plugin of.registry
- The registry of this plugin.confDesc
- The class (or interface) providing the configuration description to validate.- Throws:
ConfigurationException
- If the configuration is not valid for aPollingMailSource
, aConfigurationException
will be thrown.
-
-
Method Detail
-
setConfiguration
protected void setConfiguration(org.apache.commons.configuration2.Configuration conf, boolean completed) throws ConfigurationException
Sets the internal fields according to the values of the designated configuration. Note that this method is called from the constructor and therefore this object may not be fully initialised!- Parameters:
conf
- The configuration from which to get the configuration values.completed
- Whether the configuration is complete after this setting and therefore the values should be verified.- Throws:
ConfigurationException
- If there are problems with the configuration, for instance the configuration does not provide required values, aConfigurationException
may be thrown.
-
verifyConfigurationValues
protected void verifyConfigurationValues() throws ConfigurationException
This method verifies the configuration values that have been set yet. If a configuration value is missing or inappropriate, aConfigurationException
will be thrown.- Throws:
ConfigurationException
- If there are problems with the configuration, for instance the configuration does not provide required values, aConfigurationException
may be thrown.
-
setConfiguration
public void setConfiguration(org.apache.commons.configuration2.Configuration conf, String id) throws ConfigurationException
Description copied from interface:EventSource
Sets the (additional) configuration of this event source and its ID. The configuration is additional with respect to the configuration provided by the plugin load mechanism, if the event source is loaded as plugin.
It is ensured that this method will be called beforeRunnable.run()
.- Specified by:
setConfiguration
in interfaceEventSource<MailEvent>
- Overrides:
setConfiguration
in classAbstractEventSource<MailEvent>
- Parameters:
conf
- The configuration of this event source. This adheres (and overwrites) the configuration provided by the plugin loading, if this event source has been loaded as plugin. Whether this is allowed to benull
depends on the implementation of the event source.id
- The ID of this event source. This must not benull
nor the empty string.- Throws:
ConfigurationException
- If there are problems with the designated configuration (configuration isnull
although this is not allowed, a required configuration value is missing, ...), aConfigurationException
will be thrown.
-
getPollTime
public long getPollTime()
Description copied from interface:PollingSource
Gets the time in milliseconds this event source polls, more precisely, the time span between two polls. If this is 0 or negative, no polling will apply. However,PollingSource.init()
andPollingSource.terminate()
will be called.- Specified by:
getPollTime
in interfacePollingSource<MailEvent>
- Returns:
- The time in milliseconds this event source polls (> 0).
-
getStartTime
public long getStartTime()
Description copied from interface:PollingSource
Gets the absolute time in milliseconds since midnight, January 1, 1970 UTC when this polling source should start polling. This is used for calculating the first poll time by adding the time span defined inPollingSource.getPollTime()
.- Specified by:
getStartTime
in interfacePollingSource<MailEvent>
- Returns:
- The absolute time in milliseconds since midnight, January 1, 1970 UTC when this polling source should start polling.
-
init
public void init()
Description copied from interface:PollingSource
Initialises this polling source. This is called once before polling is started the first time.- Specified by:
init
in interfacePollingSource<MailEvent>
-
checkForAndHandleEvent
public void checkForAndHandleEvent()
Description copied from class:AbstractEventSource
Checks for an event and handles it (usually by usingAbstractEventSource.forwardEvent(Event, boolean, boolean)
).- Specified by:
checkForAndHandleEvent
in classAbstractEventSource<MailEvent>
-
terminate
public void terminate()
Description copied from interface:PollingSource
Terminates this polling source. This is called once after polling has terminated and this source is removed.
Note that this may be called any time, even when creating or handling an event. So take care to synchronise the implementation appropriately!- Specified by:
terminate
in interfacePollingSource<MailEvent>
-
createMailEvent
protected MailEvent createMailEvent(javax.mail.Message message) throws javax.mail.MessagingException, IOException
Creates aMailEvent
for the designated message.- Parameters:
message
- The message for which to create aMailEvent
.- Returns:
- The
MailEvent
created for the designated message. - Throws:
javax.mail.MessagingException
- If retrieving the required data from the message fails, aMessagingException
will be thrown.IOException
- Retrieving the (complex) content of the message may thrown anIOException
.
-
nextHandler
protected boolean nextHandler(MailEvent event, boolean consumed, boolean concurrent)
Description copied from class:AbstractEventSource
This method is called right before an event is offered to the next handler. In case of concurrent handling, this method will only be called once for all concurrent handling but this will be indicated by the corresponding parameter.- Specified by:
nextHandler
in classAbstractEventSource<MailEvent>
- Parameters:
event
- The event which is about to be handled by the next registered handler.consumed
- Whether the event has been consumed yet.concurrent
- Whether the next event handling is concurrent and thus the event will be offered to several event handler concurrently.- Returns:
- Whether the event handling may continue. In case of serious
problems preventing further event handling, return
false
here.
-
connectToStore
protected void connectToStore() throws javax.mail.MessagingException
Connects to the configured store of this event source.- Throws:
javax.mail.MessagingException
- If there are problems connecting to the store, aMessagingException
will be thrown.
-
closeStore
protected void closeStore() throws javax.mail.MessagingException
Closes the message store of this event source.- Throws:
javax.mail.MessagingException
- If there are problems closing the store, aMessagingException
will be thrown.
-
openFolder
protected javax.mail.Folder openFolder() throws javax.mail.MessagingException
Opens and returns the folder configured for the message store in read/write mode.- Returns:
- The opened folder configured for the message store.
- Throws:
javax.mail.MessagingException
- If there are problems retrieving or opening the folder, aMessagingException
will be thrown.
-
close
protected void close(javax.mail.Folder folder) throws javax.mail.MessagingException
Closes and expunges the designated folder if it is open.- Parameters:
folder
- The folder to close and expunge.- Throws:
javax.mail.MessagingException
- If there are problems closing the designated folder, aMessagingException
will be thrown.
-
timedOut
protected boolean timedOut(Throwable t)
Gets whether the designated throwable indicates a timeout. Depending on the protocol provider, a timeout can be signalled differently.- Parameters:
t
- The throwable which to check for whether it indicates a timeout.- Returns:
- Whether the designated throwable indicates a timeout.
-
-