KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: JavaMailMimePartDS.java,v 1.8 2005/04/28 08:43:25 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.mail.factory;
28
29 //import java
30
import java.util.Properties JavaDoc;
31
32 import javax.naming.BinaryRefAddr JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35
36 import org.objectweb.jonas.common.PropDump;
37 import org.objectweb.jonas.common.JNDIUtils;
38 import org.objectweb.jonas.mail.MailService;
39 import org.objectweb.jonas.service.ServiceManager;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * This class provides a way for referencing mail session.
44  * @author Florent Benoit
45  * @author Ludovic Bert
46  */

47 public class JavaMailMimePartDS extends JavaMail {
48
49     /**
50      * Type of the factory
51      */

52     private static final String JavaDoc FACTORY_TYPE = "javax.mail.internet.MimePartDataSource";
53
54     /**
55      * Properties for the javax.mail.MimeMessage object.
56      */

57     private Properties JavaDoc messageProperties = null;
58
59     /**
60      * JOnAS-specific property for Mime Messages configuration (mail.to)
61      */

62     protected static final String JavaDoc MIMEMESSAGE_TO = "mail.to";
63
64     /**
65      * JOnAS-specific property for Mime Messages configuration (mail.cc)
66      */

67
68     protected static final String JavaDoc MIMEMESSAGE_CC = "mail.cc";
69
70     /**
71      * JOnAS-specific property for Mime Messages configuration (mail.bcc)
72      */

73     protected static final String JavaDoc MIMEMESSAGE_BCC = "mail.bcc";
74
75     /**
76      * JOnAS-specific property for Mime Messages configuration (mail.subject)
77      */

78     protected static final String JavaDoc MIMEMESSAGE_SUBJECT = "mail.subject";
79
80     /**
81      * Value used as sequence number by reconfiguration notifications
82      */

83     //protected long sequenceNumber = 0;
84

85     /**
86      * Return the type of the factory
87      * @return the type of the mail factory
88      */

89     public String JavaDoc getType() {
90         return FACTORY_TYPE;
91     }
92
93
94     /**
95      * Constructor of a JavaMailMimePartDS Object
96      * @param factoryName the name of the factory.
97      * @param name the name of this object.
98      * @param mailProperties properties for configuring this object.
99      */

100     public JavaMailMimePartDS(String JavaDoc factoryName, String JavaDoc name, Properties JavaDoc mailProperties) {
101         super(factoryName, name, mailProperties);
102
103         PropDump.print("Received props:", mailProperties, getLogger(), BasicLevel.DEBUG);
104
105         //Get the message properties
106
messageProperties = new Properties JavaDoc();
107         String JavaDoc propValue = null;
108         propValue = (String JavaDoc) getMailSessionProperties().remove(MIMEMESSAGE_TO);
109         if (propValue != null) {
110             messageProperties.setProperty(MIMEMESSAGE_TO, propValue);
111         }
112         propValue = (String JavaDoc) getMailSessionProperties().remove(MIMEMESSAGE_CC);
113         if (propValue != null) {
114             messageProperties.setProperty(MIMEMESSAGE_CC, propValue);
115         }
116         propValue = (String JavaDoc) getMailSessionProperties().remove(MIMEMESSAGE_BCC);
117         if (propValue != null) {
118             messageProperties.setProperty(MIMEMESSAGE_BCC, propValue);
119         }
120         propValue = (String JavaDoc) getMailSessionProperties().remove(MIMEMESSAGE_SUBJECT);
121         if (propValue != null) {
122             messageProperties.setProperty(MIMEMESSAGE_SUBJECT, propValue);
123         }
124
125         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
126             PropDump.print("Message props:", messageProperties, getLogger(), BasicLevel.DEBUG);
127         }
128     }
129
130
131     /**
132      * Retrieves the mimeMessage properties of this object.
133      * @return the mimeMessage properties of this object.
134      */

135     Properties JavaDoc getMimeMessageProperties() {
136         return messageProperties;
137     }
138
139     /**
140      * Set the mimeMessage properties of this object.
141      * @param props the mimeMessage properties
142      */

143     void setMimeMessageProperties(Properties JavaDoc props) {
144         this.messageProperties = props;
145         try {
146             ((MailService) ServiceManager.getInstance().getMailService()).recreateJavaMailFactory(this);
147         } catch (Exception JavaDoc e) {
148             // should never occurs
149
}
150         //notifyReconfiguration(props);
151
if (getLogger().isLoggable(BasicLevel.DEBUG)) {
152             PropDump.print("These are the udated message props",
153                     this.messageProperties, getLogger(), BasicLevel.DEBUG);
154         }
155     }
156
157
158     /**
159      * Retrieves the Reference of the javax.mail.MimePartDataSource object.
160      * The Reference contains the factory used to create this object
161      * (that is the JavaMimePartDSFactory) and the optional parameters used to
162      * configure the factory.
163      * @return the non-null Reference of the javax.mail.MimePartDataSource
164      * object.
165      * @throws NamingException if a naming exception was encountered while
166      * retrieving the reference.
167      */

168     public Reference JavaDoc getReference() throws NamingException JavaDoc {
169
170         //Build the reference to the factory JMailSessionFactory
171
Reference JavaDoc reference = new Reference JavaDoc(FACTORY_TYPE,
172                                             "org.objectweb.jonas.mail.factory.JavaMailMimePartDSFactory",
173                                             null);
174         byte[] bytes = null;
175
176         //Give the session properties
177
bytes = JNDIUtils.getBytesFromObject(getMailSessionProperties());
178         if (bytes != null) {
179             reference.add(new BinaryRefAddr JavaDoc("javaxmailSession.properties", bytes));
180         }
181
182         //Give the mime message properties
183
bytes = JNDIUtils.getBytesFromObject(messageProperties);
184         if (bytes != null) {
185             reference.add(new BinaryRefAddr JavaDoc("javaxInternetMimeMessage.properties", bytes));
186         }
187
188         //Give the authentication properties
189
bytes = JNDIUtils.getBytesFromObject(getAuthenticationProperties());
190         if (bytes != null) {
191             reference.add(new BinaryRefAddr JavaDoc("authentication.properties", bytes));
192         }
193
194         return reference;
195
196     }
197 }
198
Popular Tags