public boolean commit() throws LoginException {
if (externalUser == null || syncHandler == null) {
return false;
}
SyncContext context = null;
try {
Root root = getRoot();
UserManager userManager = getUserManager();
if (root == null || userManager == null) {
throw new LoginException("Cannot synchronize user.");
}
context = syncHandler.createContext(idp, userManager, root);
context.sync(externalUser);
root.commit();
Set<? extends Principal> principals = getPrincipals(externalUser.getId());
if (!principals.isEmpty()) {
if (!subject.isReadOnly()) {
subject.getPrincipals().addAll(principals);
subject.getPublicCredentials().add(credentials);
setAuthInfo(new AuthInfoImpl(externalUser.getId(), null, principals), subject);
} else {
log.debug("Could not add information to read only subject {}", subject);
}
return true;
}
return false;
} catch (SyncException e) {
throw new LoginException("User synchronization failed: " + e);
} catch (CommitFailedException e) {
throw new LoginException("User synchronization failed: " + e);
} finally {
if (context != null) {
context.close();
}
}
}