KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mailsb > SessionMailerBean


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: SessionMailerBean.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.MessagingException;
39 import javax.mail.Session;
40 import javax.mail.Transport;
41 import javax.mail.internet.InternetAddress;
42 import javax.mail.internet.MimeMessage;
43
44 import javax.naming.InitialContext;
45 import javax.naming.NamingException;
46
47 /**
48  * Implementation of the mailer Session Bean. It uses javax.mail.Session This
49  * bean is a statefull, and synchronized bean. The container uses the
50  * SessionBean methods to notify the enterprise Bean instances of the instance's
51  * life cycle events.
52  * @author Florent Benoit
53  * @author Ludovic Bert
54  */

55 public class SessionMailerBean implements SessionBean {
56
57     /**
58      * Name of the bean
59      */

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

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

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

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

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

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

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

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

176     public void setMessage(String recipient, String subject, String content) throws Exception, RemoteException {
177
178         //Get the initial context
179
InitialContext ictx = null;
180         try {
181             ictx = new InitialContext();
182         } catch (NamingException e) {
183             throw new Exception("Can not get an inital context : " + e.getMessage());
184         }
185
186         //get a new Session from our ENC envirnoment java:comp/env
187
Session session = null;
188         try {
189             session = (Session) ictx.lookup("java:comp/env/mail/MailSession");
190         } catch (NamingException e) {
191             throw new Exception("You have not configure the mail factory with the name specified"
192                     + " in the jonas-ejb-jar.xml file for java:comp/env/mail/MailSession ."
193                     + " By default, the factory's name is mailSession_1 :" + e.getMessage());
194         }
195
196         try {
197             //Create the message
198
message = new MimeMessage(session);
199             InternetAddress[] toRecipients = new InternetAddress[] {new InternetAddress(recipient)};
200             /*
201              * try { toRecipients[0].validate(); } catch (AddressException e) {
202              * throw new Exception("A failure occurs when validating the email
203              * address '" + recipient + "' :" + e.getMessage()); }
204              */

205             message.setRecipients(Message.RecipientType.TO, toRecipients);
206             message.setSubject(subject);
207             message.setContent(content, "text/plain");
208         } catch (MessagingException e) {
209             throw new Exception("A failure occurs when getting a message from the session and setting "
210                     + "the different parameters :" + e.getMessage());
211         }
212
213     }
214
215     /**
216      * Send the message which was previously configured.
217      * @throws Exception if a problem occurs.
218      * @throws RemoteException if the send failed.
219      */

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