ARTEMIS-274 ActiveMQThreadFactory has to be constructed within doPrivileged block

This commit is contained in:
Ivo Studensky 2015-10-22 14:22:41 +02:00 committed by Clebert Suconic
parent 852a5717ac
commit 8a2ecff4fd
9 changed files with 85 additions and 85 deletions

View File

@ -36,6 +36,14 @@ public final class ActiveMQThreadFactory implements ThreadFactory {
private final AccessControlContext acc;
/**
* Construct a new instance. The access control context of the calling thread will be the one used to create
* new threads if a security manager is installed.
*
* @param groupName the name of the thread group to assign threads to by default
* @param daemon whether the created threads should be daemon threads
* @param tccl the context class loader of newly created threads
*/
public ActiveMQThreadFactory(final String groupName, final boolean daemon, final ClassLoader tccl) {
group = new ThreadGroup(groupName + "-" + System.identityHashCode(this));
@ -45,12 +53,12 @@ public final class ActiveMQThreadFactory implements ThreadFactory {
this.daemon = daemon;
this.acc = (System.getSecurityManager() == null) ? null : AccessController.getContext();
this.acc = AccessController.getContext();
}
public Thread newThread(final Runnable command) {
// create a thread in a privileged block if running with Security Manager
if (acc != null && System.getSecurityManager() != null) {
if (acc != null) {
return AccessController.doPrivileged(new ThreadCreateAction(command), acc);
}
else {
@ -76,7 +84,6 @@ public final class ActiveMQThreadFactory implements ThreadFactory {
t.setDaemon(daemon);
t.setPriority(threadPriority);
t.setContextClassLoader(tccl);
return t;
}

View File

@ -242,7 +242,12 @@ public final class ServerLocatorImpl implements ServerLocatorInternal, Discovery
private static synchronized ExecutorService getGlobalThreadPool() {
if (globalThreadPool == null) {
ThreadFactory factory = new ActiveMQThreadFactory("ActiveMQ-client-global-threads", true, getThisClassLoader());
ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-client-global-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
}
});
globalThreadPool = Executors.newCachedThreadPool(factory);
}
@ -252,7 +257,12 @@ public final class ServerLocatorImpl implements ServerLocatorInternal, Discovery
private static synchronized ScheduledExecutorService getGlobalScheduledThreadPool() {
if (globalScheduledThreadPool == null) {
ThreadFactory factory = new ActiveMQThreadFactory("ActiveMQ-client-global-scheduled-threads", true, getThisClassLoader());
ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-client-global-scheduled-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
}
});
globalScheduledThreadPool = Executors.newScheduledThreadPool(ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE,
@ -274,7 +284,12 @@ public final class ServerLocatorImpl implements ServerLocatorInternal, Discovery
else {
this.shutdownPool = true;
ThreadFactory factory = new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, getThisClassLoader());
ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader());
}
});
if (threadPoolMaxSize == -1) {
threadPool = Executors.newCachedThreadPool(factory);
@ -283,19 +298,15 @@ public final class ServerLocatorImpl implements ServerLocatorInternal, Discovery
threadPool = Executors.newFixedThreadPool(threadPoolMaxSize, factory);
}
factory = new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, getThisClassLoader());
scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory);
}
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClientSessionFactoryImpl.class.getClassLoader();
factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader());
}
});
scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory);
}
}
private void instantiateLoadBalancingPolicy() {

View File

@ -44,14 +44,6 @@ public class SharedNioEventLoopGroup extends NioEventLoopGroup {
super(numThreads, factory);
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClientSessionFactoryImpl.class.getClassLoader();
}
});
}
public static synchronized void forceShutdown() {
if (instance != null) {
instance.shutdown();
@ -68,7 +60,12 @@ public class SharedNioEventLoopGroup extends NioEventLoopGroup {
}
}
else {
instance = new SharedNioEventLoopGroup(numThreads, new ActiveMQThreadFactory("ActiveMQ-client-netty-threads", true, getThisClassLoader()));
instance = new SharedNioEventLoopGroup(numThreads, AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-client-netty-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
}
}));
}
instance.nioChannelFactoryCount.incrementAndGet();
return instance;

View File

@ -112,7 +112,12 @@ public abstract class AbstractSequentialFileFactory implements SequentialFileFac
}
if (isSupportsCallbacks()) {
writeExecutor = Executors.newSingleThreadExecutor(new ActiveMQThreadFactory("ActiveMQ-Asynchronous-Persistent-Writes" + System.identityHashCode(this), true, AbstractSequentialFileFactory.getThisClassLoader()));
writeExecutor = Executors.newSingleThreadExecutor(AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {
@Override
public ActiveMQThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-Asynchronous-Persistent-Writes" + System.identityHashCode(this), true, AbstractSequentialFileFactory.class.getClassLoader());
}
}));
}
}
@ -177,13 +182,4 @@ public abstract class AbstractSequentialFileFactory implements SequentialFileFac
return Arrays.asList(fileNames);
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return AbstractSequentialFileFactory.class.getClassLoader();
}
});
}
}

View File

@ -186,7 +186,12 @@ public final class AIOSequentialFileFactory extends AbstractSequentialFileFactor
this.running.set(true);
pollerExecutor = Executors.newCachedThreadPool(new ActiveMQThreadFactory("ActiveMQ-AIO-poller-pool" + System.identityHashCode(this), true, AIOSequentialFileFactory.getThisClassLoader()));
pollerExecutor = Executors.newCachedThreadPool(AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {
@Override
public ActiveMQThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-AIO-poller-pool" + System.identityHashCode(this), true, AIOSequentialFileFactory.class.getClassLoader());
}
}));
pollerExecutor.execute(new PollerRunnable());
}
@ -431,15 +436,6 @@ public final class AIOSequentialFileFactory extends AbstractSequentialFileFactor
}
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return AIOSequentialFileFactory.class.getClassLoader();
}
});
}
@Override
public String toString() {
return AIOSequentialFileFactory.class.getSimpleName() + "(buffersControl.stopped=" + buffersControl.stopped +

View File

@ -1813,7 +1813,12 @@ public class JournalStorageManager implements StorageManager {
cleanupIncompleteFiles();
singleThreadExecutor = Executors.newSingleThreadExecutor(new ActiveMQThreadFactory("ActiveMQ-IO-SingleThread", true, getThisClassLoader()));
singleThreadExecutor = Executors.newSingleThreadExecutor(AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {
@Override
public ActiveMQThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-IO-SingleThread", true, JournalStorageManager.class.getClassLoader());
}
}));
bindingsJournal.start();
@ -2287,14 +2292,6 @@ public class JournalStorageManager implements StorageManager {
}
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return JournalStorageManager.class.getClassLoader();
}
});
}
// Inner Classes
// ----------------------------------------------------------------------------

View File

@ -266,7 +266,12 @@ public class NettyAcceptor implements Acceptor {
threadsToUse = this.nioRemotingThreads;
}
channelClazz = NioServerSocketChannel.class;
eventLoopGroup = new NioEventLoopGroup(threadsToUse, new ActiveMQThreadFactory("activemq-netty-threads", true, getThisClassLoader()));
eventLoopGroup = new NioEventLoopGroup(threadsToUse, AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {
@Override
public ActiveMQThreadFactory run() {
return new ActiveMQThreadFactory("activemq-netty-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
}
}));
}
bootstrap = new ServerBootstrap();
@ -682,13 +687,4 @@ public class NettyAcceptor implements Acceptor {
cancelled = true;
}
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClientSessionFactoryImpl.class.getClassLoader();
}
});
}
}

View File

@ -188,21 +188,20 @@ public class RemotingServiceImpl implements RemotingService, ConnectionLifeCycle
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
// If NIO these are capped at nio-remoting-threads which defaults to num cores * 3
// This needs to be a different thread pool to the main thread pool especially for OIO where we may need
// to support many hundreds of connections, but the main thread pool must be kept small for better performance
ThreadFactory tFactory = new ActiveMQThreadFactory("ActiveMQ-remoting-threads-" + server.toString() +
ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-remoting-threads-" + server.toString() +
"-" +
System.identityHashCode(this), false, tccl);
System.identityHashCode(this), false, Thread.currentThread().getContextClassLoader());
}
});
threadPool = Executors.newCachedThreadPool(tFactory);

View File

@ -1405,7 +1405,12 @@ public class ActiveMQServerImpl implements ActiveMQServer {
* Executor based on the provided Thread pool. Otherwise we create a new ThreadPool.
*/
if (serviceRegistry.getExecutorService() == null) {
ThreadFactory tFactory = new ActiveMQThreadFactory("ActiveMQ-server-" + this.toString(), false, getThisClassLoader());
ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-server-" + this.toString(), false, ClientSessionFactoryImpl.class.getClassLoader());
}
});
if (configuration.getThreadPoolMaxSize() == -1) {
threadPool = Executors.newCachedThreadPool(tFactory);
}
@ -1423,7 +1428,12 @@ public class ActiveMQServerImpl implements ActiveMQServer {
* Scheduled ExecutorService otherwise we create a new one.
*/
if (serviceRegistry.getScheduledExecutorService() == null) {
ThreadFactory tFactory = new ActiveMQThreadFactory("ActiveMQ-scheduled-threads", false, getThisClassLoader());
ThreadFactory tFactory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() {
@Override
public ThreadFactory run() {
return new ActiveMQThreadFactory("ActiveMQ-scheduled-threads", false, ClientSessionFactoryImpl.class.getClassLoader());
}
});
scheduledPool = new ScheduledThreadPoolExecutor(configuration.getScheduledThreadPoolMaxSize(), tFactory);
}
else {
@ -1784,15 +1794,6 @@ public class ActiveMQServerImpl implements ActiveMQServer {
}
}
private static ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClientSessionFactoryImpl.class.getClassLoader();
}
});
}
/**
* Check if journal directory exists or create it (if configured to do so)
*/