tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory MailSessionFactory.java ResourceFactory.java
Date Sat, 24 Mar 2001 20:52:08 GMT
craigmcc    01/03/24 12:52:08

  Modified:    catalina build.xml
               catalina/src/share/org/apache/naming/factory
                        ResourceFactory.java
  Added:       catalina/src/share/org/apache/naming/factory
                        MailSessionFactory.java
  Log:
  Add a naming context resource factory for javax.mail.Session resources
  (compliant with J2EE requirements) so that web applications can look up
  and utilize a preconfigured javax.mail.Session instance.  The resource is
  configured by creating <Resource> and <ResourceParams> elements nested in
  the <Context> or <DefaultContext> element in "conf/server.xml" -- the
  example application will be updated to demonstrate this shortly.
  
  NOTE:  the mechanism by which new object factories are registered needs to
  be generalized.
  
  Revision  Changes    Path
  1.34      +2 -0      jakarta-tomcat-4.0/catalina/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/build.xml,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- build.xml	2001/03/23 01:12:11	1.33
  +++ build.xml	2001/03/24 20:52:07	1.34
  @@ -112,6 +112,8 @@
          unless="jdbcse.present" />
         <exclude name="**/factory/TransactionFactory.java" 
          unless="jta.present" />
  +      <exclude name="**/factory/MailSessionFactory.java"
  +       unless="javamail.present" />
         <exclude name="**/factory/SendMailFactory.java"
          unless="javamail.present" />
         <exclude name="**/startup/CatalinaBlock.java" 
  
  
  
  1.4       +13 -3     jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/ResourceFactory.java
  
  Index: ResourceFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/ResourceFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ResourceFactory.java	2001/01/23 03:43:54	1.3
  +++ ResourceFactory.java	2001/03/24 20:52:07	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/ResourceFactory.java,v
1.3 2001/01/23 03:43:54 remm Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/01/23 03:43:54 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/ResourceFactory.java,v
1.4 2001/03/24 20:52:07 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/03/24 20:52:07 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * Object factory for Resources.
    * 
    * @author Remy Maucherat
  - * @version $Revision: 1.3 $ $Date: 2001/01/23 03:43:54 $
  + * @version $Revision: 1.4 $ $Date: 2001/03/24 20:52:07 $
    */
   
   public class ResourceFactory
  @@ -145,6 +145,16 @@
                       try {
                           factory = (ObjectFactory) 
                               Class.forName(javaxSqlDataSourceFactoryClassName)
  +                            .newInstance();
  +                    } catch(Throwable t) {
  +                    }
  +                } else if (ref.getClassName().equals("javax.mail.Session")) {
  +                    String javaxMailSessionFactoryClassName =
  +                        System.getProperty("javax.mail.Session.Factory",
  +                                           "org.apache.naming.factory.MailSessionFactory");
  +                    try {
  +                        factory = (ObjectFactory) 
  +                            Class.forName(javaxMailSessionFactoryClassName)
                               .newInstance();
                       } catch(Throwable t) {
                       }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/MailSessionFactory.java
  
  Index: MailSessionFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/factory/MailSessionFactory.java,v
1.1 2001/03/24 20:52:07 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/24 20:52:07 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.naming.factory;
  
  import java.security.AccessController;
  import java.security.PrivilegedAction;
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Properties;
  import javax.mail.Session;
  import javax.naming.Name;
  import javax.naming.Context;
  import javax.naming.RefAddr;
  import javax.naming.Reference;
  import javax.naming.spi.ObjectFactory;
  
  /**
   * <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);
  
  		}
  	    } );
  
      }
  
  
  }
  
  
  

Mime
View raw message