# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: /home/mlowe/Projects/JBPAPP_5_0_1/varia/src/main/org/jboss/mail
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: MailService.java
--- MailService.java Base (BASE)
+++ MailService.java Locally Modified (Based On LOCAL)
@@ -41,7 +41,7 @@
 import javax.mail.Authenticator;
 
 import org.jboss.system.ServiceMBeanSupport;
-import org.jboss.naming.NonSerializableFactory;
+
 import org.jboss.util.naming.Util;
 
 /**
@@ -326,8 +326,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 +362,7 @@
             ctx.close();
          }
 
-         SessionObjectFactory.setSessionFactoryInfo(null, null);
+         SessionObjectFactory.remove(bindName);
          log.info("Mail service '" + getJNDIName() + "' removed from JNDI");
       }
    }
Index: SessionObjectFactory.java
--- SessionObjectFactory.java Base (BASE)
+++ SessionObjectFactory.java Locally Modified (Based On LOCAL)
@@ -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;
@@ -37,45 +39,67 @@
  * @author Scott.Stark@jboss.org
  * @version $Revision: 85945 $
  */
-public class SessionObjectFactory implements ObjectFactory
-{
-   private static Properties props;
-   private static Authenticator auth;
-   private static Session instance;
-   private static boolean shareSessionInstance;
+public class SessionObjectFactory implements ObjectFactory {
 
-   static void setSessionFactoryInfo(Properties props, Authenticator auth)
-   {
-      SessionObjectFactory.props = props;
-      SessionObjectFactory.auth = auth;
+    private static Map<String,MailSessionConfig> mailSessions = new HashMap<String, MailSessionConfig>();
+
+    static void setSessionFactoryInfo(String bindName,Properties props, Authenticator auth,boolean shareSessionInstance) {
+
+        mailSessions.put(bindName, new MailSessionConfig(props, auth,shareSessionInstance));
+
    }
    
-   static void setShareSessionInstance(boolean shareSessionInstance)
-   {
-      SessionObjectFactory.shareSessionInstance = shareSessionInstance;
+    static void remove(String bindName) {
+        mailSessions.remove(bindName);
    }
 
+
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
-         Hashtable<?, ?> environment) throws Exception
-   {
-      Session session = null;
-      if(shareSessionInstance)
-      {
-         initSession();
-         session = instance;
+         Hashtable<?, ?> environment) throws Exception {
+
+        if(name == null) {
+            //TODO is it can be null then this case needs handling
+            throw new IllegalArgumentException("Name arguement cannot be null");
       }
-      else
-      {
-         session = Session.getInstance(props, auth);
-      }
+
+        MailSessionConfig config = mailSessions.get(name.toString());
+
+        Session session = Session.getInstance(config.getProperties(), config.getAuth());
+
       return session;
+
+
    }
 
-   static synchronized void initSession()
-   {
-      if(instance == null)
-      {
-         instance = Session.getInstance(props, auth);
+
+    /**
+     * 
+     */
+    private static class MailSessionConfig {
+
+        private Properties properties;
+        private Authenticator auth;
+        private boolean shareSessionInstance = false;
+
+        protected MailSessionConfig(Properties properties,
+                    Authenticator auth,boolean shareSessionInstance) {
+            this.properties = properties;
+            this.auth = auth;
+            this.shareSessionInstance = shareSessionInstance;
       }
+
+        public Authenticator getAuth() {
+            return auth;
    }
+
+        public Properties getProperties() {
+            return properties;
 }
+
+        public boolean isShareSessionInstance() {
+            return shareSessionInstance;
+        }
+
+    }
+
+}
