001: // ========================================================================
002: // $Id: MailSessionReference.java 1386 2006-12-08 17:53:22Z janb $
003: // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.naming.factories;
017:
018: import javax.mail.Authenticator;
019: import javax.mail.PasswordAuthentication;
020: import javax.mail.Session;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: import javax.naming.Context;
029: import javax.naming.Name;
030: import javax.naming.NamingException;
031: import javax.naming.RefAddr;
032: import javax.naming.Reference;
033: import javax.naming.Referenceable;
034: import javax.naming.StringRefAddr;
035: import javax.naming.spi.ObjectFactory;
036:
037: import org.mortbay.jetty.security.Password;
038:
039: /**
040: * MailSessionReference
041: *
042: * This is a subclass of javax.mail.Reference and an ObjectFactory for javax.mail.Session objects.
043: *
044: * The subclassing of Reference allows all of the setup for a javax.mail.Session
045: * to be captured without necessitating first instantiating a Session object. The
046: * reference is bound into JNDI and it is only when the reference is looked up that
047: * this object factory will create an instance of javax.mail.Session using the
048: * information captured in the Reference.
049: *
050: */
051: public class MailSessionReference extends Reference implements
052: ObjectFactory {
053:
054: public static class PasswordAuthenticator extends Authenticator {
055: PasswordAuthentication passwordAuthentication;
056: private String user;
057: private String password;
058:
059: public PasswordAuthenticator() {
060:
061: }
062:
063: public PasswordAuthenticator(String user, String password) {
064: passwordAuthentication = new PasswordAuthentication(
065: user,
066: (password.startsWith(Password.__OBFUSCATE) ? Password
067: .deobfuscate(password)
068: : password));
069: }
070:
071: public PasswordAuthentication getPasswordAuthentication() {
072: return passwordAuthentication;
073: }
074:
075: public void setUser(String user) {
076: this .user = user;
077: }
078:
079: public String getUser() {
080: return this .user;
081: }
082:
083: public String getPassword() {
084: return this .password;
085: }
086:
087: public void setPassword(String password) {
088: this .password = password;
089: }
090:
091: };
092:
093: /**
094: *
095: */
096: public MailSessionReference() {
097: super ("javax.mail.Session", MailSessionReference.class
098: .getName(), null);
099: }
100:
101: /**
102: * Create a javax.mail.Session instance based on the information passed in the Reference
103: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
104: * @param ref the Reference
105: * @param arg1 not used
106: * @param arg2 not used
107: * @param arg3 not used
108: * @return
109: * @throws Exception
110: */
111: public Object getObjectInstance(Object ref, Name arg1,
112: Context arg2, Hashtable arg3) throws Exception {
113: if (ref == null)
114: return null;
115:
116: Reference reference = (Reference) ref;
117:
118: Properties props = new Properties();
119: String user = null;
120: String password = null;
121:
122: Enumeration refs = reference.getAll();
123: while (refs.hasMoreElements()) {
124: RefAddr refAddr = (RefAddr) refs.nextElement();
125: String name = refAddr.getType();
126: String value = (String) refAddr.getContent();
127: if (name.equalsIgnoreCase("user"))
128: user = value;
129: else if (name.equalsIgnoreCase("pwd"))
130: password = value;
131: else
132: props.put(name, value);
133: }
134:
135: if (password == null)
136: return Session.getInstance(props);
137: else
138: return Session.getInstance(props,
139: new PasswordAuthenticator(user, password));
140: }
141:
142: public void setUser(String user) {
143: StringRefAddr addr = (StringRefAddr) get("user");
144: if (addr != null) {
145: throw new RuntimeException(
146: "user already set on SessionReference, can't be changed");
147: }
148: add(new StringRefAddr("user", user));
149: }
150:
151: public void setPassword(String password) {
152: StringRefAddr addr = (StringRefAddr) get("pwd");
153: if (addr != null)
154: throw new RuntimeException(
155: "password already set on SessionReference, can't be changed");
156: add(new StringRefAddr("pwd", password));
157: }
158:
159: public void setProperties(Properties properties) {
160: Iterator entries = properties.entrySet().iterator();
161: while (entries.hasNext()) {
162: Map.Entry e = (Map.Entry) entries.next();
163: StringRefAddr sref = (StringRefAddr) get((String) e
164: .getKey());
165: if (sref != null)
166: throw new RuntimeException(
167: "property "
168: + e.getKey()
169: + " already set on Session reference, can't be changed");
170: add(new StringRefAddr((String) e.getKey(), (String) e
171: .getValue()));
172: }
173: }
174:
175: }
|