A Little Help from the Callback

Listing 2. By creating an array of callbacks and passing them to the callback handler's handle method, we can gather the necessary data. Then the login method does the login by calling a helper.

public boolean login() throws LoginException
{

  TextOutputCallback tcb = new 
    TextOutputCallback(TextOutputCallback.INFORMATION,
    "Enter details for JDBC login");
  NameCallback ncb = new NameCallback("Name: ");
  PasswordCallback pcb = new PasswordCallback(
    "Password", false);
  ConfirmationCallback ccb = new 
    ConfirmationCallback(
      ConfirmationCallback.INFORMATION,
      ConfirmationCallback.OK_CANCEL_OPTION,
      ConfirmationCallback.OK);

  Callback[] handlers = new Callback[] { tcb, ncb, 
    pcb, ccb, };

  try
  {
    callbackHandler.handle(handlers);

    if (ccb.getSelectedIndex() == 
      ConfirmationCallback.OK)
    {
      loginSucceeded = doLocalLogin(ncb.getName(), 
                       new String(pcb.getPassword()));
    }
  }
  catch (Exception e)
  {
    throw new FailedLoginException(e.getMessage());
  }
  return loginSucceeded;
}