00001
00026 package org.objectweb.jonas.mail.factory;
00027
00028
00029
00030 import java.io.ByteArrayInputStream;
00031 import java.io.IOException;
00032 import java.io.ObjectInputStream;
00033 import java.io.OptionalDataException;
00034 import java.util.Hashtable;
00035 import java.util.Properties;
00036 import java.util.StringTokenizer;
00037
00038
00039 import javax.mail.internet.MimeMessage;
00040 import javax.mail.internet.MimePart;
00041 import javax.mail.internet.MimePartDataSource;
00042 import javax.mail.internet.InternetAddress;
00043 import javax.mail.Message;
00044 import javax.mail.Session;
00045 import javax.naming.Context;
00046 import javax.naming.Name;
00047 import javax.naming.Reference;
00048 import javax.naming.RefAddr;
00049 import javax.naming.spi.ObjectFactory;
00050
00051
00052 import org.objectweb.util.monolog.api.BasicLevel;
00053 import org.objectweb.util.monolog.api.Logger;
00054
00055
00056 import org.objectweb.jonas.common.Log;
00057 import org.objectweb.jonas.common.PropDump;
00058 import org.objectweb.jonas.common.JNDIUtils;
00059 import org.objectweb.jonas.mail.lib.JAuthenticator;
00060
00067 public class JavaMailMimePartDSFactory implements ObjectFactory {
00068
00072 protected static final String FACTORY_TYPE = "javax.mail.internet.MimePartDataSource";
00073
00077 private static Logger logger = null;
00078
00079
00080
00098 public Object getObjectInstance(Object obj, Name name, Context nameCtx,
00099 Hashtable environment) throws Exception {
00100
00101
00102 if (logger == null) {
00103 logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
00104 }
00105
00106
00107 Reference ref = (Reference) obj;
00108
00109
00110 String clname = ref.getClassName();
00111
00112
00113 if (!ref.getClassName().equals(FACTORY_TYPE)) {
00114 logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
00115 return (null);
00116 }
00117
00118 Properties sessionProps = new Properties();
00119 Properties mimeMessageProps = new Properties();
00120 Properties authenticationProps = new Properties();
00121 RefAddr refAddr = null;
00122
00123 refAddr = ref.get("javaxmailSession.properties");
00124 if (refAddr != null) {
00125 sessionProps = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
00126 PropDump.print("These are the properties used to obtain a new Session object", sessionProps, logger, BasicLevel.DEBUG);
00127 }
00128
00129 refAddr = ref.get("javaxInternetMimeMessage.properties");
00130 if (refAddr != null) {
00131 mimeMessageProps = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
00132 PropDump.print("These are the properties specific to Internet mail",
00133 mimeMessageProps, logger, BasicLevel.DEBUG);
00134 }
00135
00136 refAddr = ref.get("authentication.properties");
00137 if (refAddr != null) {
00138 authenticationProps = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
00139 PropDump.print("These are the authentication properties",
00140 authenticationProps, logger, BasicLevel.DEBUG);
00141 }
00142
00143
00144 String mailAuthenticationUsername = authenticationProps.getProperty("mail.authentication.username");
00145
00146
00147 String mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password");
00148
00149
00150 String mailTo = mimeMessageProps.getProperty("mail.to");
00151 InternetAddress[] toRecipients = null;
00152 if (mailTo != null) {
00153 toRecipients = getInternetAddressFromString(mailTo);
00154 }
00155
00156
00157 String mailCc = mimeMessageProps.getProperty("mail.cc");
00158 InternetAddress[] ccRecipients = null;
00159 if (ccRecipients != null) {
00160 ccRecipients = getInternetAddressFromString(mailCc);
00161 }
00162
00163
00164 String mailBcc = mimeMessageProps.getProperty("mail.bcc");
00165 InternetAddress[] bccRecipients = null;
00166 if (bccRecipients != null) {
00167 getInternetAddressFromString(mailBcc);
00168 }
00169
00170
00171 String mailSubject = mimeMessageProps.getProperty("mail.subject");
00172
00173
00174 JAuthenticator jAuthenticator = null;
00175 if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) {
00176 jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword);
00177 }
00178
00179
00180 MimeMessage mimeMessage = new MimeMessage(Session.getInstance(sessionProps, jAuthenticator));
00181
00182
00183
00184
00185 if (toRecipients != null) {
00186 mimeMessage.setRecipients(Message.RecipientType.TO, toRecipients);
00187 }
00188
00189
00190 if (ccRecipients != null) {
00191 mimeMessage.setRecipients(Message.RecipientType.CC, ccRecipients);
00192 }
00193
00194
00195 if (bccRecipients != null) {
00196 mimeMessage.setRecipients(Message.RecipientType.BCC, bccRecipients);
00197 }
00198
00199
00200 if (mailSubject != null) {
00201 mimeMessage.setSubject(mailSubject);
00202 }
00203
00204 MimePartDataSource mimePartDS = new MimePartDataSource((MimePart) mimeMessage);
00205 return mimePartDS;
00206 }
00207
00214 private InternetAddress[] getInternetAddressFromString(String txt) {
00215 if (txt == null) {
00216 return null;
00217 }
00218 StringTokenizer st = new StringTokenizer(txt, ",");
00219 InternetAddress[] addresses = new InternetAddress[st.countTokens()];
00220 int i = 0;
00221 try {
00222 while (st.hasMoreTokens()) {
00223 addresses[i] = new InternetAddress((String) st.nextToken());
00224 i++;
00225 }
00226 } catch (Exception e) {
00227 return null;
00228 }
00229 return addresses;
00230 }
00231 }