This method to be invoked in the Thread's constructor. The first argument
(thread) must be Thread's this and the second must be a snapshot of the
current AccessControlContext:
Thread() {
SecurityUtils.putContext(this,AccessController.getContext());
...do the stuff you need...
}
The method throws SecurityException if the method is called more than
once for a given thread. The first call to putContext
is
always performed in the Thread's constructor so this effectively means
that no one can replace the snapshot taken.
if (thread == null) {
throw new NullPointerException(Messages.getString("security.140")); //$NON-NLS-1$
}
synchronized (ACC_CACHE) {
if (ACC_CACHE.containsKey(thread)) {
throw new SecurityException(Messages.getString("security.141")); //$NON-NLS-1$
}
if (context == null) {
// this only allowed once - for the very first thread.
if (ACC_CACHE.containsValue(null)) {
throw new Error(Messages.getString("security.142")); //$NON-NLS-1$
}
}
ACC_CACHE.put(thread, context);
}