KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mailsb > MimePartDSMailerBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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: MimePartDSMailerBean.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package mailsb;
28
29 //import java
30
import java.rmi.RemoteException;
31
32 //import javax
33
import javax.ejb.EJBException;
34 import javax.ejb.SessionBean;
35 import javax.ejb.SessionContext;
36
37 import javax.mail.Message;
38 import javax.mail.MessageContext;
39 import javax.mail.MessagingException;
40 import javax.mail.Transport;
41 import javax.mail.internet.MimePartDataSource;
42 import javax.naming.InitialContext;
43 import javax.naming.NamingException;
44
45 /**
46  * Implementation of the mailer Session Bean. It uses
47  * javax.mail.internet.MimePartDataSource This bean is a statefull, and
48  * synchronized bean. The container uses the SessionBean methods to notify the
49  * enterprise Bean instances of the instance's life cycle events.
50  * @author Florent Benoit
51  * @author Ludovic Bert
52  */

53 public class MimePartDSMailerBean implements SessionBean {
54
55     /**
56      * Name of the bean
57      */

58     private String name = null;
59
60     /**
61      * Session context that the container provides for a session enterprise Bean
62      * instance.
63      */

64     private SessionContext sessionContext = null;
65
66     /**
67      * Reference to the javax.mail.Message object which is the message to send
68      * with javamail
69      */

70     private Message message = null;
71
72     /* ========================= ejbCreate methods =========================== */
73
74     /**
75      * There must be one ejbCreate() method per create() method on the Home
76      * interface, and with the same signature.
77      * @param name the name of the bean
78      */

79     public void ejbCreate(String name) {
80         this.name = name;
81     }
82
83     /* =============== javax.ejb.SessionBean 2.0 implementation ============== */
84
85     /**
86      * Set the associated session context. The container calls this method after
87      * the instance creation. The enterprise Bean instance should store the
88      * reference to the context object in an instance variable. This method is
89      * called with no transaction context.
90      * @param sessionContext A SessionContext interface for the instance.
91      * @throws EJBException Thrown by the method to indicate a failure caused by
92      * a system-level error.
93      * @throws java.rmi.RemoteException This exception is defined in the method
94      * signature to provide backward compatibility for applications
95      * written for the EJB 1.0 specification. Enterprise beans written
96      * for the EJB 1.1 specification should throw the
97      * javax.ejb.EJBException instead of this exception. Enterprise
98      * beans written for the EJB2.0 and higher specifications must throw
99      * the javax.ejb.EJBException instead of this exception.
100      */

101     public void setSessionContext(SessionContext sessionContext) throws EJBException, java.rmi.RemoteException {
102         this.sessionContext = sessionContext;
103     }
104
105     /**
106      * A container invokes this method before it ends the life of the session
107      * object. This happens as a result of a client's invoking a remove
108      * operation, or when a container decides to terminate the session object
109      * after a timeout. This method is called with no transaction context.
110      * @throws EJBException Thrown by the method to indicate a failure caused by
111      * a system-level error.
112      * @throws java.rmi.RemoteException This exception is defined in the method
113      * signature to provide backward compatibility for enterprise beans
114      * written for the EJB 1.0 specification. Enterprise beans written
115      * for the EJB 1.1 specification should throw the
116      * javax.ejb.EJBException instead of this exception. Enterprise
117      * beans written for the EJB2.0 and higher specifications must throw
118      * the javax.ejb.EJBException instead of this exception.
119      */

120     public void ejbRemove() throws EJBException, java.rmi.RemoteException {
121         // Nothing to do for this simple mailer example
122
}
123
124     /**
125      * The activate method is called when the instance is activated from its
126      * "passive" state. The instance should acquire any resource that it has
127      * released earlier in the ejbPassivate() method. This method is called with
128      * no transaction context.
129      * @throws EJBException Thrown by the method to indicate a failure caused by
130      * a system-level error.
131      * @throws java.rmi.RemoteException This exception is defined in the method
132      * signature to provide backward compatibility for enterprise beans
133      * written for the EJB 1.0 specification. Enterprise beans written
134      * for the EJB 1.1 specification should throw the
135      * javax.ejb.EJBException instead of this exception. Enterprise
136      * beans written for the EJB2.0 and higher specifications must throw
137      * the javax.ejb.EJBException instead of this exception.
138      */

139     public void ejbActivate() throws EJBException, java.rmi.RemoteException {
140         // Nothing to do for this simple mailer example
141
}
142
143     /**
144      * The passivate method is called before the instance enters the "passive"
145      * state. The instance should release any resources that it can re-acquire
146      * later in the ejbActivate() method. After the passivate method completes,
147      * the instance must be in a state that allows the container to use the Java
148      * Serialization protocol to externalize and store away the instance's
149      * state. This method is called with no transaction context.
150      * @throws EJBException Thrown by the method to indicate a failure caused by
151      * a system-level error.
152      * @throws java.rmi.RemoteException This exception is defined in the method
153      * signature to provide backward compatibility for enterprise beans
154      * written for the EJB 1.0 specification. Enterprise beans written
155      * for the EJB 1.1 specification should throw the
156      * javax.ejb.EJBException instead of this exception. Enterprise
157      * beans written for the EJB2.0 and higher specifications must throw
158      * the javax.ejb.EJBException instead of this exception.
159      */

160     public void ejbPassivate() throws EJBException, java.rmi.RemoteException {
161         // Nothing to do for this simple mailer example
162
}
163
164     /* ======================== Mailer implementation ======================== */
165
166     /**
167      * Set the message with a specific recipient, subject and content.
168      * @param content the content of the message.
169      * @throws Exception if a problem occurs.
170      * @throws RemoteException if the call failed.
171      */

172     public void setMessage(String content) throws Exception, RemoteException {
173
174         //Get the initial context
175
InitialContext ictx = null;
176         try {
177             ictx = new InitialContext();
178         } catch (NamingException e) {
179             throw new Exception("Can not get an inital context : " + e.getMessage());
180         }
181
182         //get a new MimePartDataSource from our ENC envirnoment java:comp/env
183
MimePartDataSource mimePartDataSource = null;
184         try {
185             mimePartDataSource = (MimePartDataSource) ictx.lookup("java:comp/env/mail/MailMimePartDataSource");
186         } catch (NamingException e) {
187             throw new Exception("You have not configure the mail factory with the name specified"
188                     + " in the jonas-ejb-jar.xml file for java:comp/env/mail/MailMimePartDataSource ."
189                     + " By default, the factory's name is mailMimePartDS_1 :" + e.getMessage());
190         }
191
192         //Get the message context
193
MessageContext messageContext = mimePartDataSource.getMessageContext();
194
195         if (messageContext == null) {
196             throw new Exception("Can not get the message Context of the mimepartDatasource");
197         }
198
199         //Get the message from the context
200
message = messageContext.getMessage();
201
202         try {
203             message.setContent(content, "text/plain");
204         } catch (MessagingException e) {
205             throw new Exception("A failure occurs when setting content of the message :" + e.getMessage());
206         }
207     }
208
209     /**
210      * Send the message which was previously configured.
211      * @throws Exception if a problem occurs.
212      * @throws RemoteException if the send failed.
213      */

214     public void send() throws Exception, RemoteException {
215
216         if (message == null) {
217             throw new Exception("The message can not be send because the method setMessage() "
218                     + " was not called before the send() method.");
219         }
220
221         try {
222             Transport.send(message);
223         } catch (MessagingException e) {
224             throw new Exception("The message can not be send : " + e.getMessage());
225         }
226     }
227
228 }
Popular Tags