[jboss-cvs] JBossAS SVN: r77589 - in trunk/varia: src/main/org/jboss/mail and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Aug 28 03:12:15 EDT 2008


Author: scott.stark at jboss.org
Date: 2008-08-28 03:12:15 -0400 (Thu, 28 Aug 2008)
New Revision: 77589

Added:
   trunk/varia/src/main/org/jboss/mail/SessionObjectFactory.java
Modified:
   trunk/varia/build.xml
   trunk/varia/src/main/org/jboss/mail/MailService.java
Log:
JBAS-5896, add a sharedSession attribute which controls whether a single mail session should be shared across all lookups (sharedSession = true) or a new session created on each lookup (sharedSession = false, the default). 

Modified: trunk/varia/build.xml
===================================================================
--- trunk/varia/build.xml	2008-08-28 06:46:41 UTC (rev 77588)
+++ trunk/varia/build.xml	2008-08-28 07:12:15 UTC (rev 77589)
@@ -344,7 +344,7 @@
     <jar jarfile="${build.lib}/mail-plugin.jar"
       manifest="${build.etc}/default.mf">
       <fileset dir="${build.classes}">
-        <include name="org/jboss/mail/MailService*.class"/>
+        <include name="org/jboss/mail/*"/>
       </fileset>
     </jar>
 

Modified: trunk/varia/src/main/org/jboss/mail/MailService.java
===================================================================
--- trunk/varia/src/main/org/jboss/mail/MailService.java	2008-08-28 06:46:41 UTC (rev 77588)
+++ trunk/varia/src/main/org/jboss/mail/MailService.java	2008-08-28 07:12:15 UTC (rev 77589)
@@ -34,10 +34,7 @@
 import javax.naming.InitialContext;
 import javax.naming.Reference;
 import javax.naming.StringRefAddr;
-import javax.naming.Name;
-import javax.naming.Context;
 import javax.naming.NamingException;
-import javax.naming.NameNotFoundException;
 
 import javax.mail.Session;
 import javax.mail.PasswordAuthentication;
@@ -45,6 +42,7 @@
 
 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
@@ -63,13 +61,17 @@
    implements MailServiceMBean
 {
    public static final String JNDI_NAME = "java:/Mail";
-
+   /** */
    private String user;
+   /** */
    private String password;
+   /** */
    private String jndiName = JNDI_NAME;
+   /** Whether lookups share a single session */
+   private boolean sharedSession = false;
    private Element config;
 
-   /** Object Name of the JSR-77 representant of this service */
+   /** Object Name of the JSR-77 representation of this service */
    ObjectName mMail;
 
    /** save properties here */
@@ -152,7 +154,22 @@
       return jndiName;
    }
 
+   public boolean isSharedSession()
+   {
+      return sharedSession;
+   }
    /**
+    * Set whether a single mail session should be shared across all lookups
+    * (sharedSession = true) or a new session created on each lookup
+    * (sharedSession = false, the default).
+    * @param sharedSession
+    */
+   public void setSharedSession(boolean sharedSession)
+   {
+      this.sharedSession = sharedSession;
+   }
+
+   /**
     * @jmx:managed-attribute
     */
    public String getStoreProtocol()
@@ -228,9 +245,8 @@
 
       Properties props = getProperties();
 
-      // Finally create a mail session
-      Session session = Session.getInstance(props, a);
-      bind(session);
+      // Finally bind a mail session
+      bind(props, a);
       
       // now make the properties available
       ourProps = props;
@@ -307,41 +323,21 @@
       unbind();
    }
 
-   private void bind(Session session) throws NamingException
+   private void bind(Properties props, Authenticator auth) throws NamingException
    {
       String bindName = getJNDIName();
+      SessionObjectFactory.setSessionFactoryInfo(props, auth);
+      SessionObjectFactory.setShareSessionInstance(sharedSession);
 
-      // Ah ! Session isn't serializable, so we use a helper class
-      NonSerializableFactory.bind(bindName, session);
-
-      Context ctx = new InitialContext();
+      InitialContext ctx = new InitialContext();
       try
       {
-         Name n = ctx.getNameParser("").parse(bindName);
-         while (n.size() > 1)
-         {
-            String ctxName = n.get(0);
-            try
-            {
-               ctx = (Context) ctx.lookup(ctxName);
-            }
-            catch (NameNotFoundException e)
-            {
-               ctx = ctx.createSubcontext(ctxName);
-            }
-            n = n.getSuffix(1);
-         }
-
-
-         // The helper class NonSerializableFactory uses address type nns, we go on to
-         // use the helper class to bind the javax.mail.Session object in JNDI
-
          StringRefAddr addr = new StringRefAddr("nns", bindName);
          Reference ref = new Reference(Session.class.getName(),
             addr,
-            NonSerializableFactory.class.getName(),
+            SessionObjectFactory.class.getName(),
             null);
-         ctx.bind(n.get(0), ref);
+         Util.bind(ctx, bindName, ref);
       }
       finally
       {
@@ -367,7 +363,7 @@
             ctx.close();
          }
 
-         NonSerializableFactory.unbind(bindName);
+         SessionObjectFactory.setSessionFactoryInfo(null, null);
          log.info("Mail service '" + getJNDIName() + "' removed from JNDI");
       }
    }

Added: trunk/varia/src/main/org/jboss/mail/SessionObjectFactory.java
===================================================================
--- trunk/varia/src/main/org/jboss/mail/SessionObjectFactory.java	                        (rev 0)
+++ trunk/varia/src/main/org/jboss/mail/SessionObjectFactory.java	2008-08-28 07:12:15 UTC (rev 77589)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.mail;
+
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.mail.Authenticator;
+import javax.mail.Session;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.spi.ObjectFactory;
+
+/**
+ * A jndi ObjectFactory implementation that creates a new Session from the
+ * static class information on each getObjectInstance call.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @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)
+   {
+      SessionObjectFactory.props = props;
+      SessionObjectFactory.auth = auth;
+   }
+   
+   static void setShareSessionInstance(boolean shareSessionInstance)
+   {
+      SessionObjectFactory.shareSessionInstance = shareSessionInstance;
+   }
+
+   public Object getObjectInstance(Object obj, Name name, Context nameCtx,
+         Hashtable<?, ?> environment) throws Exception
+   {
+      Session session = null;
+      if(shareSessionInstance)
+      {
+         initSession();
+         session = instance;
+      }
+      else
+      {
+         session = Session.getInstance(props, auth);
+      }
+      return session;
+   }
+
+   static synchronized void initSession()
+   {
+      if(instance == null)
+      {
+         instance = Session.getInstance(props, auth);
+      }
+   }
+}


Property changes on: trunk/varia/src/main/org/jboss/mail/SessionObjectFactory.java
___________________________________________________________________
Name: svn:keywords
   + Revision




More information about the jboss-cvs-commits mailing list