Email Answering System
Version 1.0

com.ninecode.email.connector
Class EmailManagedConnectionFactory

java.lang.Object
  |
  +--com.ninecode.email.connector.EmailConnectionData
        |
        +--com.ninecode.email.connector.EmailConfigurationData
              |
              +--com.ninecode.email.connector.EmailManagedConnectionFactory
All Implemented Interfaces:
Cloneable, ConnectionRequestInfo, ConnectionSpec, ManagedConnectionFactory, Runnable, Serializable

public class EmailManagedConnectionFactory
extends EmailConfigurationData
implements ManagedConnectionFactory, Runnable

Generate javax.mail connections wrapped for the Connector architecture.

Author:
Derrick Oswald
See Also:
Serialized Form

Field Summary
protected  PrintWriter mPrintWriter
          The logging output stream.
protected static HashSet mSources
          Set of email sources.
 
Fields inherited from class com.ninecode.email.connector.EmailConfigurationData
mBackgroundThread, mBackgroundThreadInitialDelay, mBackgroundThreadPollDelay, mBackgroundThreadReconnectDelay, mQueueConnectionFactoryResourceReference, mQueueConnectionReference, mStoreFile, mStoreHost, mStorePort, mStoreProtocol, mTransportFile, mTransportHost, mTransportPort, mTransportProtocol
 
Fields inherited from class com.ninecode.email.connector.EmailConnectionData
mMailProperties, mPropertySupport, mStorePassword, mStoreUserName, mTransportPassword, mTransportUserName
 
Constructor Summary
EmailManagedConnectionFactory()
          Creates a new instance of EmailManagedConnectionFactory.
EmailManagedConnectionFactory(EmailConfigurationData data)
          Creates a new instance of EmailManagedConnectionFactory.
 
Method Summary
 Object createConnectionFactory()
          Creates a Connection Factory instance.
 Object createConnectionFactory(ConnectionManager manager)
          Creates a Connection Factory instance.
 ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo info)
          Creates a new physical connection to the underlying EIS resource manager.
 boolean equals(Object obj)
          Check if this ManagedConnectionFactory is equal to another ManagedConnectionFactory.
protected static EmailSource findSourceForConfiguration(EmailConfigurationData data)
          Look up the configuration in the list of sources.
protected  void forward(Message[] messages)
          Perform "best effort" to forward the messages to the jms queue.
protected  EmailConfigurationData getDefaults()
          Get the default settings.
 PrintWriter getLogWriter()
          Get the log writer for this ManagedConnectionFactory instance.
protected static EmailSource getSourceForCurrentThread()
          Look up the current thread in the list of sources.
 int hashCode()
          Returns the hash code for the ManagedConnectionFactory.
protected  void initialDelay()
          Sleep for a few seconds while the container sets *all* our properties.
protected  String mark()
          Create a time marker for the current time.
 ManagedConnection matchManagedConnections(Set set, Subject subject, ConnectionRequestInfo info)
          Returns a matched connection from the candidate set of connections.
 void run()
          Perform background polling for incoming email.
protected  void sendMessage(String reply, String subject, String content, byte[] data)
          Send a jms message with the contents of the email as data.
 void setBackgroundThread(boolean state)
          Override setter for property BackgroundThread.
 void setBackgroundThread(Boolean state)
          Override setter for property BackgroundThread.
 void setLogWriter(PrintWriter writer)
          Set the log writer for this ManagedConnectionFactory instance.
protected  void sleep(int delay, long then)
          Sleep for a few milliseconds.
protected  void startThread()
          Start a new thread running against this Runnable object.
 
Methods inherited from class com.ninecode.email.connector.EmailConfigurationData
getBackgroundThread, getBackgroundThreadInitialDelay, getBackgroundThreadPollDelay, getBackgroundThreadReconnectDelay, getQueueConnectionFactoryResourceReference, getQueueConnectionReference, getStoreFile, getStoreHost, getStorePort, getStoreProtocol, getTransportFile, getTransportHost, getTransportPort, getTransportProtocol, setBackgroundThreadInitialDelay, setBackgroundThreadInitialDelay, setBackgroundThreadPollDelay, setBackgroundThreadPollDelay, setBackgroundThreadReconnectDelay, setBackgroundThreadReconnectDelay, setQueueConnectionFactoryResourceReference, setQueueConnectionReference, setStoreFile, setStoreHost, setStorePort, setStorePort, setStoreProtocol, setTransportFile, setTransportHost, setTransportPort, setTransportPort, setTransportProtocol, storeEquals, toString
 
Methods inherited from class com.ninecode.email.connector.EmailConnectionData
addPropertyChangeListener, clone, dup, getMailProperties, getPassword, getStorePassword, getStoreUserName, getTransportPassword, getTransportUserName, getUserName, override, removePropertyChangeListener, setMailProperties, setPassword, setStorePassword, setStoreUserName, setTransportPassword, setTransportUserName, setUserName
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

mPrintWriter

protected PrintWriter mPrintWriter
The logging output stream.


mSources

protected static HashSet mSources
Set of email sources. Values are EmailSource objects that are unique according to storeEquals(). This set provides for one thread to be running for each distinct configuration that has background polling enabled, and provides for synchronized access to distinct mail stores through the Source.

Constructor Detail

EmailManagedConnectionFactory

public EmailManagedConnectionFactory()
Creates a new instance of EmailManagedConnectionFactory.


EmailManagedConnectionFactory

public EmailManagedConnectionFactory(EmailConfigurationData data)
Creates a new instance of EmailManagedConnectionFactory.

Method Detail

getDefaults

protected EmailConfigurationData getDefaults()
Get the default settings.

Returns:
A bean configured with the current settings.

startThread

protected void startThread()
Start a new thread running against this Runnable object.


setBackgroundThread

public void setBackgroundThread(Boolean state)
Override setter for property BackgroundThread. Starts or stops the background thread based on new state.

Overrides:
setBackgroundThread in class EmailConfigurationData
Parameters:
state - New value of property BackgroundThread.
See Also:
EmailConfigurationData.setBackgroundThread(boolean)

setBackgroundThread

public void setBackgroundThread(boolean state)
Override setter for property BackgroundThread. Starts or stops the background thread based on new state.

Overrides:
setBackgroundThread in class EmailConfigurationData
Parameters:
state - New value of property BackgroundThread.

sendMessage

protected void sendMessage(String reply,
                           String subject,
                           String content,
                           byte[] data)
                    throws ResourceException
Send a jms message with the contents of the email as data. It gets the queue connection factory from the configuration setting QueueConnectionFactoryResourceReference which is of the form "java:comp/env/jms/MyQueueConnectionFactory". It gets the queue name from the configuration setting QueueConnectionReference which is of the form "java:comp/env/jms/QueueName". The message sent is a text message with the contents set to the RFC 822 format email messgae received.

Parameters:
data - The message to send.
Throws:
ResourceException - If a problem occurs, such as:
  • an initial JNDI context cannot be obtained
  • the JNI lookup of either the QueueConnectionFactory or Queue fails
  • any JMSException occurs

  • initialDelay

    protected void initialDelay()
    Sleep for a few seconds while the container sets *all* our properties.


    forward

    protected void forward(Message[] messages)
    Perform "best effort" to forward the messages to the jms queue.

    Parameters:
    messages - The messages to be forwarded.

    sleep

    protected void sleep(int delay,
                         long then)
    Sleep for a few milliseconds. Cause a delay between polls.

    Parameters:
    delay - The number of milliseconds to sleep.
    then - The current time when processing started. This is used to moderate the sleep time so that polls happen at nearly exactly the requested interval. For example, in the following snippet the delay between processes is zero if processing takes more than 50 milliseconds:
     while (true) {
         long t = System.currentTimeMillis ();
         .. do some processing ..
         sleep (50, t); }
     

    getSourceForCurrentThread

    protected static EmailSource getSourceForCurrentThread()
                                                    throws ResourceAdapterInternalException
    Look up the current thread in the list of sources.

    Returns:
    The source that has the current thread.
    Throws:
    ResourceAdapterInternalException - If there is no source that has the current thread.

    findSourceForConfiguration

    protected static EmailSource findSourceForConfiguration(EmailConfigurationData data)
                                                     throws ResourceAdapterInternalException
    Look up the configuration in the list of sources.

    Returns:
    The source that has the given configuration.
    Throws:
    ResourceAdapterInternalException - If there is no source that has that configuration.

    mark

    protected String mark()
    Create a time marker for the current time.

    Returns:
    The date/time mark for now.

    run

    public void run()
    Perform background polling for incoming email. After a programmable initial delay, a Store is obtained and periodically polled for new messages. Messages are forwarded to the configured queue. On encountering a third connection failure the polling switches to a waiting state using a (longer) reconnect delay time. On a successful connection it reverts to the original polling interval.

    Specified by:
    run in interface Runnable
    See Also:
    initialDelay(), EmailConfigurationData.getBackgroundThreadPollDelay(), EmailConfigurationData.getBackgroundThreadReconnectDelay()

    createConnectionFactory

    public Object createConnectionFactory()
                                   throws ResourceException
    Creates a Connection Factory instance. The Connection Factory instance gets initialized with a default ConnectionManager provided by the resource adapter.

    Specified by:
    createConnectionFactory in interface ManagedConnectionFactory
    Returns:
    EIS-specific Connection Factory instance or javax.resource.cci.ConnectionFactory instance.
    Throws:
    ResourceException - Generic exception.
    ResourceAdapterInternalException - Resource adapter related error condition.

    createConnectionFactory

    public Object createConnectionFactory(ConnectionManager manager)
                                   throws ResourceException
    Creates a Connection Factory instance. The Connection Factory instance gets initialized with the passed ConnectionManager. In the managed scenario, ConnectionManager is provided by the application server.

    Specified by:
    createConnectionFactory in interface ManagedConnectionFactory
    Parameters:
    manager - ConnectionManager to be associated with created EIS connection factory instance.
    Returns:
    EIS-specific Connection Factory instance or javax.resource.cci.ConnectionFactory instance.
    Throws:
    ResourceException - Generic exception.
    ResourceAdapterInternalException - Resource adapter related error condition.

    createManagedConnection

    public ManagedConnection createManagedConnection(Subject subject,
                                                     ConnectionRequestInfo info)
                                              throws ResourceException
    Creates a new physical connection to the underlying EIS resource manager. ManagedConnectionFactory uses the security information (passed as Subject) and additional ConnectionRequestInfo (which is specific to ResourceAdapter and opaque to application server) to create this new connection.

    Specified by:
    createManagedConnection in interface ManagedConnectionFactory
    Parameters:
    subject - Caller's security information.
    info - - Additional resource adapter specific connection request information.
    Returns:
    ManagedConnection instance.
    Throws:
    ResourceException - Generic exception.
    SecurityException - Security related error.
    ResourceAllocationException - Failed to allocate system resources for connection request.
    ResourceAdapterInternalException - Resource adapter related error condition.
    EISSystemException - Internal error condition in EIS instance.

    equals

    public boolean equals(Object obj)
    Check if this ManagedConnectionFactory is equal to another ManagedConnectionFactory. Compares the configuration information in this managed connection against configuration information in the given ManagedConnectionFactory (NOTE: it does not compare LogWriter or state information).

    Specified by:
    equals in interface ManagedConnectionFactory
    Overrides:
    equals in class EmailConfigurationData
    Returns:
    true if two instances are equal.

    getLogWriter

    public PrintWriter getLogWriter()
                             throws ResourceException
    Get the log writer for this ManagedConnectionFactory instance. The log writer is a character output stream to which all logging and tracing messages for this ManagedConnectionFactory instance will be printed. ApplicationServer manages the association of output stream with the ManagedConnectionFactory. When a ManagedConnectionFactory object is created the log writer is initially null, in other words, logging is disabled.

    Specified by:
    getLogWriter in interface ManagedConnectionFactory
    Returns:
    The PrintWriter or null if none has been set yet.
    Throws:
    ResourceException - Generic exception.

    hashCode

    public int hashCode()
    Returns the hash code for the ManagedConnectionFactory.

    Specified by:
    hashCode in interface ManagedConnectionFactory
    Overrides:
    hashCode in class EmailConfigurationData
    Returns:
    The hash code for the ManagedConnectionFactory.

    matchManagedConnections

    public ManagedConnection matchManagedConnections(Set set,
                                                     Subject subject,
                                                     ConnectionRequestInfo info)
                                              throws ResourceException
    Returns a matched connection from the candidate set of connections. ManagedConnectionFactory uses the security info (as in Subject) and information provided through ConnectionRequestInfo and additional Resource Adapter specific criteria to do matching. Note that criteria used for matching is specific to a resource adapter and is not prescribed by the Connector specification. This method returns a ManagedConnection instance that is the best match for handling the connection allocation request.

    Specified by:
    matchManagedConnections in interface ManagedConnectionFactory
    Parameters:
    set - Candidate connection set.
    subject - Caller's security information.
    info - Additional resource adapter specific connection request information.
    Returns:
    ManagedConnection if resource adapter finds an acceptable match, otherwise null.
    Throws:
    ResourceException - Generic exception.
    SecurityException - Security related error.
    ResourceAdapterInternalException - Resource adapter related error condition.
    NotSupportedException - If operation is not supported.

    setLogWriter

    public void setLogWriter(PrintWriter writer)
    Set the log writer for this ManagedConnectionFactory instance. The log writer is a character output stream to which all logging and tracing messages for this ManagedConnectionfactory instance will be printed. ApplicationServer manages the association of output stream with the ManagedConnectionFactory. When a ManagedConnectionFactory object is created the log writer is initially null, in other words, logging is disabled. Once a log writer is associated with a ManagedConnectionFactory, logging and tracing for ManagedConnectionFactory instance is enabled. The ManagedConnection instances created by ManagedConnectionFactory "inherits" the log writer, which can be overridden by ApplicationServer using ManagedConnection.setLogWriter to set ManagedConnection specific logging and tracing.

    Specified by:
    setLogWriter in interface ManagedConnectionFactory
    Parameters:
    writer - An out stream for error logging and tracing.

    Email Answering System
    Version 1.0

    Copyright © 2003 Derrick Oswald. All rights reserved.