MailSessionEJB.ejb
001 package com.bea.medrec.controller;
002 
003 import com.bea.medrec.utils.MedRecLog4jFactory;
004 import com.bea.medrec.value.Mail;
005 import java.text.MessageFormat;
006 import javax.ejb.EJBException;
007 import javax.ejb.SessionContext;
008 import javax.jms.*;
009 import javax.naming.NamingException;
010 import org.apache.log4j.Logger;
011 import weblogic.ejbgen.*;
012 import weblogic.ejbgen.Session;
013 
014 /**
015  <p>Session Bean implementation for Mail EJB.
016  * The Mail EJB provides support for composing and sending mail messages.</p>
017  *
018  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
019  */
020 @FileGeneration(remoteClass = Constants.Bool.TRUE,
021                 remoteHome = Constants.Bool.TRUE)
022 @JndiName(remote = "MailSessionEJB.MailSessionHome")
023 @ResourceRefs({
024     @ResourceRef(name = "jms/MedRecQueueConnectionFactory",
025                  type = "javax.jms.QueueConnectionFactory",
026                  auth = ResourceRef.Auth.APPLICATION,
027                  sharingScope = ResourceRef.SharingScope.SHAREABLE,
028                  jndiName = "jms/MedRecQueueConnectionFactory")
029 })
030 @ResourceEnvRefs({
031     @ResourceEnvRef(name = "jms/MAIL_MDB_QUEUE",
032                     type = "javax.jms.Queue",
033                     jndiName = "jms/MAIL_MDB_QUEUE")
034 })
035 @Session(maxBeansInFreePool = "1000",
036          initialBeansInFreePool = "0",
037          transTimeoutSeconds = "0",
038          type = Session.SessionType.STATELESS,
039          defaultTransaction = Constants.TransactionAttribute.REQUIRED,
040          enableCallByReference = Constants.Bool.TRUE,
041          ejbName = "MailSessionEJB")
042 
043 public class MailSessionEJB extends weblogic.ejb.GenericSessionBean {
044   private static Logger logger =
045     MedRecLog4jFactory.getLogger(MailSessionEJB.class.getName());
046   private SessionContext ctx;
047   private QueueConnectionFactory qcFactory;
048   private Queue que;
049 
050   /**
051   <p>Sets the session context.  Get handles for all
052   * session and entity and JMS connections used throughout
053   * this session bean.</p>
054   *
055   @param ctx  SessionContext Context for session
056   */
057   public void setSessionContext(SessionContext ctx) {
058     this.ctx = ctx;
059     try {
060       // JMS connections and queues.
061       que = JNDILookupUtils.getJMailQueue();
062       qcFactory = JNDILookupUtils.getQCFactory();
063     catch (NamingException ne) {
064       throw new EJBException(ne);
065     }
066   }
067 
068     //   P U B L I C   M E T H O D S
069 
070     //   C O M P O S E   M A I L
071   /**
072   <p>Composes standard MedRec mail message.</p>
073   *
074   @param pMail
075   @exception NamingException
076   @exception Exception
077   */
078   @RemoteMethod()
079   public Mail composeMail(Mail pMail, Object[] pObjs)
080       throws NamingException, Exception {
081     logger.debug("Composing mail message.");
082     if (pObjs != null) {
083       String formattedText = MessageFormat.format(pMail.getMessage(), pObjs);
084       pMail.setMessage(formattedText);
085     }
086     return pMail;
087   }
088 
089     //   C O M P O S E   A N D   M A I L
090   /**
091   <p>Composes standard MedRec mail message.</p>
092   *
093   @param pMail
094   @exception NamingException
095   @exception Exception
096   */
097   @RemoteMethod()
098   public void composeAndSendMail(Mail pMailthrows NamingException, Exception {
099     sendMailToQueue(composeMail(pMail, null));
100   }
101 
102     //   C O M P O S E   A N D   M A I L
103   /**
104   <p>Composes standard MedRec mail message.</p>
105   *
106   @param pMail
107   @exception NamingException
108   @exception Exception
109   */
110   @RemoteMethod()
111   public void composeAndSendMail(Mail pMail, Object[] pObjs)
112       throws NamingException, Exception {
113     sendMailToQueue(composeMail(pMail, pObjs));
114   }
115 
116       //   S E N D   M A I L   T O   Q U E U E
117   /**
118 <p>Adds a patient by calling the appropriate entity beans.</p>
119 *
120 @param pMail    Patient value object.
121 @exception NamingException
122 @exception Exception
123 */
124   @RemoteMethod()
125   public void sendMailToQueue(Mail pMailthrows NamingException, Exception {
126     logger.debug("Sending mail message: " +
127         pMail.toString());
128 
129     // Declare local variables
130     ObjectMessage mailMessage = null;
131     QueueSession qSession = null;
132     QueueConnection qConnection = null;
133     QueueSender qSender = null;
134     try {
135       qConnection = qcFactory.createQueueConnection();
136       qSession = qConnection.createQueueSession(false, 0);
137       qConnection.start();
138       qSender = qSession.createSender(que);
139       mailMessage = qSession.createObjectMessage(pMail);
140       qSender.send(mailMessage);
141       logger.debug("Finished sending e-mail to queue");
142     catch (JMSException jmse) {
143       throw new EJBException(jmse);
144     catch (Exception e) {
145       logger.error(e);
146       throw e;
147     }
148   }
149 }