activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andytay...@apache.org
Subject [1/2] activemq-artemis git commit: [ARTEMIS-294] Use doPriviledged block for ServiceLoader
Date Wed, 04 Nov 2015 11:52:00 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master 8f1848108 -> 95a8570de


[ARTEMIS-294] Use doPriviledged block for ServiceLoader

In ServiceUtils, loads services from an AccessController.doPriviledged
block and use the ServiceUtils's own classloader instead of the TCCL
(that may be different depending on who's requesting a managed
connection factory first)

JIRA: https://issues.apache.org/jira/browse/ARTEMIS-294


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/7d6a6de8
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/7d6a6de8
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/7d6a6de8

Branch: refs/heads/master
Commit: 7d6a6de8dde9700995771daddcf505b78cd58e45
Parents: 8f18481
Author: Jeff Mesnil <jmesnil@gmail.com>
Authored: Wed Nov 4 11:38:43 2015 +0100
Committer: Jeff Mesnil <jmesnil@gmail.com>
Committed: Wed Nov 4 12:26:48 2015 +0100

----------------------------------------------------------------------
 .../service/extensions/ServiceUtils.java        | 55 ++++++++++++++------
 .../extensions/tests/xa/ServiceUtilsTest.java   | 36 +++----------
 ...tensions.xa.ActiveMQXAResourceWrapperFactory |  1 +
 3 files changed, 47 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/7d6a6de8/artemis-service-extensions/src/main/java/org/apache/activemq/artemis/service/extensions/ServiceUtils.java
----------------------------------------------------------------------
diff --git a/artemis-service-extensions/src/main/java/org/apache/activemq/artemis/service/extensions/ServiceUtils.java
b/artemis-service-extensions/src/main/java/org/apache/activemq/artemis/service/extensions/ServiceUtils.java
index 6452c6e..99a372b 100644
--- a/artemis-service-extensions/src/main/java/org/apache/activemq/artemis/service/extensions/ServiceUtils.java
+++ b/artemis-service-extensions/src/main/java/org/apache/activemq/artemis/service/extensions/ServiceUtils.java
@@ -16,12 +16,15 @@
  */
 package org.apache.activemq.artemis.service.extensions;
 
-import javax.transaction.TransactionManager;
-import javax.transaction.xa.XAResource;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.ServiceLoader;
 
+import javax.transaction.TransactionManager;
+import javax.transaction.xa.XAResource;
+
 import org.apache.activemq.artemis.service.extensions.transactions.TransactionManagerLocator;
 import org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapper;
 import org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory;
@@ -37,7 +40,7 @@ public class ServiceUtils {
 
    private static ActiveMQXAResourceWrapperFactory getActiveMQXAResourceWrapperFactory()
{
       if (activeMQXAResourceWrapperFactory == null) {
-         setActiveMQXAResourceWrapperFactory(ServiceLoader.load(ActiveMQXAResourceWrapperFactory.class));
+         activeMQXAResourceWrapperFactory = findActiveMQXAResourceWrapperFactory();
       }
       return activeMQXAResourceWrapperFactory;
    }
@@ -48,11 +51,7 @@ public class ServiceUtils {
 
    public static synchronized TransactionManager getTransactionManager() {
       if (!transactionManagerLoaded) {
-         Iterator<TransactionManagerLocator> it = ServiceLoader.load(TransactionManagerLocator.class).iterator();
-         while (it.hasNext() && transactionManager == null) {
-            transactionManager = it.next().getTransactionManager();
-         }
-
+         transactionManager = findTransactionManager();
          if (transactionManager != null) {
             transactionManagerLoaded = true;
          }
@@ -68,13 +67,39 @@ public class ServiceUtils {
       transactionManagerLoaded = (transactionManager != null);
    }
 
-   private static void setActiveMQXAResourceWrapperFactory(Iterable<ActiveMQXAResourceWrapperFactory>
iterable) {
-      if (iterable.iterator().hasNext()) {
-         activeMQXAResourceWrapperFactory = iterable.iterator().next();
-      }
-      else {
-         activeMQXAResourceWrapperFactory = new ActiveMQXAResourceWrapperFactoryImpl();
-      }
+
+   /**
+    *  Find the <em>first</em> transaction manager loaded from the {@code TransactionManagerLocator}
service or {@code null} if none is loaded.
+    */
+   private static TransactionManager findTransactionManager() {
+      return AccessController.doPrivileged(new PrivilegedAction<TransactionManager>()
{
+         @Override
+         public TransactionManager run() {
+            Iterator<TransactionManagerLocator> it = ServiceLoader.load(TransactionManagerLocator.class,
ServiceUtils.class.getClassLoader()).iterator();
+            while (it.hasNext() && transactionManager == null) {
+               transactionManager = it.next().getTransactionManager();
+            }
+            return transactionManager;
+         }
+      });
    }
 
+   /**
+    *  Find the <em>first</em> wrapper factory loaded from the {@code ActiveMQXAResourceWrapperFactory}
service or
+    *  use the default {@code ActiveMQXAResourceWrapperFactoryImpl} if none is loaded.
+    */
+   private static ActiveMQXAResourceWrapperFactory findActiveMQXAResourceWrapperFactory()
{
+      return AccessController.doPrivileged(new PrivilegedAction<ActiveMQXAResourceWrapperFactory>()
{
+         @Override
+         public ActiveMQXAResourceWrapperFactory run() {
+            Iterator<ActiveMQXAResourceWrapperFactory> iterator = ServiceLoader.load(ActiveMQXAResourceWrapperFactory.class,
ServiceUtils.class.getClassLoader()).iterator();
+            if (iterator.hasNext()) {
+               return iterator.next();
+            }
+            else {
+               return new ActiveMQXAResourceWrapperFactoryImpl();
+            }
+         }
+      });
+   }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/7d6a6de8/artemis-service-extensions/src/test/java/org/apache/activemq/artemis/service/extensions/tests/xa/ServiceUtilsTest.java
----------------------------------------------------------------------
diff --git a/artemis-service-extensions/src/test/java/org/apache/activemq/artemis/service/extensions/tests/xa/ServiceUtilsTest.java
b/artemis-service-extensions/src/test/java/org/apache/activemq/artemis/service/extensions/tests/xa/ServiceUtilsTest.java
index 90754bd..5e9db00 100644
--- a/artemis-service-extensions/src/test/java/org/apache/activemq/artemis/service/extensions/tests/xa/ServiceUtilsTest.java
+++ b/artemis-service-extensions/src/test/java/org/apache/activemq/artemis/service/extensions/tests/xa/ServiceUtilsTest.java
@@ -16,44 +16,20 @@
  */
 package org.apache.activemq.artemis.service.extensions.tests.xa;
 
-import java.lang.reflect.Field;
+import static org.jgroups.util.Util.assertTrue;
+
 import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.activemq.artemis.service.extensions.ServiceUtils;
-import org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory;
-import org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactoryImpl;
 import org.junit.Test;
 
-import static org.jgroups.util.Util.assertTrue;
-
 public class ServiceUtilsTest {
 
    @Test
-   public void testSetActiveMQXAResourceWrapperFactorySetsDefaultImplWhenNoOther() throws
Exception {
-      List<ActiveMQXAResourceWrapperFactory> factories = new ArrayList<ActiveMQXAResourceWrapperFactory>();
-
-      Method method = ServiceUtils.class.getDeclaredMethod("setActiveMQXAResourceWrapperFactory",
Iterable.class);
-      method.setAccessible(true);
-      method.invoke(null, factories);
-
-      Field field = ServiceUtils.class.getDeclaredField("activeMQXAResourceWrapperFactory");
-      field.setAccessible(true);
-      assertTrue(field.get(null) instanceof ActiveMQXAResourceWrapperFactoryImpl);
-   }
-
-   @Test
-   public void testSetActiveMQXAResourceWrapperFactorySetsExtensionImplWhenSupplied() throws
Exception {
-      List<ActiveMQXAResourceWrapperFactory> factories = new ArrayList<ActiveMQXAResourceWrapperFactory>();
-      factories.add(new MockActiveMQResourceWrapperFactory());
-
-      Method method = ServiceUtils.class.getDeclaredMethod("setActiveMQXAResourceWrapperFactory",
Iterable.class);
+   public void testGetActiveMQXAResourceWrapperFactoryLoadsService() throws Exception {
+      Method method = ServiceUtils.class.getDeclaredMethod("getActiveMQXAResourceWrapperFactory");
       method.setAccessible(true);
-      method.invoke(null, factories);
-
-      Field field = ServiceUtils.class.getDeclaredField("activeMQXAResourceWrapperFactory");
-      field.setAccessible(true);
-      assertTrue(field.get(null) instanceof MockActiveMQResourceWrapperFactory);
+      Object o = method.invoke(null);
+      assertTrue(o instanceof MockActiveMQResourceWrapperFactory);
    }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/7d6a6de8/artemis-service-extensions/src/test/resources/META-INF/services/org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory
----------------------------------------------------------------------
diff --git a/artemis-service-extensions/src/test/resources/META-INF/services/org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory
b/artemis-service-extensions/src/test/resources/META-INF/services/org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory
new file mode 100644
index 0000000..b778190
--- /dev/null
+++ b/artemis-service-extensions/src/test/resources/META-INF/services/org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperFactory
@@ -0,0 +1 @@
+org.apache.activemq.artemis.service.extensions.tests.xa.MockActiveMQResourceWrapperFactory
\ No newline at end of file


Mime
View raw message