001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming.factory;
019:
020: import java.security.AccessController;
021: import java.security.PrivilegedAction;
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Properties;
025: import javax.mail.Authenticator;
026: import javax.mail.PasswordAuthentication;
027: import javax.mail.Session;
028: import javax.naming.Name;
029: import javax.naming.Context;
030: import javax.naming.RefAddr;
031: import javax.naming.Reference;
032: import javax.naming.spi.ObjectFactory;
033:
034: /**
035: * <p>Factory class that creates a JNDI named JavaMail Session factory,
036: * which can be used for managing inbound and outbound electronic mail
037: * messages via JavaMail APIs. All messaging environment properties
038: * described in the JavaMail Specification may be passed to the Session
039: * factory; however the following properties are the most commonly used:</p>
040: * <ul>
041: * <li>
042: * <li><strong>mail.smtp.host</strong> - Hostname for outbound transport
043: * connections. Defaults to <code>localhost</code> if not specified.</li>
044: * </ul>
045: *
046: * <p>This factory can be configured in a <code><DefaultContext></code>
047: * or <code><Context></code> element in your <code>conf/server.xml</code>
048: * configuration file. An example of factory configuration is:</p>
049: * <pre>
050: * <Resource name="mail/smtp" auth="CONTAINER"
051: * type="javax.mail.Session"/>
052: * <ResourceParams name="mail/smtp">
053: * <parameter>
054: * <name>factory</name>
055: * <value>org.apache.naming.factory.MailSessionFactory</value>
056: * </parameter>
057: * <parameter>
058: * <name>mail.smtp.host</name>
059: * <value>mail.mycompany.com</value>
060: * </parameter>
061: * </ResourceParams>
062: * </pre>
063: *
064: * @author Craig R. McClanahan
065: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
066: */
067:
068: public class MailSessionFactory implements ObjectFactory {
069:
070: /**
071: * The Java type for which this factory knows how to create objects.
072: */
073: protected static final String factoryType = "javax.mail.Session";
074:
075: /**
076: * Create and return an object instance based on the specified
077: * characteristics.
078: *
079: * @param refObj Reference information containing our parameters, or null
080: * if there are no parameters
081: * @param name The name of this object, relative to context, or null
082: * if there is no name
083: * @param context The context to which name is relative, or null if name
084: * is relative to the default initial context
085: * @param env Environment variables, or null if there are none
086: *
087: * @exception Exception if an error occurs during object creation
088: */
089: public Object getObjectInstance(Object refObj, Name name,
090: Context context, Hashtable env) throws Exception {
091:
092: // Return null if we cannot create an object of the requested type
093: final Reference ref = (Reference) refObj;
094: if (!ref.getClassName().equals(factoryType))
095: return (null);
096:
097: // Create a new Session inside a doPrivileged block, so that JavaMail
098: // can read its default properties without throwing Security
099: // exceptions.
100: //
101: // Bugzilla 31288, 33077: add support for authentication.
102: return AccessController.doPrivileged(new PrivilegedAction() {
103: public Object run() {
104:
105: // Create the JavaMail properties we will use
106: Properties props = new Properties();
107: props.put("mail.transport.protocol", "smtp");
108: props.put("mail.smtp.host", "localhost");
109:
110: String password = null;
111:
112: Enumeration attrs = ref.getAll();
113: while (attrs.hasMoreElements()) {
114: RefAddr attr = (RefAddr) attrs.nextElement();
115: if ("factory".equals(attr.getType())) {
116: continue;
117: }
118:
119: if ("password".equals(attr.getType())) {
120: password = (String) attr.getContent();
121: continue;
122: }
123:
124: props.put(attr.getType(), (String) attr
125: .getContent());
126: }
127:
128: Authenticator auth = null;
129: if (password != null) {
130: String user = props.getProperty("mail.smtp.user");
131: if (user == null) {
132: user = props.getProperty("mail.user");
133: }
134:
135: if (user != null) {
136: final PasswordAuthentication pa = new PasswordAuthentication(
137: user, password);
138: auth = new Authenticator() {
139: protected PasswordAuthentication getPasswordAuthentication() {
140: return pa;
141: }
142: };
143: }
144: }
145:
146: // Create and return the new Session object
147: Session session = Session.getInstance(props, auth);
148: return (session);
149:
150: }
151: });
152:
153: }
154:
155: }
|