KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > mail > MimeMessageWrapper


1 /*
2  * $Id: MimeMessageWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.mail;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.Session JavaDoc;
33 import javax.mail.internet.MimeMessage JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36
37 /**
38  *
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
40  * @version $Rev: 5462 $
41  * @since 3.3
42  */

43 public class MimeMessageWrapper implements java.io.Serializable JavaDoc {
44
45     public static final String JavaDoc module = MimeMessageWrapper.class.getName();
46     protected transient MimeMessage JavaDoc message = null;
47     protected transient Session JavaDoc session = null;
48     protected Properties JavaDoc mailProperties = null;
49     protected byte[] serializedBytes = null;
50
51     public MimeMessageWrapper(Session JavaDoc session, MimeMessage JavaDoc message) {
52         this(session);
53         this.setMessage(message);
54     }
55
56     public MimeMessageWrapper(Session JavaDoc session) {
57         this.setSession(session);
58     }
59
60     public void setSession(Session JavaDoc session) {
61         this.session = session;
62         this.mailProperties = session.getProperties();
63     }
64
65     public synchronized Session JavaDoc getSession() {
66         if (session == null) {
67             session = Session.getInstance(mailProperties, null);
68         }
69         return session;
70     }
71
72     public void setMessage(MimeMessage JavaDoc message) {
73         if (message != null) {
74             // serialize the message
75
this.message = message;
76             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
77             try {
78                 message.writeTo(baos);
79                 baos.flush();
80                 serializedBytes = baos.toByteArray();
81             } catch (MessagingException JavaDoc e) {
82                 Debug.logError(e, module);
83             } catch (IOException JavaDoc e) {
84                 Debug.logError(e, module);
85             } finally {
86                 try {
87                     baos.close();
88                 } catch (IOException JavaDoc e) {
89                     Debug.logError(e, module);
90                 }
91             }
92         }
93     }
94
95     public synchronized MimeMessage JavaDoc getMessage() {
96         if (message == null) {
97             // deserialize the message
98
if (serializedBytes != null) {
99                 ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(serializedBytes);
100                 try {
101                     message = new MimeMessage JavaDoc(this.getSession(), bais);
102                 } catch (MessagingException JavaDoc e) {
103                     Debug.logError(e, module);
104                 }
105             }
106         }
107         return message;
108     }
109 }
110
Popular Tags