View Javadoc

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