activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gtu...@apache.org
Subject svn commit: r1463528 - in /activemq/trunk: activemq-osgi/pom.xml activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java
Date Tue, 02 Apr 2013 13:26:08 GMT
Author: gtully
Date: Tue Apr  2 13:26:08 2013
New Revision: 1463528

URL: http://svn.apache.org/r1463528
Log:
https://issues.apache.org/jira/browse/AMQ-4426 - fix and tests

Modified:
    activemq/trunk/activemq-osgi/pom.xml
    activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java
    activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java

Modified: activemq/trunk/activemq-osgi/pom.xml
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-osgi/pom.xml?rev=1463528&r1=1463527&r2=1463528&view=diff
==============================================================================
--- activemq/trunk/activemq-osgi/pom.xml (original)
+++ activemq/trunk/activemq-osgi/pom.xml Tue Apr  2 13:26:08 2013
@@ -63,6 +63,7 @@
       javax.jms*,
       javax.management*,
       javax.transaction*,
+      javax.naming*;resolution:=optional,
       org.apache.commons.pool*;resolution:=optional,
       org.apache.commons.net*;resolution:=optional,
       com.sun.syndication*;resolution:=optional,

Modified: activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java?rev=1463528&r1=1463527&r2=1463528&view=diff
==============================================================================
--- activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java
(original)
+++ activemq/trunk/activemq-pool/src/main/java/org/apache/activemq/pool/XaPooledConnectionFactory.java
Tue Apr  2 13:26:08 2013
@@ -16,18 +16,53 @@
  */
 package org.apache.activemq.pool;
 
+import java.io.Serializable;
+import java.util.Hashtable;
+import javax.jms.JMSException;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.TopicConnection;
+import javax.jms.TopicConnectionFactory;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.Name;
+import javax.naming.spi.ObjectFactory;
 import javax.transaction.TransactionManager;
 
 import org.apache.activemq.ActiveMQConnection;
 import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.util.IntrospectionSupport;
 
 /**
  * A pooled connection factory that automatically enlists
  * sessions in the current active XA transaction if any.
  */
-public class XaPooledConnectionFactory extends PooledConnectionFactory {
+public class XaPooledConnectionFactory extends PooledConnectionFactory implements ObjectFactory,
Serializable, QueueConnectionFactory, TopicConnectionFactory
+{
 
     private TransactionManager transactionManager;
+    private boolean tmFromJndi = false;
+    private String tmJndiName = "java:/TransactionManager";
+
+    public String getTmJndiName() {
+        return tmJndiName;
+    }
+
+    public void setTmJndiName(String tmJndiName) {
+        this.tmJndiName = tmJndiName;
+    }
+
+    public boolean isTmFromJndi() {
+        return tmFromJndi;
+    }
+
+    /**
+     * Allow transaction manager resolution from JNDI (ee deployment)
+     * @param tmFromJndi
+     */
+    public void setTmFromJndi(boolean tmFromJndi) {
+        this.tmFromJndi = tmFromJndi;
+    }
 
     public XaPooledConnectionFactory() {
         super();
@@ -42,6 +77,13 @@ public class XaPooledConnectionFactory e
     }
 
     public TransactionManager getTransactionManager() {
+        if (transactionManager == null && tmFromJndi) {
+            try {
+                transactionManager = (TransactionManager) new InitialContext().lookup(getTmJndiName());
+            } catch (Throwable ignored) {
+                ignored.printStackTrace();
+            }
+        }
         return transactionManager;
     }
 
@@ -53,4 +95,33 @@ public class XaPooledConnectionFactory e
     protected ConnectionPool createConnectionPool(ActiveMQConnection connection) {
         return new XaConnectionPool(connection, getTransactionManager());
     }
+
+    @Override
+    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?,
?> environment) throws Exception {
+        setTmFromJndi(true);
+        if (environment != null) {
+            IntrospectionSupport.setProperties(this, environment);
+        }
+        return this;
+    }
+
+    @Override
+    public QueueConnection createQueueConnection() throws JMSException {
+        return (QueueConnection) createConnection();
+    }
+
+    @Override
+    public QueueConnection createQueueConnection(String userName, String password) throws
JMSException {
+        return (QueueConnection) createConnection(userName, password);
+    }
+
+    @Override
+    public TopicConnection createTopicConnection() throws JMSException {
+        return (TopicConnection) createConnection();
+    }
+
+    @Override
+    public TopicConnection createTopicConnection(String userName, String password) throws
JMSException {
+        return (TopicConnection) createConnection(userName, password);
+    }
 }

Modified: activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java?rev=1463528&r1=1463527&r2=1463528&view=diff
==============================================================================
--- activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java
(original)
+++ activemq/trunk/activemq-pool/src/test/java/org/apache/activemq/pool/XAConnectionPoolTest.java
Tue Apr  2 13:26:08 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.activemq.pool;
 
+import java.util.Hashtable;
 import java.util.Vector;
+import javax.jms.QueueConnectionFactory;
 import javax.jms.Session;
 import javax.jms.TopicConnection;
+import javax.jms.TopicConnectionFactory;
 import javax.jms.TopicPublisher;
 import javax.jms.TopicSession;
+import javax.naming.spi.ObjectFactory;
 import javax.transaction.HeuristicMixedException;
 import javax.transaction.HeuristicRollbackException;
 import javax.transaction.InvalidTransactionException;
@@ -140,4 +144,27 @@ public class XAConnectionPoolTest extend
         }
         connection.close();
     }
+
+    public void testInstanceOf() throws  Exception {
+        XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
+        assertTrue(pcf instanceof QueueConnectionFactory);
+        assertTrue(pcf instanceof TopicConnectionFactory);
+    }
+
+    public void testBindable() throws Exception {
+        XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
+        assertTrue(pcf  instanceof ObjectFactory);
+        assertTrue(((ObjectFactory)pcf).getObjectInstance(null, null, null, null) instanceof
XaPooledConnectionFactory);
+        assertTrue(pcf.isTmFromJndi());
+    }
+
+    public void testBindableEnvOverrides() throws Exception {
+        XaPooledConnectionFactory pcf = new XaPooledConnectionFactory();
+        assertTrue(pcf  instanceof ObjectFactory);
+        Hashtable<String, String> environment = new Hashtable<String, String>();
+        environment.put("tmFromJndi", String.valueOf(Boolean.FALSE));
+        assertTrue(((ObjectFactory) pcf).getObjectInstance(null, null, null, environment)
instanceof XaPooledConnectionFactory);
+        assertFalse(pcf.isTmFromJndi());
+    }
+
 }



Mime
View raw message