You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Glenn Nielsen <gl...@voyager.apg.more.net> on 2001/03/25 03:25:49 UTC

Re: cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory MailSessionFactory.java ResourceFactory.java

Craig,

I have another version of a MailSessionFactory (I thought I had mentioned
I would take care of it).  It is identical to what you just commited,
with one exception.

It uses getInstance() instead of getDefaultInstance().  getDefaultInstance()
creates just one instance of a Session for the entire JVM which would be global
to all web applications.  With getInstance() you can configure different SMTP settings
per web application.

I also have an updated version of the Factory which creates a MimePartDataSource
which allows more generic configuration of SMTP settings like you did below.

Regards,

Glenn

craigmcc@apache.org wrote:
> 
>   /**
>    * <p>Factory class that creates a JNDI named JavaMail Session factory,
>    * which can be used for managing inbound and outbound electronic mail
>    * messages via JavaMail APIs.  All messaging environment properties
>    * described in the JavaMail Specification may be passed to the Session
>    * factory; however the following properties are the most commonly used:</p>
>    * <ul>
>    * <li>
>    * <li><strong>mail.smtp.host</strong> - Hostname for outbound transport
>    *     connections.  Defaults to <code>localhost</code> if not specified.</li>
>    * </ul>
>    *
>    * <p>This factory can be configured in a <code>&lt;DefaultContext&gt;</code>
>    * or <code>&lt;Context&gt;</code> element in your <code>conf/server.xml</code>
>    * configuration file.  An example of factory configuration is:</p>
>    * <pre>
>    * &lt;Resource name="mail/smtp" auth="CONTAINER"
>    *           type="javax.mail.Session"/&gt;
>    * &lt;ResourceParams name="mail/smtp"&gt;
>    *   &lt;parameter&gt;
>    *     &lt;name&gt;factory&lt;/name&gt;
>    *     &lt;value&gt;org.apache.naming.factory.MailSessionFactory&lt;/value&gt;
>    *   &lt;/parameter&gt;
>    *   &lt;parameter&gt;
>    *     &lt;name&gt;mail.smtp.host&lt;/name&gt;
>    *     &lt;value&gt;mail.mycompany.com&lt;/value&gt;
>    *   &lt;/parameter&gt;
>    * &lt;/ResourceParams&gt;
>    * </pre>
>    *
>    * @author Craig R. McClanahan
>    * @version $Revision: 1.1 $ $Date: 2001/03/24 20:52:07 $
>    */
> 
>   public class MailSessionFactory implements ObjectFactory {
> 
> 
>       /**
>        * The Java type for which this factory knows how to create objects.
>        */
>       protected static final String factoryType = "javax.mail.Session";
> 
> 
>       /**
>        * Create and return an object instance based on the specified
>        * characteristics.
>        *
>        * @param refObj Reference information containing our parameters, or null
>        *  if there are no parameters
>        * @param name The name of this object, relative to context, or null
>        *  if there is no name
>        * @param context The context to which name is relative, or null if name
>        *  is relative to the default initial context
>        * @param env Environment variables, or null if there are none
>        *
>        * @exception Exception if an error occurs during object creation
>        */
>       public Object getObjectInstance(Object refObj, Name name, Context context,
>                                     Hashtable env) throws Exception
>       {
> 
>           // Return null if we cannot create an object of the requested type
>         final Reference ref = (Reference) refObj;
>           if (!ref.getClassName().equals(factoryType))
>               return (null);
> 
>           // Create a new Session inside a doPrivileged block, so that JavaMail
>           // can read its default properties without throwing Security
>           // exceptions
>           return AccessController.doPrivileged( new PrivilegedAction() {
>                 public Object run() {
> 
>                       // Create the JavaMail properties we will use
>                       Properties props = new Properties();
>                       props.put("mail.transport.protocol", "smtp");
>                       props.put("mail.smtp.host", "localhost");
>                       Enumeration attrs = ref.getAll();
>                       while (attrs.hasMoreElements()) {
>                           RefAddr attr = (RefAddr) attrs.nextElement();
>                           if ("factory".equals(attr.getType()))
>                               continue;
>                           props.put(attr.getType(), (String) attr.getContent());
>                       }
> 
>                       // Create and return the new Session object
>                       Session session = Session.getDefaultInstance(props, null);
>                       return (session);
> 
>                 }
>             } );
> 
>       }
> 
> 
>   }
> 
> 
> 


----------------------------------------------------------------------
Glenn Nielsen             glenn@more.net | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------

Re: cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory MailSessionFactory.java ResourceFactory.java

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 24 Mar 2001, Glenn Nielsen wrote:

> Craig,
> 
> I have another version of a MailSessionFactory (I thought I had mentioned
> I would take care of it).  It is identical to what you just commited,
> with one exception.
> 

Well, I must have missed your message about taking care of it (but it was
a good learning experience for me anyway).  Once we get the Commons
project up and running, the in-memory naming context stuff might be a good
candidate for a shareable component, and I wanted to understand a bit more
about how it worked first.

> It uses getInstance() instead of getDefaultInstance().  getDefaultInstance()
> creates just one instance of a Session for the entire JVM which would be global
> to all web applications.  With getInstance() you can configure different SMTP settings
> per web application.
> 

That sounds like a *much* better idea.  Go ahead and switch this this to
getInstance() if you want.

> I also have an updated version of the Factory which creates a MimePartDataSource
> which allows more generic configuration of SMTP settings like you did below.
> 

It seems like a pretty good design pattern when the underlying factory
accepts an arbitrary set of properties.

> Regards,
> 
> Glenn
> 

Craig