001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: MimePartDSMailerBean.java 4618 2004-04-19 06:39:30Z benoitf $
024: * --------------------------------------------------------------------------
025: */package mailsb;
026:
027: //import java
028: import java.rmi.RemoteException;
029:
030: //import javax
031: import javax.ejb.EJBException;
032: import javax.ejb.SessionBean;
033: import javax.ejb.SessionContext;
034:
035: import javax.mail.Message;
036: import javax.mail.MessageContext;
037: import javax.mail.MessagingException;
038: import javax.mail.Transport;
039: import javax.mail.internet.MimePartDataSource;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042:
043: /**
044: * Implementation of the mailer Session Bean. It uses
045: * javax.mail.internet.MimePartDataSource This bean is a statefull, and
046: * synchronized bean. The container uses the SessionBean methods to notify the
047: * enterprise Bean instances of the instance's life cycle events.
048: * @author Florent Benoit
049: * @author Ludovic Bert
050: */
051: public class MimePartDSMailerBean implements SessionBean {
052:
053: /**
054: * Name of the bean
055: */
056: private String name = null;
057:
058: /**
059: * Session context that the container provides for a session enterprise Bean
060: * instance.
061: */
062: private SessionContext sessionContext = null;
063:
064: /**
065: * Reference to the javax.mail.Message object which is the message to send
066: * with javamail
067: */
068: private Message message = null;
069:
070: /* ========================= ejbCreate methods =========================== */
071:
072: /**
073: * There must be one ejbCreate() method per create() method on the Home
074: * interface, and with the same signature.
075: * @param name the name of the bean
076: */
077: public void ejbCreate(String name) {
078: this .name = name;
079: }
080:
081: /* =============== javax.ejb.SessionBean 2.0 implementation ============== */
082:
083: /**
084: * Set the associated session context. The container calls this method after
085: * the instance creation. The enterprise Bean instance should store the
086: * reference to the context object in an instance variable. This method is
087: * called with no transaction context.
088: * @param sessionContext A SessionContext interface for the instance.
089: * @throws EJBException Thrown by the method to indicate a failure caused by
090: * a system-level error.
091: * @throws java.rmi.RemoteException This exception is defined in the method
092: * signature to provide backward compatibility for applications
093: * written for the EJB 1.0 specification. Enterprise beans written
094: * for the EJB 1.1 specification should throw the
095: * javax.ejb.EJBException instead of this exception. Enterprise
096: * beans written for the EJB2.0 and higher specifications must throw
097: * the javax.ejb.EJBException instead of this exception.
098: */
099: public void setSessionContext(SessionContext sessionContext)
100: throws EJBException, java.rmi.RemoteException {
101: this .sessionContext = sessionContext;
102: }
103:
104: /**
105: * A container invokes this method before it ends the life of the session
106: * object. This happens as a result of a client's invoking a remove
107: * operation, or when a container decides to terminate the session object
108: * after a timeout. This method is called with no transaction context.
109: * @throws EJBException Thrown by the method to indicate a failure caused by
110: * a system-level error.
111: * @throws java.rmi.RemoteException This exception is defined in the method
112: * signature to provide backward compatibility for enterprise beans
113: * written for the EJB 1.0 specification. Enterprise beans written
114: * for the EJB 1.1 specification should throw the
115: * javax.ejb.EJBException instead of this exception. Enterprise
116: * beans written for the EJB2.0 and higher specifications must throw
117: * the javax.ejb.EJBException instead of this exception.
118: */
119: public void ejbRemove() throws EJBException,
120: 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,
140: java.rmi.RemoteException {
141: // Nothing to do for this simple mailer example
142: }
143:
144: /**
145: * The passivate method is called before the instance enters the "passive"
146: * state. The instance should release any resources that it can re-acquire
147: * later in the ejbActivate() method. After the passivate method completes,
148: * the instance must be in a state that allows the container to use the Java
149: * Serialization protocol to externalize and store away the instance's
150: * state. This method is called with no transaction context.
151: * @throws EJBException Thrown by the method to indicate a failure caused by
152: * a system-level error.
153: * @throws java.rmi.RemoteException This exception is defined in the method
154: * signature to provide backward compatibility for enterprise beans
155: * written for the EJB 1.0 specification. Enterprise beans written
156: * for the EJB 1.1 specification should throw the
157: * javax.ejb.EJBException instead of this exception. Enterprise
158: * beans written for the EJB2.0 and higher specifications must throw
159: * the javax.ejb.EJBException instead of this exception.
160: */
161: public void ejbPassivate() throws EJBException,
162: 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 content the content of the message.
171: * @throws Exception if a problem occurs.
172: * @throws RemoteException if the call failed.
173: */
174: public void setMessage(String content) throws Exception,
175: RemoteException {
176:
177: //Get the initial context
178: InitialContext ictx = null;
179: try {
180: ictx = new InitialContext();
181: } catch (NamingException e) {
182: throw new Exception("Can not get an inital context : "
183: + e.getMessage());
184: }
185:
186: //get a new MimePartDataSource from our ENC envirnoment java:comp/env
187: MimePartDataSource mimePartDataSource = null;
188: try {
189: mimePartDataSource = (MimePartDataSource) ictx
190: .lookup("java:comp/env/mail/MailMimePartDataSource");
191: } catch (NamingException e) {
192: throw new Exception(
193: "You have not configure the mail factory with the name specified"
194: + " in the jonas-ejb-jar.xml file for java:comp/env/mail/MailMimePartDataSource ."
195: + " By default, the factory's name is mailMimePartDS_1 :"
196: + e.getMessage());
197: }
198:
199: //Get the message context
200: MessageContext messageContext = mimePartDataSource
201: .getMessageContext();
202:
203: if (messageContext == null) {
204: throw new Exception(
205: "Can not get the message Context of the mimepartDatasource");
206: }
207:
208: //Get the message from the context
209: message = messageContext.getMessage();
210:
211: try {
212: message.setContent(content, "text/plain");
213: } catch (MessagingException e) {
214: throw new Exception(
215: "A failure occurs when setting content of the message :"
216: + e.getMessage());
217: }
218: }
219:
220: /**
221: * Send the message which was previously configured.
222: * @throws Exception if a problem occurs.
223: * @throws RemoteException if the send failed.
224: */
225: public void send() throws Exception, RemoteException {
226:
227: if (message == null) {
228: throw new Exception(
229: "The message can not be send because the method setMessage() "
230: + " was not called before the send() method.");
231: }
232:
233: try {
234: Transport.send(message);
235: } catch (MessagingException e) {
236: throw new Exception("The message can not be send : "
237: + e.getMessage());
238: }
239: }
240:
241: }
|