01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.service.mail;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.io.ByteArrayInputStream;
23: import java.util.Properties;
24: import javax.mail.MessagingException;
25: import javax.mail.Session;
26: import javax.mail.internet.MimeMessage;
27:
28: import org.ofbiz.base.util.Debug;
29:
30: public class MimeMessageWrapper implements java.io.Serializable {
31:
32: public static final String module = MimeMessageWrapper.class
33: .getName();
34: protected transient MimeMessage message = null;
35: protected transient Session session = null;
36: protected Properties mailProperties = null;
37: protected byte[] serializedBytes = null;
38:
39: public MimeMessageWrapper(Session session, MimeMessage message) {
40: this (session);
41: this .setMessage(message);
42: }
43:
44: public MimeMessageWrapper(Session session) {
45: this .setSession(session);
46: }
47:
48: public void setSession(Session session) {
49: this .session = session;
50: this .mailProperties = session.getProperties();
51: }
52:
53: public synchronized Session getSession() {
54: if (session == null) {
55: session = Session.getInstance(mailProperties, null);
56: }
57: return session;
58: }
59:
60: public void setMessage(MimeMessage message) {
61: if (message != null) {
62: // serialize the message
63: this .message = message;
64: ByteArrayOutputStream baos = new ByteArrayOutputStream();
65: try {
66: message.writeTo(baos);
67: baos.flush();
68: serializedBytes = baos.toByteArray();
69: } catch (MessagingException e) {
70: Debug.logError(e, module);
71: } catch (IOException e) {
72: Debug.logError(e, module);
73: } finally {
74: try {
75: baos.close();
76: } catch (IOException e) {
77: Debug.logError(e, module);
78: }
79: }
80: }
81: }
82:
83: public synchronized MimeMessage getMessage() {
84: if (message == null) {
85: // deserialize the message
86: if (serializedBytes != null) {
87: ByteArrayInputStream bais = new ByteArrayInputStream(
88: serializedBytes);
89: try {
90: message = new MimeMessage(this .getSession(), bais);
91: } catch (MessagingException e) {
92: Debug.logError(e, module);
93: }
94: }
95: }
96: return message;
97: }
98: }
|