Handling the Callbacks from a Login Module

This example implements a class that handles a few types of callbacks from a login module.
// This login-module callback handler handles a few types of callbacks
public class MyCallbackHandler implements CallbackHandler {
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (int i=0; i<callbacks.length; i++) {
            if (callbacks[i] instanceof TextOutputCallback) {
                // This callback delivers messages from a login module
                TextOutputCallback toCb = (TextOutputCallback)callbacks[i];

                // Get message
                String msg = toCb.getMessage();

                // Get message type
                switch (toCb.getMessageType()) {
                  case TextOutputCallback.INFORMATION:
                    break;
                  case TextOutputCallback.ERROR:
                    break;
                  case TextOutputCallback.WARNING:
                    break;
                }

                // Display message to the user ...
            } else if (callbacks[i] instanceof NameCallback) {
                // A login module is requesting the name of the user
                NameCallback nCb = (NameCallback)callbacks[i];

                // Get the prompt to display to the user
                String prompt = nCb.getPrompt();

                // Get the name ...

                // Set the name
                nCb.setName("username");
            } else if (callbacks[i] instanceof PasswordCallback) {
                // A login module is requesting the user's password
                PasswordCallback pCb = (PasswordCallback)callbacks[i];

                // Get the prompt to display to the user
                String prompt = pCb.getPrompt();

                // Get the password ...

                // Set the password
                pCb.setPassword("password".toCharArray());
            } else if (callbacks[i] instanceof LanguageCallback) {
                // A login module is requesting the user's locale
                LanguageCallback lCb = (LanguageCallback)callbacks[i];

                // Get the locale ...

                // Set the locale
                lCb.setLocale(Locale.CHINESE);
            } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
            }
        }
    }
}
Here's an example that uses the callback handler:
try {
    // Create login context
    LoginContext lc = new LoginContext("LoginAppName",
        new MyCallbackHandler());
} catch (LoginException e) {
    // Login failed
}