Source Code Cross Referenced for MailComponent.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » component » mail » 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 » ow2 easybeans » org.ow2.easybeans.component.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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: MailComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.component.mail;
025:
026:        import java.util.ArrayList;
027:        import java.util.List;
028:
029:        import javax.mail.Address;
030:        import javax.mail.internet.AddressException;
031:        import javax.mail.internet.InternetAddress;
032:        import javax.naming.InitialContext;
033:        import javax.naming.NamingException;
034:
035:        import org.ow2.easybeans.component.api.EZBComponentException;
036:        import org.ow2.easybeans.component.itf.JavaMailComponent;
037:        import org.ow2.easybeans.component.mail.factory.AbsJavaMailRef;
038:        import org.ow2.easybeans.component.mail.factory.JavaMailMimePartDataSourceRef;
039:        import org.ow2.easybeans.component.mail.factory.JavaMailSessionRef;
040:        import org.ow2.util.log.Log;
041:        import org.ow2.util.log.LogFactory;
042:
043:        /**
044:         * Component for providing mail access to EE components.
045:         * @author Florent BENOIT
046:         */
047:        public class MailComponent implements  JavaMailComponent {
048:
049:            /**
050:             * Logger.
051:             */
052:            private Log logger = LogFactory.getLog(MailComponent.class);
053:
054:            /**
055:             * Default auth for factories associated to this component.
056:             */
057:            private Auth defaultAuth = null;
058:
059:            /**
060:             * List of mail session factories.
061:             */
062:            private List<Session> sessionFactories = null;
063:
064:            /**
065:             * List of mail mime part ds factories.
066:             */
067:            private List<MimePart> mimePartFactories = null;
068:
069:            /**
070:             * Init method.<br/> This method is called before the start method.
071:             * @throws EZBComponentException if the initialization has failed.
072:             */
073:            public void init() throws EZBComponentException {
074:
075:            }
076:
077:            /**
078:             * Start method.<br/> This method is called after the init method.
079:             * @throws EZBComponentException if the start has failed.
080:             */
081:            public void start() throws EZBComponentException {
082:
083:                // Create and bind factories
084:                for (MailItf factory : getAllFactories()) {
085:
086:                    AbsJavaMailRef javaMailRef = null;
087:                    String jndiName = factory.getJNDIName();
088:
089:                    // Create object and bind it
090:                    if (factory instanceof  Session) {
091:                        JavaMailSessionRef javamailSession = new JavaMailSessionRef();
092:                        javaMailRef = javamailSession;
093:                    } else if (factory instanceof  MimePart) {
094:                        JavaMailMimePartDataSourceRef mimePartDataSourceRef = new JavaMailMimePartDataSourceRef();
095:                        javaMailRef = mimePartDataSourceRef;
096:
097:                        // Set properties of this factory.
098:                        MimePart mimePart = (MimePart) factory;
099:
100:                        // Set subject
101:                        mimePartDataSourceRef.setSubject(mimePart.getSubject());
102:
103:                        // Define addresses
104:                        List<Address> toAddresses = new ArrayList<Address>();
105:                        List<Address> ccAddresses = new ArrayList<Address>();
106:                        List<Address> bccAddresses = new ArrayList<Address>();
107:
108:                        // Get value
109:                        for (MailAddress mail : mimePart.getMailAddresses()) {
110:
111:                            // Build address object from the string
112:                            Address address = null;
113:                            try {
114:                                address = new InternetAddress(mail.getName());
115:                            } catch (AddressException e) {
116:                                throw new EZBComponentException(
117:                                        "Cannot build an internet address with given value '"
118:                                                + mail.getName() + "'.", e);
119:                            }
120:
121:                            // Add address in the correct list
122:                            if ("CC".equalsIgnoreCase(mail.getType())) {
123:                                ccAddresses.add(address);
124:                            } else if ("BCC".equalsIgnoreCase(mail.getType())) {
125:                                bccAddresses.add(address);
126:                            } else {
127:                                // Default is TO
128:                                toAddresses.add(address);
129:                            }
130:                        }
131:
132:                        // Set recipients
133:                        mimePartDataSourceRef.setToRecipients(toAddresses
134:                                .toArray(new Address[toAddresses.size()]));
135:                        mimePartDataSourceRef.setCcRecipients(ccAddresses
136:                                .toArray(new Address[ccAddresses.size()]));
137:                        mimePartDataSourceRef.setBccRecipients(bccAddresses
138:                                .toArray(new Address[bccAddresses.size()]));
139:
140:                    } else {
141:                        throw new EZBComponentException("Unknown factory '"
142:                                + factory + "'.");
143:                    }
144:
145:                    // Set common values
146:                    javaMailRef.setName(factory.getName());
147:                    javaMailRef.setJNDIName(jndiName);
148:                    javaMailRef.setProperties(factory
149:                            .getMailSessionProperties());
150:
151:                    // Auth set for the factory ?
152:                    Auth auth = factory.getAuth();
153:
154:                    // No, try to see for default auth ?
155:                    if (auth == null) {
156:                        auth = getAuth();
157:                    }
158:
159:                    // There is authentication, set it
160:                    if (auth != null) {
161:                        javaMailRef.setAuthName(auth.getUsername());
162:                        javaMailRef.setAuthName(auth.getPassword());
163:                    }
164:
165:                    // Bind object
166:                    try {
167:                        new InitialContext().bind(jndiName, javaMailRef);
168:                        logger.info(
169:                                "Binding {0} Mail factory with JNDI name {1}",
170:                                javaMailRef.getType(), jndiName);
171:                    } catch (NamingException e) {
172:                        throw new EZBComponentException(
173:                                "Unable to bind the factory '" + factory
174:                                        + "' with JNDI name '" + jndiName
175:                                        + "'.", e);
176:                    }
177:
178:                }
179:
180:            }
181:
182:            /**
183:             * Stop method.<br/> This method is called when component needs to be
184:             * stopped.
185:             * @throws EZBComponentException if the stop is failing.
186:             */
187:            public void stop() throws EZBComponentException {
188:                // Unbind factories
189:                // Create and bind factories
190:                for (MailItf factory : getAllFactories()) {
191:                    try {
192:                        new InitialContext().unbind(factory.getJNDIName());
193:                    } catch (NamingException e) {
194:                        logger
195:                                .error(
196:                                        "Cannot unbind factory with name {0} and JNDI name {1}",
197:                                        factory.getName(), factory
198:                                                .getJNDIName());
199:                    }
200:                }
201:            }
202:
203:            /**
204:             * Set the auth.
205:             * @param auth the given auth
206:             */
207:            public void setAuth(final Auth auth) {
208:                this .defaultAuth = auth;
209:            }
210:
211:            /**
212:             * @return the auth.
213:             */
214:            public Auth getAuth() {
215:                return this .defaultAuth;
216:            }
217:
218:            /**
219:             * @return list of factories.
220:             */
221:            public List<MailItf> getAllFactories() {
222:                List<MailItf> factories = new ArrayList<MailItf>();
223:                factories.addAll(sessionFactories);
224:                factories.addAll(mimePartFactories);
225:                return factories;
226:            }
227:
228:            /**
229:             * Sets the list of mail session factories.
230:             * @param sessionFactories the list of factories
231:             */
232:            public void setSessions(final List<Session> sessionFactories) {
233:                this .sessionFactories = sessionFactories;
234:            }
235:
236:            /**
237:             * Gets the list of mail session factories.
238:             * @return the list of mail session factories.
239:             */
240:            public List<Session> getSessions() {
241:                return this .sessionFactories;
242:            }
243:
244:            /**
245:             * Sets the list of mail mimepart factories.
246:             * @param mimePartFactories the list of factories
247:             */
248:            public void setMimeParts(final List<MimePart> mimePartFactories) {
249:                this .mimePartFactories = mimePartFactories;
250:            }
251:
252:            /**
253:             * Gets the list of mail mimepart factories.
254:             * @return the list of mail mimepart factories.
255:             */
256:            public List<MimePart> getMimeParts() {
257:                return this.mimePartFactories;
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.