[hornetq-commits] JBoss hornetq SVN: r9155 - in trunk: src/main/org/hornetq/core/journal/impl and 5 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Apr 22 19:23:36 EDT 2010


Author: clebert.suconic at jboss.com
Date: 2010-04-22 19:23:33 -0400 (Thu, 22 Apr 2010)
New Revision: 9155

Modified:
   trunk/src/main/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java
   trunk/src/main/org/hornetq/core/client/impl/FailoverManagerImpl.java
   trunk/src/main/org/hornetq/core/journal/impl/AIOSequentialFileFactory.java
   trunk/src/main/org/hornetq/core/journal/impl/AbstractSequentialFileFactory.java
   trunk/src/main/org/hornetq/core/remoting/server/impl/RemotingServiceImpl.java
   trunk/src/main/org/hornetq/core/server/impl/HornetQServerImpl.java
   trunk/src/main/org/hornetq/utils/HornetQThreadFactory.java
   trunk/tests/src/org/hornetq/tests/integration/replication/ReplicationTest.java
   trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java
   trunk/tests/src/org/hornetq/tests/unit/core/asyncio/MultiThreadAsynchronousFileTest.java
Log:
HORNETQ-366 & HORNETQ-335 - Setting classLoader on ThreadFactory and some Security Manager tweaks

Modified: trunk/src/main/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java
===================================================================
--- trunk/src/main/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/client/impl/ClientSessionFactoryImpl.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -14,6 +14,8 @@
 
 import java.io.Serializable;
 import java.net.InetAddress;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -87,7 +89,7 @@
    private List<Pair<TransportConfiguration, TransportConfiguration>> staticConnectors;
 
    private String localBindAddress;
-   
+
    private String discoveryAddress;
 
    private int discoveryPort;
@@ -141,7 +143,7 @@
    private long maxRetryInterval;
 
    private int reconnectAttempts;
-   
+
    private boolean failoverOnInitialConnection;
 
    private int initialMessagePacketSize;
@@ -157,12 +159,12 @@
    private static ScheduledExecutorService globalScheduledThreadPool;
 
    private String groupID;
-   
+
    private static synchronized ExecutorService getGlobalThreadPool()
    {
       if (ClientSessionFactoryImpl.globalThreadPool == null)
       {
-         ThreadFactory factory = new HornetQThreadFactory("HornetQ-client-global-threads", true);
+         ThreadFactory factory = new HornetQThreadFactory("HornetQ-client-global-threads", true, getThisClassLoader());
 
          ClientSessionFactoryImpl.globalThreadPool = Executors.newCachedThreadPool(factory);
       }
@@ -174,10 +176,10 @@
    {
       if (ClientSessionFactoryImpl.globalScheduledThreadPool == null)
       {
-         ThreadFactory factory = new HornetQThreadFactory("HornetQ-client-global-scheduled-threads", true);
+         ThreadFactory factory = new HornetQThreadFactory("HornetQ-client-global-scheduled-threads", true, getThisClassLoader());
 
          ClientSessionFactoryImpl.globalScheduledThreadPool = Executors.newScheduledThreadPool(HornetQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE,
-           
+
                                                                                                factory);
       }
 
@@ -195,7 +197,7 @@
       else
       {
          ThreadFactory factory = new HornetQThreadFactory("HornetQ-client-factory-threads-" + System.identityHashCode(this),
-                                                          true);
+                                                          true, getThisClassLoader());
 
          if (threadPoolMaxSize == -1)
          {
@@ -207,7 +209,7 @@
          }
 
          factory = new HornetQThreadFactory("HornetQ-client-factory-pinger-threads-" + System.identityHashCode(this),
-                                            true);
+                                            true, getThisClassLoader());
 
          scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory);
       }
@@ -224,9 +226,9 @@
          if (discoveryAddress != null)
          {
             InetAddress groupAddress = InetAddress.getByName(discoveryAddress);
-            
+
             InetAddress lbAddress;
-            
+
             if (localBindAddress != null)
             {
                lbAddress = InetAddress.getByName(localBindAddress);
@@ -261,7 +263,7 @@
                                                             retryInterval,
                                                             retryIntervalMultiplier,
                                                             maxRetryInterval,
-                                                            reconnectAttempts,     
+                                                            reconnectAttempts,
                                                             failoverOnInitialConnection,
                                                             threadPool,
                                                             scheduledThreadPool,
@@ -288,8 +290,8 @@
 
    public ClientSessionFactoryImpl(final ClientSessionFactory other)
    {
-      localBindAddress =  other.getLocalBindAddress();
-      
+      localBindAddress = other.getLocalBindAddress();
+
       discoveryAddress = other.getDiscoveryAddress();
 
       discoveryPort = other.getDiscoveryPort();
@@ -345,7 +347,7 @@
       maxRetryInterval = other.getMaxRetryInterval();
 
       reconnectAttempts = other.getReconnectAttempts();
-      
+
       failoverOnInitialConnection = other.isFailoverOnInitialConnection();
 
       failoverOnServerShutdown = other.isFailoverOnServerShutdown();
@@ -408,7 +410,7 @@
       maxRetryInterval = HornetQClient.DEFAULT_MAX_RETRY_INTERVAL;
 
       reconnectAttempts = HornetQClient.DEFAULT_RECONNECT_ATTEMPTS;
-      
+
       failoverOnInitialConnection = HornetQClient.DEFAULT_FAILOVER_ON_INITIAL_CONNECTION;
 
       failoverOnServerShutdown = HornetQClient.DEFAULT_FAILOVER_ON_SERVER_SHUTDOWN;
@@ -426,11 +428,11 @@
 
       this.discoveryPort = discoveryPort;
    }
-   
+
    public ClientSessionFactoryImpl(final String localBindAddress, final String discoveryAddress, final int discoveryPort)
    {
       this();
-      
+
       this.localBindAddress = localBindAddress;
 
       this.discoveryAddress = discoveryAddress;
@@ -737,7 +739,7 @@
       checkWrite();
       this.reconnectAttempts = reconnectAttempts;
    }
-   
+
    public synchronized boolean isFailoverOnInitialConnection()
    {
       return this.failoverOnInitialConnection;
@@ -770,7 +772,7 @@
       checkWrite();
       connectionLoadBalancingPolicyClassName = loadBalancingPolicyClassName;
    }
-   
+
    public synchronized String getLocalBindAddress()
    {
       return localBindAddress;
@@ -835,7 +837,7 @@
       checkWrite();
       initialMessagePacketSize = size;
    }
-   
+
    public ClientSession createSession(final String username,
                                       final String password,
                                       final boolean xa,
@@ -1041,7 +1043,7 @@
                                                                       retryInterval,
                                                                       retryIntervalMultiplier,
                                                                       maxRetryInterval,
-                                                                      reconnectAttempts,  
+                                                                      reconnectAttempts,
                                                                       failoverOnInitialConnection,
                                                                       threadPool,
                                                                       scheduledThreadPool,
@@ -1150,20 +1152,39 @@
          throw new IllegalStateException("Please specify a load balancing policy class name on the session factory");
       }
 
-      ClassLoader loader = Thread.currentThread().getContextClassLoader();
-      try
+      AccessController.doPrivileged(new PrivilegedAction<Object>()
       {
-         Class<?> clazz = loader.loadClass(connectionLoadBalancingPolicyClassName);
-         loadBalancingPolicy = (ConnectionLoadBalancingPolicy)clazz.newInstance();
-      }
-      catch (Exception e)
-      {
-         throw new IllegalArgumentException("Unable to instantiate load balancing policy \"" + connectionLoadBalancingPolicyClassName +
-                                                     "\"",
-                                            e);
-      }
+         public Object run()
+         {
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            try
+            {
+               Class<?> clazz = loader.loadClass(connectionLoadBalancingPolicyClassName);
+               loadBalancingPolicy = (ConnectionLoadBalancingPolicy)clazz.newInstance();
+               return null;
+            }
+            catch (Exception e)
+            {
+               throw new IllegalArgumentException("Unable to instantiate load balancing policy \"" + connectionLoadBalancingPolicyClassName +
+                                                           "\"",
+                                                  e);
+            }
+         }
+      });
    }
 
+   private static ClassLoader getThisClassLoader()
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+                                    {
+                                       public ClassLoader run()
+                                       {
+                                          return ClientSessionFactoryImpl.class.getClassLoader();
+                                       }
+                                    });
+      
+   }
+
    private synchronized void updatefailoverManagerArray()
    {
       failoverManagerArray = new FailoverManager[failoverManagerMap.size()];

Modified: trunk/src/main/org/hornetq/core/client/impl/FailoverManagerImpl.java
===================================================================
--- trunk/src/main/org/hornetq/core/client/impl/FailoverManagerImpl.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/client/impl/FailoverManagerImpl.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -486,10 +486,8 @@
                else
                {
                   HornetQException me = new HornetQException(HornetQException.INTERNAL_ERROR,
-                                                             "Failed to create session");
+                                                             "Failed to create session", t);
 
-                  me.initCause(t);
-
                   throw me;
                }
             }

Modified: trunk/src/main/org/hornetq/core/journal/impl/AIOSequentialFileFactory.java
===================================================================
--- trunk/src/main/org/hornetq/core/journal/impl/AIOSequentialFileFactory.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/journal/impl/AIOSequentialFileFactory.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -14,6 +14,8 @@
 package org.hornetq.core.journal.impl;
 
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -21,6 +23,7 @@
 
 import org.hornetq.core.asyncio.BufferCallback;
 import org.hornetq.core.asyncio.impl.AsynchronousFileImpl;
+import org.hornetq.core.client.impl.ClientSessionFactoryImpl;
 import org.hornetq.core.config.impl.ConfigurationImpl;
 import org.hornetq.core.journal.SequentialFile;
 import org.hornetq.core.logging.Logger;
@@ -143,7 +146,7 @@
       super.start();
 
       pollerExecutor = Executors.newCachedThreadPool(new HornetQThreadFactory("HornetQ-AIO-poller-pool" + System.identityHashCode(this),
-                                                                              true));
+                                                                              true, getThisClassLoader()));
 
    }
 
@@ -292,5 +295,18 @@
          }
       }
    }
+   
+   private static ClassLoader getThisClassLoader()
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+                                    {
+                                       public ClassLoader run()
+                                       {
+                                          return ClientSessionFactoryImpl.class.getClassLoader();
+                                       }
+                                    });
+      
+   }
 
+
 }

Modified: trunk/src/main/org/hornetq/core/journal/impl/AbstractSequentialFileFactory.java
===================================================================
--- trunk/src/main/org/hornetq/core/journal/impl/AbstractSequentialFileFactory.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/journal/impl/AbstractSequentialFileFactory.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -17,12 +17,15 @@
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
+import org.hornetq.core.client.impl.ClientSessionFactoryImpl;
 import org.hornetq.core.journal.SequentialFile;
 import org.hornetq.core.journal.SequentialFileFactory;
 import org.hornetq.core.logging.Logger;
@@ -114,7 +117,7 @@
       if (isSupportsCallbacks())
       {
          writeExecutor = Executors.newSingleThreadExecutor(new HornetQThreadFactory("HornetQ-Asynchronous-Persistent-Writes" + System.identityHashCode(this),
-                                                                                    true));
+                                                                                    true, getThisClassLoader()));
       }
 
    }
@@ -186,4 +189,18 @@
 
       return Arrays.asList(fileNames);
    }
+
+   private static ClassLoader getThisClassLoader()
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+                                    {
+                                       public ClassLoader run()
+                                       {
+                                          return ClientSessionFactoryImpl.class.getClassLoader();
+                                       }
+                                    });
+      
+   }
+
+
 }

Modified: trunk/src/main/org/hornetq/core/remoting/server/impl/RemotingServiceImpl.java
===================================================================
--- trunk/src/main/org/hornetq/core/remoting/server/impl/RemotingServiceImpl.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/remoting/server/impl/RemotingServiceImpl.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -13,6 +13,8 @@
 
 package org.hornetq.core.remoting.server.impl;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -140,6 +142,15 @@
       {
          return;
       }
+      
+      ClassLoader tccl =
+         AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+         {
+            public ClassLoader run()
+            {
+               return Thread.currentThread().getContextClassLoader();
+            }
+         });
 
       // The remoting service maintains it's own thread pool for handling remoting traffic
       // If OIO each connection will have it's own thread
@@ -148,7 +159,7 @@
       // to support many hundreds of connections, but the main thread pool must be kept small for better performance
 
       ThreadFactory tFactory = new HornetQThreadFactory("HornetQ-remoting-threads" + System.identityHashCode(this),
-                                                        false);
+                                                        false, tccl);
 
       threadPool = Executors.newCachedThreadPool(tFactory);
 

Modified: trunk/src/main/org/hornetq/core/server/impl/HornetQServerImpl.java
===================================================================
--- trunk/src/main/org/hornetq/core/server/impl/HornetQServerImpl.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/core/server/impl/HornetQServerImpl.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -14,6 +14,8 @@
 package org.hornetq.core.server.impl;
 
 import java.lang.management.ManagementFactory;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -37,6 +39,7 @@
 import org.hornetq.api.core.TransportConfiguration;
 import org.hornetq.api.core.client.ClientSessionFactory;
 import org.hornetq.api.core.client.HornetQClient;
+import org.hornetq.core.client.impl.ClientSessionFactoryImpl;
 import org.hornetq.core.client.impl.FailoverManager;
 import org.hornetq.core.client.impl.FailoverManagerImpl;
 import org.hornetq.core.config.Configuration;
@@ -903,7 +906,7 @@
    {
       // Create the pools - we have two pools - one for non scheduled - and another for scheduled
 
-      ThreadFactory tFactory = new HornetQThreadFactory("HornetQ-server-threads" + System.identityHashCode(this), false);
+      ThreadFactory tFactory = new HornetQThreadFactory("HornetQ-server-threads" + System.identityHashCode(this), false, getThisClassLoader());
 
       if (configuration.getThreadPoolMaxSize() == -1)
       {
@@ -917,7 +920,7 @@
       executorFactory = new OrderedExecutorFactory(threadPool);
 
       scheduledPool = new ScheduledThreadPoolExecutor(configuration.getScheduledThreadPoolMaxSize(),
-                                                      new HornetQThreadFactory("HornetQ-scheduled-threads", false));
+                                                      new HornetQThreadFactory("HornetQ-scheduled-threads", false, getThisClassLoader()));
 
       managementService = new ManagementServiceImpl(mbeanServer, configuration);
 
@@ -1442,6 +1445,19 @@
          throw new IllegalArgumentException("Error instantiating class \"" + className + "\"", e);
       }
    }
+   
+   private static ClassLoader getThisClassLoader()
+   {
+      return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+                                    {
+                                       public ClassLoader run()
+                                       {
+                                          return ClientSessionFactoryImpl.class.getClassLoader();
+                                       }
+                                    });
+      
+   }
+   
 
    // Inner classes
    // --------------------------------------------------------------------------------

Modified: trunk/src/main/org/hornetq/utils/HornetQThreadFactory.java
===================================================================
--- trunk/src/main/org/hornetq/utils/HornetQThreadFactory.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/src/main/org/hornetq/utils/HornetQThreadFactory.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -12,6 +12,8 @@
  */
 package org.hornetq.utils;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -35,24 +37,28 @@
    private final int threadPriority;
 
    private final boolean daemon;
+   
+   private final ClassLoader tccl;
 
-   public HornetQThreadFactory(final String groupName, final boolean daemon)
+   public HornetQThreadFactory(final String groupName, final boolean daemon, final ClassLoader tccl)
    {
-      this(groupName, Thread.NORM_PRIORITY, daemon);
+      this(groupName, Thread.NORM_PRIORITY, daemon, tccl);
    }
 
-   public HornetQThreadFactory(final String groupName, final int threadPriority, final boolean daemon)
+   public HornetQThreadFactory(final String groupName, final int threadPriority, final boolean daemon, final ClassLoader tccl)
    {
       group = new ThreadGroup(groupName + "-" + System.identityHashCode(this));
 
       this.threadPriority = threadPriority;
+      
+      this.tccl = tccl;
 
       this.daemon = daemon;
    }
 
    public Thread newThread(final Runnable command)
    {
-      Thread t = null;
+      final Thread t;
       // attach the thread to a group only if there is no security manager:
       // when sandboxed, the code does not have the RuntimePermission modifyThreadGroup
       if (System.getSecurityManager() == null)
@@ -64,8 +70,32 @@
          t = new Thread(command, "Thread-" + threadCount.getAndIncrement());
       }
       
-      t.setDaemon(daemon);
-      t.setPriority(threadPriority);
+      AccessController.doPrivileged(new PrivilegedAction<Object>()
+      {
+         public Object run()
+         {
+            t.setDaemon(daemon);
+            t.setPriority(threadPriority);
+            return null;
+         }
+      });
+        
+      try
+      {
+         AccessController.doPrivileged(new PrivilegedAction<Object>()
+         {
+            public Object run()
+            {
+               t.setContextClassLoader(tccl);
+               return null;
+            }
+         });
+      }
+      catch (java.security.AccessControlException e)
+      {
+         log.warn("Missing privileges to set Thread Context Class Loader on Thread Factory. Using current Thread Context Class Loader");
+      }
+
       return t;
    }
 }

Modified: trunk/tests/src/org/hornetq/tests/integration/replication/ReplicationTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/integration/replication/ReplicationTest.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/tests/src/org/hornetq/tests/integration/replication/ReplicationTest.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -677,7 +677,7 @@
    {
       super.setUp();
 
-      tFactory = new HornetQThreadFactory("HornetQ-ReplicationTest", false);
+      tFactory = new HornetQThreadFactory("HornetQ-ReplicationTest", false, this.getClass().getClassLoader());
 
       executor = Executors.newCachedThreadPool(tFactory);
 

Modified: trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/tests/src/org/hornetq/tests/unit/core/asyncio/AsynchronousFileTest.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -72,7 +72,7 @@
    {
       super.setUp();
       pollerExecutor = Executors.newCachedThreadPool(new HornetQThreadFactory("HornetQ-AIO-poller-pool" + System.identityHashCode(this),
-                                                                              false));
+                                                                              false, this.getClass().getClassLoader()));
       executor = Executors.newSingleThreadExecutor();
    }
 

Modified: trunk/tests/src/org/hornetq/tests/unit/core/asyncio/MultiThreadAsynchronousFileTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/unit/core/asyncio/MultiThreadAsynchronousFileTest.java	2010-04-22 21:53:41 UTC (rev 9154)
+++ trunk/tests/src/org/hornetq/tests/unit/core/asyncio/MultiThreadAsynchronousFileTest.java	2010-04-22 23:23:33 UTC (rev 9155)
@@ -72,7 +72,7 @@
    {
       super.setUp();
       pollerExecutor = Executors.newCachedThreadPool(new HornetQThreadFactory("HornetQ-AIO-poller-pool" + System.identityHashCode(this),
-                                                                              false));
+                                                                              false, this.getClass().getClassLoader()));
       executor = Executors.newSingleThreadExecutor();
    }
 



More information about the hornetq-commits mailing list