HORNETQ-1381 create new threads under AccessControlContext of the creator of ActiveMQThreadFactory

This commit is contained in:
Ivo Studensky 2014-12-05 12:23:48 +01:00
parent b61747de09
commit ff9052b83b
1 changed files with 37 additions and 12 deletions

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.activemq.utils; package org.apache.activemq.utils;
import java.security.AccessControlContext;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
@ -40,6 +41,8 @@ public final class ActiveMQThreadFactory implements ThreadFactory
private final ClassLoader tccl; private final ClassLoader tccl;
private final AccessControlContext acc;
public ActiveMQThreadFactory(final String groupName, final boolean daemon, final ClassLoader tccl) public ActiveMQThreadFactory(final String groupName, final boolean daemon, final ClassLoader tccl)
{ {
group = new ThreadGroup(groupName + "-" + System.identityHashCode(this)); group = new ThreadGroup(groupName + "-" + System.identityHashCode(this));
@ -49,24 +52,46 @@ public final class ActiveMQThreadFactory implements ThreadFactory
this.tccl = tccl; this.tccl = tccl;
this.daemon = daemon; this.daemon = daemon;
this.acc = (System.getSecurityManager() == null) ? null : AccessController.getContext();
} }
public Thread newThread(final Runnable command) public Thread newThread(final Runnable command)
{ {
// always create a thread in a privileged block. // create a thread in a privileged block if running with Security Manager
return AccessController.doPrivileged(new PrivilegedAction<Thread>() if (acc != null && System.getSecurityManager() != null)
{ {
@Override return AccessController.doPrivileged(new ThreadCreateAction(command), acc);
public Thread run() }
{ else
final Thread t = new Thread(group, command, "Thread-" + threadCount.getAndIncrement() + " (" + group.getName() + ")"); {
t.setDaemon(daemon); return createThread(command);
t.setPriority(threadPriority); }
t.setContextClassLoader(tccl); }
return t; private final class ThreadCreateAction implements PrivilegedAction<Thread>
} {
}); private final Runnable target;
private ThreadCreateAction(final Runnable target)
{
this.target = target;
}
public Thread run()
{
return createThread(target);
}
}
private Thread createThread(final Runnable command)
{
final Thread t = new Thread(group, command, "Thread-" + threadCount.getAndIncrement() + " (" + group.getName() + ")");
t.setDaemon(daemon);
t.setPriority(threadPriority);
t.setContextClassLoader(tccl);
return t;
} }
} }