package extjtech;

import java.io.*;
import com.sssw.rt.util.*;

// The LoginHandler class shows how you can code a
// SilverStream login handler for use in external Java 
// clients. You'll supply an instance of this class in the
// AgRuntime.init() method when preparing to connect your 
// client to a SilverStream server. This enables the server
// to call back to that LoginHandler instance when it 
// requires user authentication. 
//
// LoginHandler implements the SilverStream interface
// AgiUserLogin, which has one method: prompt(). You must
// code that method to prompt the user for name and password,
// then return those values to the server (in an instance of
// the SilverStream class AgoUserLoginInfo). 
//
// This example prompts the user by writing prompt text to 
// standard output and reading data entry from standard
// input. Alternatively, you could display a dialog (as 
// shown in class LoginHandlerDlg) or read command-line args
// to obtain the name and password.
public class LoginHandler implements AgiUserLogin {

  public AgoUserLoginInfo prompt(String realm,
                                 String username,
                                 boolean forceDialog) {
    BufferedReader in = null;
    String user = null;
    String password = null;

    try {
      // Prepare to read data entered from standard input.
      in = new BufferedReader(new InputStreamReader(System.in));

      // Ask the user to log in to this security realm.
      System.out.println("Please log in for " + realm);

      // Obtain the username.
      System.out.print("username: ");
      System.out.flush();
      user = in.readLine();

      // If a username was entered, obtain the password.
      // Note: A limitation of this approach is that the
      // password displays on the user's screen.
      if (user != null && user.length() != 0) {
        System.out.print("password: ");
        System.out.flush();
        password = in.readLine();
      }
    }
    catch (IOException ex) {
      System.out.println("I/O error during login prompt");
      ex.printStackTrace();
    }

    // Return the username and password to the server for
    // authentication.
    return new AgoUserLoginInfo(user, password);
  }
}
