View Javadoc

    1   // ========================================================================
    2   // $Id: MailSessionReference.java 1386 2006-12-08 17:53:22Z janb $
    3   // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.naming.factories;
    17
    18
    19  import javax.mail.Authenticator;
    20  import javax.mail.PasswordAuthentication;
    21  import javax.mail.Session;
    22
    23
    24  import java.util.Enumeration;
    25  import java.util.Hashtable;
    26  import java.util.Iterator;
    27  import java.util.Map;
    28  import java.util.Properties;
    29
    30  import javax.naming.Context;
    31  import javax.naming.Name;
    32  import javax.naming.NamingException;
    33  import javax.naming.RefAddr;
    34  import javax.naming.Reference;
    35  import javax.naming.Referenceable;
    36  import javax.naming.StringRefAddr;
    37  import javax.naming.spi.ObjectFactory;
    38
    39  import org.mortbay.jetty.security.Password;
    40
    41  /**
    42   * MailSessionReference
    43   *
    44   * This is a subclass of javax.mail.Reference and an ObjectFactory for javax.mail.Session objects.
    45   *
    46   * The subclassing of Reference allows all of the setup for a javax.mail.Session
    47   * to be captured without necessitating first instantiating a Session object. The
    48   * reference is bound into JNDI and it is only when the reference is looked up that
    49   * this object factory will create an instance of javax.mail.Session using the
    50   * information captured in the Reference.
    51   *
    52   */
    53  public class MailSessionReference extends Reference implements ObjectFactory
    54  {
    55
    56
    57      public static class PasswordAuthenticator extends Authenticator
    58      {
    59          PasswordAuthentication passwordAuthentication;
    60          private String user;
    61          private String password;
    62
    63          public PasswordAuthenticator()
    64          {
    65
    66          }
    67
    68          public PasswordAuthenticator(String user, String password)
    69          {
    70              passwordAuthentication = new PasswordAuthentication (user, (password.startsWith(Password.__OBFUSCATE)?Password.deobfuscate(password):password));
    71          }
    72
    73          public PasswordAuthentication getPasswordAuthentication()
    74          {
    75              return passwordAuthentication;
    76          }
    77
    78          public void setUser (String user)
    79          {
    80              this.user = user;
    81          }
    82          public String getUser ()
    83          {
    84              return this.user;
    85          }
    86
    87          public String getPassword ()
    88          {
    89              return this.password;
    90          }
    91
    92          public void setPassword(String password)
    93          {
    94              this.password = password;
    95          }
    96
    97
    98      };
    99
    100
    101
    102
    103
    104     /**
    105      *
    106      */
    107     public MailSessionReference()
    108     {
    109        super ("javax.mail.Session", MailSessionReference.class.getName(), null);
    110     }
    111
    112
    113     /**
    114      * Create a javax.mail.Session instance based on the information passed in the Reference
    115      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
    116      * @param ref the Reference
    117      * @param arg1 not used
    118      * @param arg2 not used
    119      * @param arg3 not used
    120      * @return
    121      * @throws Exception
    122      */
    123     public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception
    124     {
    125         if (ref == null)
    126         return null;
    127
    128         Reference reference = (Reference)ref;
    129
    130
    131         Properties props = new Properties();
    132         String user = null;
    133         String password = null;
    134
    135         Enumeration refs = reference.getAll();
    136         while (refs.hasMoreElements())
    137         {
    138             RefAddr refAddr = (RefAddr)refs.nextElement();
    139             String name = refAddr.getType();
    140             String value =  (String)refAddr.getContent();
    141             if (name.equalsIgnoreCase("user"))
    142                 user = value;
    143             else if (name.equalsIgnoreCase("pwd"))
    144                 password = value;
    145             else
    146                 props.put(name, value);
    147         }
    148
    149         if (password == null)
    150             return Session.getInstance(props);
    151         else
    152             return Session.getInstance(props, new PasswordAuthenticator(user, password));
    153     }
    154
    155
    156     public void setUser (String user)
    157     {
    158        StringRefAddr addr =  (StringRefAddr)get("user");
    159        if (addr != null)
    160        {
    161          throw new RuntimeException ("user already set on SessionReference, can't be changed");
    162        }
    163        add(new StringRefAddr("user", user));
    164     }
    165
    166     public void setPassword (String password)
    167     {
    168         StringRefAddr addr = (StringRefAddr)get("pwd");
    169         if (addr != null)
    170             throw new RuntimeException ("password already set on SessionReference, can't be changed");
    171         add(new StringRefAddr ("pwd", password));
    172     }
    173
    174     public void setProperties (Properties properties)
    175     {
    176         Iterator entries = properties.entrySet().iterator();
    177         while (entries.hasNext())
    178         {
    179             Map.Entry e = (Map.Entry)entries.next();
    180             StringRefAddr sref = (StringRefAddr)get((String)e.getKey());
    181             if (sref != null)
    182                 throw new RuntimeException ("property "+e.getKey()+" already set on Session reference, can't be changed");
    183             add(new StringRefAddr((String)e.getKey(), (String)e.getValue()));
    184         }
    185     }
    186
    187
    188
    189 }