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