1
2
3
4
5
6
7
8
9
10
11
12
13
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
43
44
45
46
47
48
49
50
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
115
116
117
118
119
120
121
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 }