KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > mail > factory > JavaMailMimePartDSFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaMailMimePartDSFactory.java,v 1.4 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.mail.factory;
27
28
29 //import java
30
import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.ObjectInputStream JavaDoc;
33 import java.io.OptionalDataException JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 //import javax
39
import javax.mail.internet.MimeMessage JavaDoc;
40 import javax.mail.internet.MimePart JavaDoc;
41 import javax.mail.internet.MimePartDataSource JavaDoc;
42 import javax.mail.internet.InternetAddress JavaDoc;
43 import javax.mail.Message JavaDoc;
44 import javax.mail.Session JavaDoc;
45 import javax.naming.Context JavaDoc;
46 import javax.naming.Name JavaDoc;
47 import javax.naming.Reference JavaDoc;
48 import javax.naming.RefAddr JavaDoc;
49 import javax.naming.spi.ObjectFactory JavaDoc;
50
51 //import objectweb.util
52
import org.objectweb.util.monolog.api.BasicLevel;
53 import org.objectweb.util.monolog.api.Logger;
54
55 //import jonas
56
import org.objectweb.jonas.common.Log;
57 import org.objectweb.jonas.common.PropDump;
58 import org.objectweb.jonas.common.JNDIUtils;
59 import org.objectweb.jonas.mail.lib.JAuthenticator;
60
61 /**
62  * This class provides an implementation of a mime part data source factory
63  * for sending mail.
64  * @author Ludovic Bert
65  * @author Florent Benoit
66  */

67 public class JavaMailMimePartDSFactory implements ObjectFactory JavaDoc {
68
69     /**
70      * The Java type for which this factory knows how to create objects.
71      */

72     protected static final String JavaDoc FACTORY_TYPE = "javax.mail.internet.MimePartDataSource";
73
74     /**
75      * The logger used in JOnAS
76      */

77     private static Logger logger = null;
78
79
80
81     /**
82      * Creates a javax.mail.MimePartDataSource object using the location or
83      * reference information specified.
84      * @param obj the possibly null object containing location or reference
85      * information that can be used in creating an object.
86      * @param name the name of this object relative to nameCtx, or null if no
87      * name is specified.
88      * @param nameCtx the context relative to which the name parameter is
89      * specified, or null if name is relative to the default initial context.
90      * @param environment the possibly null environment that is used in
91      * creating the object.
92      * @return a newly created javax.mail.internet.MimePartDataSource object with the
93      * specific configuration; null if an object cannot be created.
94      * @throws Exception if this object factory encountered an exception
95      * while attempting to create an object, and no other object factories are
96      * to be tried.
97      */

98     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
99                                     Hashtable JavaDoc environment) throws Exception JavaDoc {
100
101         //Get the logger
102
if (logger == null) {
103             logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
104         }
105
106         //Get the reference
107
Reference JavaDoc ref = (Reference JavaDoc) obj;
108
109         //Get the class name
110
String JavaDoc clname = ref.getClassName();
111
112         //Check the class name
113
if (!ref.getClassName().equals(FACTORY_TYPE)) {
114             logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
115             return (null);
116         }
117
118         Properties JavaDoc sessionProps = new Properties JavaDoc();
119         Properties JavaDoc mimeMessageProps = new Properties JavaDoc();
120         Properties JavaDoc authenticationProps = new Properties JavaDoc();
121         RefAddr JavaDoc refAddr = null;
122
123         refAddr = ref.get("javaxmailSession.properties");
124         if (refAddr != null) {
125             sessionProps = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
126             if (logger.isLoggable(BasicLevel.DEBUG)) {
127                 PropDump.print("These are the properties used to obtain a new Session object", sessionProps, logger, BasicLevel.DEBUG);
128             }
129         }
130
131         refAddr = ref.get("javaxInternetMimeMessage.properties");
132         if (refAddr != null) {
133             mimeMessageProps = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
134             if (logger.isLoggable(BasicLevel.DEBUG)) {
135                 PropDump.print("These are the properties specific to Internet mail",
136                         mimeMessageProps, logger, BasicLevel.DEBUG);
137             }
138         }
139
140         refAddr = ref.get("authentication.properties");
141         if (refAddr != null) {
142             authenticationProps = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
143             if (logger.isLoggable(BasicLevel.DEBUG)) {
144                 PropDump.print("These are the authentication properties",
145                            authenticationProps, logger, BasicLevel.DEBUG);
146             }
147         }
148
149         //username (Authentication)
150
String JavaDoc mailAuthenticationUsername = authenticationProps.getProperty("mail.authentication.username");
151
152         //Password (Authentication)
153
String JavaDoc mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password");
154
155         //Field 'to'
156
String JavaDoc mailTo = mimeMessageProps.getProperty("mail.to");
157         InternetAddress JavaDoc[] toRecipients = null;
158         if (mailTo != null) {
159             toRecipients = getInternetAddressFromString(mailTo);
160         }
161
162         //Field 'cc'
163
String JavaDoc mailCc = mimeMessageProps.getProperty("mail.cc");
164         InternetAddress JavaDoc[] ccRecipients = null;
165         if (ccRecipients != null) {
166             ccRecipients = getInternetAddressFromString(mailCc);
167         }
168
169         //Field 'bcc'
170
String JavaDoc mailBcc = mimeMessageProps.getProperty("mail.bcc");
171         InternetAddress JavaDoc[] bccRecipients = null;
172         if (bccRecipients != null) {
173             getInternetAddressFromString(mailBcc);
174         }
175
176         //Field 'subject'
177
String JavaDoc mailSubject = mimeMessageProps.getProperty("mail.subject");
178
179
180         JAuthenticator jAuthenticator = null;
181         if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) {
182             jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword);
183         }
184
185         //Build the message from the Session.
186
MimeMessage JavaDoc mimeMessage = new MimeMessage JavaDoc(Session.getInstance(sessionProps, jAuthenticator));
187
188         //And set the properties if there are not null
189

190         //Field 'to'
191
if (toRecipients != null) {
192             mimeMessage.setRecipients(Message.RecipientType.TO, toRecipients);
193         }
194
195         //Field 'cc'
196
if (ccRecipients != null) {
197             mimeMessage.setRecipients(Message.RecipientType.CC, ccRecipients);
198         }
199
200         //Field 'bcc'
201
if (bccRecipients != null) {
202             mimeMessage.setRecipients(Message.RecipientType.BCC, bccRecipients);
203         }
204
205         //Field 'subject'
206
if (mailSubject != null) {
207             mimeMessage.setSubject(mailSubject);
208         }
209
210         MimePartDataSource JavaDoc mimePartDS = new MimePartDataSource JavaDoc((MimePart JavaDoc) mimeMessage);
211         return mimePartDS;
212     }
213
214     /**
215      * Convert a comma separated list into an array of InternetAddress
216      * @param txt acomma separated string
217      * @return an array of InternetAddress
218      */

219
220     private InternetAddress JavaDoc[] getInternetAddressFromString(String JavaDoc txt) {
221         if (txt == null) {
222             return null;
223         }
224         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(txt, ",");
225         InternetAddress JavaDoc[] addresses = new InternetAddress JavaDoc[st.countTokens()];
226         int i = 0;
227         try {
228             while (st.hasMoreTokens()) {
229                 addresses[i] = new InternetAddress JavaDoc((String JavaDoc) st.nextToken());
230                 i++;
231             }
232         } catch (Exception JavaDoc e) {
233             return null;
234         }
235         return addresses;
236     }
237 }
238
Popular Tags