callbacks[1] = new PasswordCallback("Password: ", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException ioe) {
throw new LoginException(ioe.getMessage());
} catch (UnsupportedCallbackException uce) {
throw new LoginException(uce.getMessage() + " not available to obtain information from user");
}
String user = ((NameCallback) callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
if (tmpPassword == null) {
tmpPassword = new char[0];
}
String password = new String(tmpPassword);
principals = new HashSet<Principal>();
try {
connection = dataSource.getConnection();
//Retrieve user credentials from database.
passwordStatement = connection.prepareStatement(passwordQuery);
passwordStatement.setString(1, user);
passwordResultSet = passwordStatement.executeQuery();
if (!passwordResultSet.next()) {
if (!this.detailedLoginExcepion) {
throw new LoginException("login failed");
} else {
throw new LoginException("Password for " + user + " does not match");
}
} else {
String storedPassword = passwordResultSet.getString(1);
if (!checkPassword(password, storedPassword)) {
if (!this.detailedLoginExcepion) {
throw new LoginException("login failed");
} else {
throw new LoginException("Password for " + user + " does not match");
}
}
principals.add(new UserPrincipal(user));
}
//Retrieve user roles from database
roleStatement = connection.prepareStatement(roleQuery);
roleStatement.setString(1, user);
roleResultSet = roleStatement.executeQuery();
while (roleResultSet.next()) {
String role = roleResultSet.getString(1);
principals.add(new RolePrincipal(role));
}
} catch (Exception ex) {
throw new LoginException("Error has occured while retrieving credentials from database:" + ex.getMessage());
} finally {
try {
if (passwordResultSet != null) {
passwordResultSet.close();
}