[jboss-cvs] jboss-seam/src/mail/org/jboss/seam/mail ...

Peter Muir peter at bleepbleep.org.uk
Sun Jan 28 16:58:55 EST 2007


  User: pmuir   
  Date: 07/01/28 16:58:55

  Modified:    src/mail/org/jboss/seam/mail  MailSession.java
  Log:
  Get javax.mail.Session from JNDI if available
  
  Revision  Changes    Path
  1.3       +152 -53   jboss-seam/src/mail/org/jboss/seam/mail/MailSession.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: MailSession.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/mail/org/jboss/seam/mail/MailSession.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- MailSession.java	26 Jan 2007 11:42:20 -0000	1.2
  +++ MailSession.java	28 Jan 2007 21:58:55 -0000	1.3
  @@ -9,6 +9,7 @@
   import javax.mail.Authenticator;
   import javax.mail.PasswordAuthentication;
   import javax.mail.Session;
  +import javax.naming.NamingException;
   
   import org.jboss.seam.Component;
   import org.jboss.seam.InterceptionType;
  @@ -21,8 +22,12 @@
   import org.jboss.seam.core.AbstractMutable;
   import org.jboss.seam.log.LogProvider;
   import org.jboss.seam.log.Logging;
  +import org.jboss.seam.util.Naming;
  +
  +/**
  + * Manager component for a javax.mail.Session
  + */
   
  -//TODO Support this being a manager component for a javax.mail.Session from JNDI
   @Name("org.jboss.seam.mail.mailSession")
   @Install(precedence=BUILT_IN)
   @Scope(APPLICATION)
  @@ -30,6 +35,8 @@
   public class MailSession extends AbstractMutable implements Serializable
   {
      
  +   public static final String SERVICE_NAME = "java:/Mail";
  +   
      private static final LogProvider log = Logging.getLogProvider(MailSession.class);
   
   	private Session session;
  @@ -39,17 +46,42 @@
   	private String username;
   	private String password;
   	private boolean debug = false;
  +   private String serviceName ;
  +   private Boolean lookupService;
  +   private boolean doJndi = false;
   
      @Unwrap
  -   public Session getSession()
  +   public Session getSession() throws NamingException
  +   {
  +      if (doJndi) 
  +      {
  +         // TODO Is this the best way to do this?
  +         // This simulates an EVENT scope component
  +         return  (Session) Naming.getInitialContext().lookup(getServiceName());
  +      }
  +      else 
      {
         return session;
      }
  +   }
   
  +   /**
  +    * Initialise mail session
  +    * 
  +    * Unless disabled, if a mail Session can be found in JNDI, then just manage
  +    * be a simple wrapper; otherwise configure the session as specified in
  +    * components.xml
  +    */
      @Create
   	public void create()
   	{
  -      log.info("connecting to mail server: " + getHost() + ':' + getPort());
  +     
  +      if (isDoJndi()) {
  +         doJndi = true;
  +       }
  +      else
  +      {
  +         log.info("Using Seam managed mail session (" + getHost() + ':' + getPort() + ")");
         
   		Properties properties = new Properties();
   
  @@ -100,6 +132,44 @@
         
         log.info("connected to mail server");
   	}
  +	}
  +   
  +   private boolean isDoJndi() {
  +      if (getLookupService() != null && !getLookupService()) 
  +      {
  +         return false;
  +      }
  +      else 
  +      {
  +         Session s = null;
  +         try
  +         {
  +            s = (Session) Naming.getInitialContext().lookup(getServiceName());
  +         }
  +         catch (NamingException e)
  +         { 
  +            // Swallow
  +         }
  +         
  +         if (lookupService == null && serviceName == null  && s == null) 
  +         {
  +            log.debug("Unable to get Session from JNDI: " + getServiceName());
  +            return false;
  +         }
  +         else if (s == null)
  +         {
  +            // The user has explicitly set a property that suggests they want a 
  +            // session from JNDI.  Log this at a high level as a result.
  +            log.warn("Unable to get Session from JNDI: " + getServiceName());
  +            return false;
  +         }
  +         else
  +         {
  +            log.info("Using Session from JNDI: " + getServiceName());
  +            return true;
  +         }
  +      }
  +   }
   
   	public String getPassword()
   	{
  @@ -173,8 +243,37 @@
   		return port;
   	}
      
  +   public String getServiceName()
  +   {
  +      if (serviceName == null) 
  +      {
  +         return SERVICE_NAME;
  +      }
  +      else
  +      {
  +         return serviceName;
  +      }
  +   }
  +
  +   public void setServiceName(String jndiName)
  +   {
  +      this.serviceName = jndiName;
  +   }
  +
  +   public Boolean getLookupService()
  +   {
  +      return lookupService;
  +   }
  +
  +   public void setLookupService(Boolean useJndi)
  +   {
  +      this.lookupService = useJndi;
  +   }
  +
      public static Session instance() {
         return (Session) Component.getInstance(MailSession.class);
      }
   
  +   
  +
   }
  
  
  



More information about the jboss-cvs-commits mailing list