Source Code Cross Referenced for JavaMailMimePartDSFactory.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » mail » factory » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.mail.factory 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999 Bull S.A.
004:         * Contact: jonas-team@objectweb.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: JavaMailMimePartDSFactory.java 9118 2006-07-05 13:23:44Z sauthieg $
023:         * --------------------------------------------------------------------------
024:         */package org.objectweb.jonas.mail.factory;
025:
026:        //import java
027:        import java.util.Hashtable;
028:        import java.util.Properties;
029:        import java.util.StringTokenizer;
030:
031:        import javax.mail.Message;
032:        import javax.mail.Session;
033:        import javax.mail.internet.InternetAddress;
034:        import javax.mail.internet.MimeMessage;
035:        import javax.mail.internet.MimePart;
036:        import javax.mail.internet.MimePartDataSource;
037:        import javax.naming.Context;
038:        import javax.naming.Name;
039:        import javax.naming.RefAddr;
040:        import javax.naming.Reference;
041:        import javax.naming.spi.ObjectFactory;
042:
043:        import org.objectweb.jonas.common.JNDIUtils;
044:        import org.objectweb.jonas.common.Log;
045:        import org.objectweb.jonas.common.PropDump;
046:        import org.objectweb.jonas.mail.lib.JAuthenticator;
047:        import org.objectweb.util.monolog.api.BasicLevel;
048:        import org.objectweb.util.monolog.api.Logger;
049:
050:        /**
051:         * This class provides an implementation of a mime part data source factory
052:         * for sending mail.
053:         * @author Ludovic Bert
054:         * @author Florent Benoit
055:         */
056:        public class JavaMailMimePartDSFactory implements  ObjectFactory {
057:
058:            /**
059:             * The Java type for which this factory knows how to create objects.
060:             */
061:            protected static final String FACTORY_TYPE = "javax.mail.internet.MimePartDataSource";
062:
063:            /**
064:             * The logger used in JOnAS
065:             */
066:            private static Logger logger = null;
067:
068:            /**
069:             * Creates a javax.mail.MimePartDataSource object using the location or
070:             * reference information specified.
071:             * @param obj the possibly null object containing location or reference
072:             * information that can be used in creating an object.
073:             * @param name the name of this object relative to nameCtx, or null if no
074:             * name is specified.
075:             * @param nameCtx the context relative to which the name parameter is
076:             * specified, or null if name is relative to the default initial context.
077:             * @param environment the possibly null environment that is used in
078:             * creating the object.
079:             * @return a newly created javax.mail.internet.MimePartDataSource object with the
080:             * specific configuration; null if an object cannot be created.
081:             * @throws Exception if this object factory encountered an exception
082:             * while attempting to create an object, and no other object factories are
083:             * to be tried.
084:             */
085:            public Object getObjectInstance(Object obj, Name name,
086:                    Context nameCtx, Hashtable environment) throws Exception {
087:
088:                //Get the logger
089:                if (logger == null) {
090:                    logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
091:                }
092:
093:                //Get the reference
094:                Reference ref = (Reference) obj;
095:
096:                //Get the class name
097:                String clname = ref.getClassName();
098:
099:                //Check the class name
100:                if (!ref.getClassName().equals(FACTORY_TYPE)) {
101:                    logger.log(BasicLevel.ERROR,
102:                            "Cannot create object : required type is '"
103:                                    + FACTORY_TYPE + "', but found type is '"
104:                                    + clname + "'.");
105:                    return (null);
106:                }
107:
108:                Properties sessionProps = new Properties();
109:                Properties mimeMessageProps = new Properties();
110:                Properties authenticationProps = new Properties();
111:                RefAddr refAddr = null;
112:
113:                refAddr = ref.get("javaxmailSession.properties");
114:                if (refAddr != null) {
115:                    sessionProps = (Properties) JNDIUtils
116:                            .getObjectFromBytes((byte[]) refAddr.getContent());
117:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
118:                        PropDump
119:                                .print(
120:                                        "These are the properties used to obtain a new Session object",
121:                                        sessionProps, logger, BasicLevel.DEBUG);
122:                    }
123:                }
124:
125:                refAddr = ref.get("javaxInternetMimeMessage.properties");
126:                if (refAddr != null) {
127:                    mimeMessageProps = (Properties) JNDIUtils
128:                            .getObjectFromBytes((byte[]) refAddr.getContent());
129:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
130:                        PropDump
131:                                .print(
132:                                        "These are the properties specific to Internet mail",
133:                                        mimeMessageProps, logger,
134:                                        BasicLevel.DEBUG);
135:                    }
136:                }
137:
138:                refAddr = ref.get("authentication.properties");
139:                if (refAddr != null) {
140:                    authenticationProps = (Properties) JNDIUtils
141:                            .getObjectFromBytes((byte[]) refAddr.getContent());
142:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
143:                        PropDump.print(
144:                                "These are the authentication properties",
145:                                authenticationProps, logger, BasicLevel.DEBUG);
146:                    }
147:                }
148:
149:                //username (Authentication)
150:                String mailAuthenticationUsername = authenticationProps
151:                        .getProperty("mail.authentication.username");
152:
153:                //Password (Authentication)
154:                String mailAuthenticationPassword = authenticationProps
155:                        .getProperty("mail.authentication.password");
156:
157:                //Field 'to'
158:                String mailTo = mimeMessageProps.getProperty("mail.to");
159:                InternetAddress[] toRecipients = null;
160:                if (mailTo != null) {
161:                    toRecipients = getInternetAddressFromString(mailTo);
162:                }
163:
164:                //Field 'cc'
165:                String mailCc = mimeMessageProps.getProperty("mail.cc");
166:                InternetAddress[] ccRecipients = null;
167:                if (ccRecipients != null) {
168:                    ccRecipients = getInternetAddressFromString(mailCc);
169:                }
170:
171:                //Field 'bcc'
172:                String mailBcc = mimeMessageProps.getProperty("mail.bcc");
173:                InternetAddress[] bccRecipients = null;
174:                if (bccRecipients != null) {
175:                    getInternetAddressFromString(mailBcc);
176:                }
177:
178:                //Field 'subject'
179:                String mailSubject = mimeMessageProps
180:                        .getProperty("mail.subject");
181:
182:                JAuthenticator jAuthenticator = null;
183:                if ((mailAuthenticationUsername != null)
184:                        && (mailAuthenticationPassword != null)) {
185:                    jAuthenticator = new JAuthenticator(
186:                            mailAuthenticationUsername,
187:                            mailAuthenticationPassword);
188:                }
189:
190:                //Build the message from the Session.
191:                MimeMessage mimeMessage = new MimeMessage(Session.getInstance(
192:                        sessionProps, jAuthenticator));
193:
194:                //And set the properties if there are not null
195:
196:                //Field 'to'
197:                if (toRecipients != null) {
198:                    mimeMessage.setRecipients(Message.RecipientType.TO,
199:                            toRecipients);
200:                }
201:
202:                //Field 'cc'
203:                if (ccRecipients != null) {
204:                    mimeMessage.setRecipients(Message.RecipientType.CC,
205:                            ccRecipients);
206:                }
207:
208:                //Field 'bcc'
209:                if (bccRecipients != null) {
210:                    mimeMessage.setRecipients(Message.RecipientType.BCC,
211:                            bccRecipients);
212:                }
213:
214:                //Field 'subject'
215:                if (mailSubject != null) {
216:                    mimeMessage.setSubject(mailSubject);
217:                }
218:
219:                MimePartDataSource mimePartDS = new MimePartDataSource(
220:                        (MimePart) mimeMessage);
221:                return mimePartDS;
222:            }
223:
224:            /**
225:             * Convert a comma separated list into an array of InternetAddress
226:             * @param txt acomma separated string
227:             * @return an array of InternetAddress
228:             */
229:
230:            private InternetAddress[] getInternetAddressFromString(String txt) {
231:                if (txt == null) {
232:                    return null;
233:                }
234:                StringTokenizer st = new StringTokenizer(txt, ",");
235:                InternetAddress[] addresses = new InternetAddress[st
236:                        .countTokens()];
237:                int i = 0;
238:                try {
239:                    while (st.hasMoreTokens()) {
240:                        addresses[i] = new InternetAddress((String) st
241:                                .nextToken());
242:                        i++;
243:                    }
244:                } catch (Exception e) {
245:                    return null;
246:                }
247:                return addresses;
248:            }
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.