001: /*
002: * Copyright 1999,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.Hashtable;
022: import java.util.Properties;
023: import java.util.Enumeration;
024: import javax.mail.Session;
025: import javax.mail.internet.InternetAddress;
026: import javax.mail.internet.MimeMessage;
027: import javax.mail.internet.MimePart;
028: import javax.mail.internet.MimePartDataSource;
029: import javax.naming.Name;
030: import javax.naming.Context;
031: import javax.naming.Reference;
032: import javax.naming.RefAddr;
033: import javax.naming.spi.ObjectFactory;
034:
035: /**
036: * Factory class that creates a JNDI named javamail MimePartDataSource
037: * object which can be used for sending email using SMTP.
038: * <p>
039: * Can be configured in the DefaultContext or Context scope
040: * of your server.xml configuration file.
041: * <p>
042: * Example:
043: * <p>
044: * <pre>
045: * <Resource name="mail/send" auth="CONTAINER"
046: * type="javax.mail.internet.MimePartDataSource"/>
047: * <ResourceParams name="mail/send">
048: * <parameter><name>factory</name>
049: * <value>org.apache.naming.factory.SendMailFactory</value>
050: * </parameter>
051: * <parameter><name>mail.smtp.host</name>
052: * <value>your.smtp.host</value>
053: * </parameter>
054: * <parameter><name>mail.smtp.user</name>
055: * <value>someuser</value>
056: * </parameter>
057: * <parameter><name>mail.from</name>
058: * <value>someuser@some.host</value>
059: * </parameter>
060: * <parameter><name>mail.smtp.sendpartial</name>
061: * <value>true</value>
062: * </parameter>
063: * <parameter><name>mail.smtp.dsn.notify</name>
064: * <value>FAILURE</value>
065: * </parameter>
066: * <parameter><name>mail.smtp.dsn.ret</name>
067: * <value>FULL</value>
068: * </parameter>
069: * </ResourceParams>
070: * </pre>
071: *
072: * @author Glenn Nielsen Rich Catlett
073: */
074:
075: public class SendMailFactory implements ObjectFactory {
076: // The class name for the javamail MimeMessageDataSource
077: protected final String DataSourceClassName = "javax.mail.internet.MimePartDataSource";
078:
079: public Object getObjectInstance(Object RefObj, Name Nm,
080: Context Ctx, Hashtable Env) throws Exception {
081: final Reference Ref = (Reference) RefObj;
082:
083: // Creation of the DataSource is wrapped inside a doPrivileged
084: // so that javamail can read its default properties without
085: // throwing Security Exceptions
086: if (Ref.getClassName().equals(DataSourceClassName)) {
087: return AccessController
088: .doPrivileged(new PrivilegedAction() {
089: public Object run() {
090: // set up the smtp session that will send the message
091: Properties props = new Properties();
092: // enumeration of all refaddr
093: Enumeration list = Ref.getAll();
094: // current refaddr to be set
095: RefAddr refaddr;
096: // set transport to smtp
097: props
098: .put("mail.transport.protocol",
099: "smtp");
100:
101: while (list.hasMoreElements()) {
102: refaddr = (RefAddr) list.nextElement();
103:
104: // set property
105: props.put(refaddr.getType(),
106: (String) refaddr.getContent());
107: }
108: MimeMessage message = new MimeMessage(
109: Session.getInstance(props));
110: try {
111: String from = (String) Ref.get(
112: "mail.from").getContent();
113: message.setFrom(new InternetAddress(
114: from));
115: message.setSubject("");
116: } catch (Exception e) {
117: }
118: MimePartDataSource mds = new MimePartDataSource(
119: (MimePart) message);
120: return mds;
121: }
122: });
123: } else { // We can't create an instance of the DataSource
124: return null;
125: }
126: }
127: }
|