001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JavaMailMimePartDataSourceFactory.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.mail.factory;
025:
026: import java.util.Hashtable;
027:
028: import javax.mail.Address;
029: import javax.mail.Message;
030: import javax.mail.Session;
031: import javax.mail.internet.MimeMessage;
032: import javax.mail.internet.MimePartDataSource;
033: import javax.naming.Context;
034: import javax.naming.Name;
035: import javax.naming.Reference;
036: import javax.naming.spi.ObjectFactory;
037:
038: /**
039: * JNDI factory for session factory.
040: * @author Florent BENOIT
041: */
042: public class JavaMailMimePartDataSourceFactory extends
043: AbsJavaMailFactory implements ObjectFactory {
044:
045: /**
046: * Type of object created by this factory.
047: */
048: public static final String FACTORY_TYPE = "javax.mail.internet.MimePartDataSource";
049:
050: /**
051: * TO recipients.
052: */
053: public static final String TO_RECIPIENTS = "to.recipients";
054:
055: /**
056: * CC recipients.
057: */
058: public static final String CC_RECIPIENTS = "cc.recipients";
059:
060: /**
061: * BCC recipients.
062: */
063: public static final String BCC_RECIPIENTS = "bcc.recipients";
064:
065: /**
066: * Subject.
067: */
068: public static final String SUBJECT = "subject";
069:
070: /**
071: * Creates an object using the location or reference information specified.
072: * @param obj The possibly null object containing location or reference
073: * information that can be used in creating an object.
074: * @param name The name of this object relative to <code>nameCtx</code>,
075: * or null if no name is specified.
076: * @param nameCtx The context relative to which the <code>name</code>
077: * parameter is specified, or null if <code>name</code> is relative
078: * to the default initial context.
079: * @param environment The possibly null environment that is used in creating
080: * the object.
081: * @return The object created; null if an object cannot be created.
082: * @exception Exception if this object factory encountered an exception
083: */
084: public Object getObjectInstance(final Object obj, final Name name,
085: final Context nameCtx, final Hashtable<?, ?> environment)
086: throws Exception {
087:
088: // Check if the reference classname is valid
089: Reference reference = (Reference) obj;
090: if (!FACTORY_TYPE.equals(reference.getClassName())) {
091: return null;
092: }
093:
094: // Build Mimemessage wrapping a session object
095: MimeMessage mimeMessage = new MimeMessage(Session.getInstance(
096: getSessionProperties(reference),
097: getAuthenticator(reference)));
098:
099: // Field 'to'
100: Address[] toRecipients = getObject(reference, TO_RECIPIENTS);
101: if (toRecipients != null) {
102: mimeMessage.setRecipients(Message.RecipientType.TO,
103: toRecipients);
104: }
105:
106: // Field 'cc'
107: Address[] ccRecipients = getObject(reference, CC_RECIPIENTS);
108: if (ccRecipients != null) {
109: mimeMessage.setRecipients(Message.RecipientType.CC,
110: ccRecipients);
111: }
112:
113: // Field 'bcc'
114: Address[] bccRecipients = getObject(reference, BCC_RECIPIENTS);
115: if (bccRecipients != null) {
116: mimeMessage.setRecipients(Message.RecipientType.BCC,
117: bccRecipients);
118: }
119:
120: // Field 'subject'
121: String mailSubject = getString(reference, SUBJECT);
122: if (mailSubject != null) {
123: mimeMessage.setSubject(mailSubject);
124: }
125:
126: // Build object and return it
127: MimePartDataSource mimePartDS = new MimePartDataSource(
128: mimeMessage);
129: return mimePartDS;
130:
131: }
132:
133: }
|