Skip to content

Commit

Permalink
Merge pull request hornetq#1963 from istudens/HORNETQ-1381
Browse files Browse the repository at this point in the history
HORNETQ-1381 create new threads under AccessControlContext of the creato...
  • Loading branch information
clebertsuconic committed Dec 5, 2014
2 parents ee631ec + 7ba3c90 commit 5deea9c
Showing 1 changed file with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.hornetq.utils;

import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory;
Expand All @@ -36,6 +37,8 @@ public final class HornetQThreadFactory implements ThreadFactory

private final ClassLoader tccl;

private final AccessControlContext acc;

public HornetQThreadFactory(final String groupName, final boolean daemon, final ClassLoader tccl)
{
group = new ThreadGroup(groupName + "-" + System.identityHashCode(this));
Expand All @@ -45,24 +48,45 @@ public HornetQThreadFactory(final String groupName, final boolean daemon, final
this.tccl = tccl;

this.daemon = daemon;

this.acc = (System.getSecurityManager() == null) ? null : AccessController.getContext();
}

public Thread newThread(final Runnable command)
{
// always create a thread in a privileged block.
return AccessController.doPrivileged(new PrivilegedAction<Thread>()
// always create a thread in a privileged block if running with Security Manager
if (acc != null && System.getSecurityManager() != null)
{
@Override
public Thread run()
{
final Thread t = new Thread(group, command, "Thread-" + threadCount.getAndIncrement() + " (" + group.getName() + ")");
t.setDaemon(daemon);
t.setPriority(threadPriority);
t.setContextClassLoader(tccl);

return t;
}
});
return AccessController.doPrivileged(new ThreadCreateAction(command), acc);
}
else
{
return createThread(command);
}
}

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;
}
}

0 comments on commit 5deea9c

Please sign in to comment.