[jboss-cvs] JBossAS SVN: r110759 - trunk/varia/src/main/java/org/jboss/mail.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Feb 24 08:11:41 EST 2011


Author: darran.lofthouse at jboss.com
Date: 2011-02-24 08:11:40 -0500 (Thu, 24 Feb 2011)
New Revision: 110759

Modified:
   trunk/varia/src/main/java/org/jboss/mail/MailService.java
   trunk/varia/src/main/java/org/jboss/mail/SessionObjectFactory.java
Log:
[JBAS-7883] When multiple mail services are deployed they all share the same settings.

Fix contributed by Mark Lowe mlowe at ebilling.it


Modified: trunk/varia/src/main/java/org/jboss/mail/MailService.java
===================================================================
--- trunk/varia/src/main/java/org/jboss/mail/MailService.java	2011-02-24 09:54:20 UTC (rev 110758)
+++ trunk/varia/src/main/java/org/jboss/mail/MailService.java	2011-02-24 13:11:40 UTC (rev 110759)
@@ -41,12 +41,14 @@
 import javax.mail.Authenticator;
 
 import org.jboss.system.ServiceMBeanSupport;
-import org.jboss.naming.NonSerializableFactory;
+
 import org.jboss.util.naming.Util;
 
 /**
  * MBean that gives support for JavaMail. Object of class javax.mail.Session will be bound
  * in JNDI with the name provided with method {@link #setJNDIName}.
+ * 
+ * Portions copyright 2010 Mark Lowe (mlowe at ebilling.it) - JBAS-7883
  *
  * @jmx:mbean name="jboss:type=Service,service=Mail"
  *            extends="org.jboss.system.ServiceMBean"
@@ -55,6 +57,7 @@
  * @author  <a href="mailto:simone.bordet at compaq.com">Simone Bordet</a>
  * @author  <a href="mailto:jason at planet57.com">Jason Dillon</a>
  * @author Scott.Stark at jboss.org
+ * @author  <a href="mailto:mlowe at ebilling.it">Mark Lowe</a>
  */
 public class MailService
    extends ServiceMBeanSupport
@@ -326,8 +329,7 @@
    private void bind(Properties props, Authenticator auth) throws NamingException
    {
       String bindName = getJNDIName();
-      SessionObjectFactory.setSessionFactoryInfo(props, auth);
-      SessionObjectFactory.setShareSessionInstance(sharedSession);
+      SessionObjectFactory.setSessionFactoryInfo(bindName,props, auth,sharedSession);
 
       InitialContext ctx = new InitialContext();
       try
@@ -363,7 +365,7 @@
             ctx.close();
          }
 
-         SessionObjectFactory.setSessionFactoryInfo(null, null);
+         SessionObjectFactory.remove(bindName);
          log.info("Mail service '" + getJNDIName() + "' removed from JNDI");
       }
    }

Modified: trunk/varia/src/main/java/org/jboss/mail/SessionObjectFactory.java
===================================================================
--- trunk/varia/src/main/java/org/jboss/mail/SessionObjectFactory.java	2011-02-24 09:54:20 UTC (rev 110758)
+++ trunk/varia/src/main/java/org/jboss/mail/SessionObjectFactory.java	2011-02-24 13:11:40 UTC (rev 110759)
@@ -21,7 +21,9 @@
  */
 package org.jboss.mail;
 
+import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.mail.Authenticator;
@@ -33,49 +35,98 @@
 /**
  * A jndi ObjectFactory implementation that creates a new Session from the
  * static class information on each getObjectInstance call.
+ *
+ * Portions copyright 2010 Mark Lowe (mlowe at ebilling.it) - JBAS-7883
  * 
  * @author Scott.Stark at jboss.org
+ * @author  <a href="mailto:mlowe at ebilling.it">Mark Lowe</a>
  * @version $Revision$
  */
 public class SessionObjectFactory implements ObjectFactory
 {
-   private static Properties props;
-   private static Authenticator auth;
-   private static Session instance;
-   private static boolean shareSessionInstance;
 
-   static void setSessionFactoryInfo(Properties props, Authenticator auth)
+   private static final Map<String, MailSessionConfig> mailSessions = new HashMap<String, MailSessionConfig>();
+
+   static void setSessionFactoryInfo(String bindName, Properties props, Authenticator auth, boolean shareSessionInstance)
    {
-      SessionObjectFactory.props = props;
-      SessionObjectFactory.auth = auth;
+      mailSessions.put(bindName, new MailSessionConfig(props, auth, shareSessionInstance));
    }
-   
-   static void setShareSessionInstance(boolean shareSessionInstance)
+
+   static void remove(String bindName)
    {
-      SessionObjectFactory.shareSessionInstance = shareSessionInstance;
+      mailSessions.remove(bindName);
    }
 
-   public Object getObjectInstance(Object obj, Name name, Context nameCtx,
-         Hashtable<?, ?> environment) throws Exception
+   public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
+         throws Exception
    {
-      Session session = null;
-      if(shareSessionInstance)
+      if (name == null)
       {
-         initSession();
-         session = instance;
+         MailSessionConfig defaultConfig = mailSessions.get(MailService.JNDI_NAME);
+
+         if (defaultConfig == null)
+         {
+            throw new IllegalStateException("No default mail session found and no alternative jndi name provided.");
+         }
+         return defaultConfig;
       }
-      else
-      {
-         session = Session.getInstance(props, auth);
-      }
-      return session;
+
+      MailSessionConfig config = mailSessions.get(name.toString());
+
+      return config.getSession();
    }
 
-   static synchronized void initSession()
+   /**
+    * 
+    */
+   private static class MailSessionConfig
    {
-      if(instance == null)
+
+      private Properties properties;
+
+      private Authenticator auth;
+
+      private boolean shareSessionInstance = false;
+
+      private Session session;
+
+      protected MailSessionConfig(Properties properties, Authenticator auth, boolean shareSessionInstance)
       {
-         instance = Session.getInstance(props, auth);
+         this.properties = properties;
+         this.auth = auth;
+         this.shareSessionInstance = shareSessionInstance;
       }
+
+      public Authenticator getAuth()
+      {
+         return auth;
+      }
+
+      public Properties getProperties()
+      {
+         return properties;
+      }
+
+      public Session getSession()
+      {
+         //when share session instance is set return a cached session
+         if (isShareSessionInstance())
+         {
+            if (session == null)
+            {
+               session = Session.getInstance(getProperties(), getAuth());
+            }
+            return session;
+         }
+
+         return Session.getInstance(getProperties(), getAuth());
+      }
+
+      public boolean isShareSessionInstance()
+      {
+         return shareSessionInstance;
+      }
+
    }
+
 }



More information about the jboss-cvs-commits mailing list