use of org.apache.harmony.javax.security.auth.Subject in project AsmackService by rtreffer.
the class LoginContext method loginImpl.
/**
* The real implementation of login() method whose calls are wrapped into
* appropriate doPrivileged calls in login().
*/
private void loginImpl() throws LoginException {
if (subject == null) {
subject = new Subject();
}
if (sharedState == null) {
sharedState = new HashMap<String, Object>();
}
// PHASE 1: Calling login()-s
Throwable firstProblem = null;
int[] logged = new int[4];
int[] total = new int[4];
for (Module module : modules) {
try {
// if a module fails during Class.forName(), then it breaks overall
// attempt - see catch() below
module.create(subject, callbackHandler, sharedState);
if (module.module.login()) {
++total[module.getFlag()];
++logged[module.getFlag()];
if (module.getFlag() == SUFFICIENT) {
break;
}
}
} catch (Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
if (module.klass == null) {
/*
* an exception occurred during class lookup - overall
* attempt must fail a little trick: increase the REQUIRED's
* number - this will look like a failed REQUIRED module
* later, so overall attempt will fail
*/
++total[REQUIRED];
break;
}
++total[module.getFlag()];
// something happened after the class was loaded
if (module.getFlag() == REQUISITE) {
// ... and no need to walk down anymore
break;
}
}
}
// end of PHASE1,
// Let's decide whether we have either overall success or a total failure
boolean fail = true;
// if any REQ* module failed - then it's failure
if (logged[REQUIRED] != total[REQUIRED] || logged[REQUISITE] != total[REQUISITE]) {
// fail = true;
} else {
if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
// must have at least one SUFFICIENT or OPTIONAL
if (logged[OPTIONAL] != 0 || logged[SUFFICIENT] != 0) {
fail = false;
}
//else { fail = true; }
} else {
fail = false;
}
}
int[] commited = new int[4];
// clear it
total[0] = total[1] = total[2] = total[3] = 0;
if (!fail) {
for (Module module : modules) {
if (module.klass != null) {
++total[module.getFlag()];
try {
module.module.commit();
++commited[module.getFlag()];
} catch (Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
}
}
}
}
// need to decide once again
fail = true;
if (commited[REQUIRED] != total[REQUIRED] || commited[REQUISITE] != total[REQUISITE]) {
//fail = true;
} else {
if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
/*
* neither REQUIRED nor REQUISITE was configured. must have at
* least one SUFFICIENT or OPTIONAL
*/
if (commited[OPTIONAL] != 0 || commited[SUFFICIENT] != 0) {
fail = false;
} else {
//fail = true;
}
} else {
fail = false;
}
}
if (fail) {
for (Module module : modules) {
try {
module.module.abort();
} catch (/*LoginException*/
Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
}
}
if (firstProblem instanceof PrivilegedActionException && firstProblem.getCause() != null) {
firstProblem = firstProblem.getCause();
}
if (firstProblem instanceof LoginException) {
throw (LoginException) firstProblem;
}
//$NON-NLS-1$
throw (LoginException) new LoginException("auth.37").initCause(firstProblem);
}
loggedIn = true;
}
Aggregations