FileDocCategorySizeDatePackage
SecurityUtils.javaAPI DocAndroid 1.5 API4481Wed May 06 22:41:06 BST 2009org.apache.harmony.security.fortress

SecurityUtils

public final class SecurityUtils extends Object
The class is used to perform an exchange of information between java.lang.Thread and java.security.AccessController.
The data to exchange is inherited contexts for the Thread-s.

Fields Summary
private static final WeakHashMap
ACC_CACHE
Constructors Summary
Methods Summary
public static java.security.AccessControlContextgetContext(java.lang.Thread thread)
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.


        // ~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);
        }
    
public static voidputContext(java.lang.Thread thread, java.security.AccessControlContext context)
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.

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


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