FileDocCategorySizeDatePackage
AuthContext.javaAPI DocGlassfish v2 API7623Fri May 04 22:35:42 BST 2007com.sun.enterprise.security.jauth

AuthContext

public final class AuthContext extends Object
Shared logic from Client and ServerAuthContext reside here.

Fields Summary
static final String
INIT
static final String
DISPOSE_SUBJECT
static final String
SECURE_REQUEST
static final String
VALIDATE_RESPONSE
static final String
VALIDATE_REQUEST
static final String
SECURE_RESPONSE
static final String
MANAGES_SESSIONS
static final String
MANAGES_SESSIONS_OPTION
private ConfigFile.Entry[]
entries
private Debug
debug
Constructors Summary
AuthContext(ConfigFile.Entry[] entries, Debug debug)


     
		    

	this.entries = entries;
	this.debug = debug;
    
Methods Summary
java.lang.Object[]invoke(java.lang.String methodName, java.lang.Object[] args)
Invoke modules according to configuration


	// invoke modules in a doPrivileged
	final Object rValues[] = new Object[entries.length];

	try {
	    java.security.AccessController.doPrivileged
		(new java.security.PrivilegedExceptionAction() {
		public Object run() throws AuthException {
		    invokePriv(methodName, args, rValues);
		    return null;
		}
	    });
	} catch (java.security.PrivilegedActionException pae) {
	    if (pae.getException() instanceof AuthException) {
		throw (AuthException)pae.getException();
	    } else {
		AuthException ae = new AuthException();
		ae.initCause(pae.getException());
		throw ae;
	    }
	}
	return rValues;
    
voidinvokePriv(java.lang.String methodName, java.lang.Object[] args, java.lang.Object[] rValues)


	// special treatment for managesSessions until the module
	// interface can be extended.
	if (methodName.equals(AuthContext.MANAGES_SESSIONS)) {
	    for (int i = 0; i < entries.length; i++) {
		Map options = entries[i].getOptions();
		String mS = (String) options.get(AuthContext.MANAGES_SESSIONS_OPTION); 
		rValues[i] = Boolean.valueOf(mS);
	    }
	    return;
	}

	boolean success = false;
	AuthException firstRequiredError = null;
	AuthException firstError = null;

	// XXX no way to reverse module invocation

	for (int i = 0; i < entries.length; i++) {

	    // get initialized module instance
	
	    Object module = entries[i].module;

	    // invoke the module

	    try {
		Method[] mArray = module.getClass().getMethods();
		for (int j = 0; j < mArray.length; j++) {
		    if (mArray[j].getName().equals(methodName)) {

			// invoke module
			rValues[i] = mArray[j].invoke(module, args);

			// success -
			// return if SUFFICIENT and no previous REQUIRED errors
			
			if (firstRequiredError == null &&
			    entries[i].getControlFlag() ==
		      AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT) {

			    if (debug != null) {
				debug.println(entries[i].getLoginModuleName() +
					"." +
					methodName +
					" SUFFICIENT success");
			    }

			    return;
			}

			if (debug != null) {
			    debug.println(entries[i].getLoginModuleName() +
					"." +
					methodName +
					" success");
			}

			success = true;
			break;
		    }
		}

		if (!success) {
		    // PLEASE NOTE:
		    // this exception will be thrown if any module 
		    // in the context does not support the method.
		    NoSuchMethodException nsme = 
			new NoSuchMethodException("module " +
				module.getClass().getName() +
				" does not implement " +
				methodName);
		    AuthException ae = new AuthException();
		    ae.initCause(nsme);
		    throw ae;
		}
	    } catch (IllegalAccessException iae) {
		AuthException ae = new AuthException();
		ae.initCause(iae);
		throw ae;
	    } catch (InvocationTargetException ite) {

		// failure cases

		AuthException ae;

		if (ite.getCause() instanceof AuthException) {
		    ae = (AuthException)ite.getCause();
		} else {
		    ae = new AuthException();
		    ae.initCause(ite.getCause());
		}

		if (entries[i].getControlFlag() ==
		    AppConfigurationEntry.LoginModuleControlFlag.REQUISITE) {

		    if (debug != null) {
			debug.println(entries[i].getLoginModuleName() +
					"." +
					methodName +
					" REQUISITE failure");
		    }

		    // immediately throw exception

		    if (firstRequiredError != null) {
			throw firstRequiredError;
		    } else {
			throw ae;
		    }

		} else if (entries[i].getControlFlag() ==
			AppConfigurationEntry.LoginModuleControlFlag.REQUIRED) {

		    if (debug != null) {
			debug.println(entries[i].getLoginModuleName() +
					"." +
					methodName +
					" REQUIRED failure");
		    }

		    // save exception and continue

		    if (firstRequiredError == null) {
			firstRequiredError = ae;
		    }

		} else {

		    if (debug != null) {
			debug.println(entries[i].getLoginModuleName() +
					"." +
					methodName +
					" OPTIONAL failure");
		    }

		    // save exception and continue

		    if (firstError == null) {
			firstError = ae;
		    }
		}
	    }
	}

	// done invoking entire stack of modules

	if (firstRequiredError != null) {
	    throw firstRequiredError;
	} else if (firstError != null && !success) {
	    throw firstError;
	}

	// if no errors, return gracefully
	if (debug != null) {
	    debug.println("overall " + methodName + " success");
	}