*
* Thread() {
*
* The method throws SecurityException if the method is called more than
* once for a given thread. The first call to
* SecurityUtils.putContext(this,AccessController.getContext());
* ...do the stuff you need...
* }
* putContext
is
* always performed in the Thread's constructor so this effectively means
* that no one can replace the snapshot taken.
*
* @throws SecurityException if a context for the passed
* thread
already exists in the map.
* @throws NullPointerException if thread is null
* @throws Error if context is null AND if null context is already stored
* in the map
*/
public static void putContext(Thread thread, AccessControlContext context)
throws SecurityException {
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);
}
}
/**
* Returns the AccessControlContext stored for a given thread.
* The method may return null - for the very first thread created
* by the VM which does not have inherited context.
* It may also return null if no Thread found in the map - that seems
* possible during VM startup process.
*/
public static AccessControlContext getContext(Thread thread)
throws SecurityException {
// ~fixme: see 'fixme' at the top of the file
/*
Class cl = VMStack.getCallerClass(0);
if (cl != AccessController.class) {
throw new SecurityException("You ["+cl+"] do not have access to this resource.");
}
*/
synchronized (ACC_CACHE) {
return ACC_CACHE.get(thread);
}
}
}