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