IdRepoSampleUtils.java revision 386650a8957e131311273ee84a4a4113ad5e3fe2
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: IdRepoSampleUtils.java,v 1.5 2008/08/07 22:08:20 goodearth Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.samples.clientsdk.idrepo;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport java.io.*;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport java.util.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.lang.Integer;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport java.security.Principal;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport javax.security.auth.callback.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport com.iplanet.sso.SSOTokenManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.AuthContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.spi.AuthLoginException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.share.AuthXMLTags;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.util.ISAuthConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.idm.AMIdentity;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.idm.AMIdentityRepository;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.idm.IdRepoException;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpottsimport com.sun.identity.idm.IdType;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @author
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class IdRepoSampleUtils {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext lc = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String userID = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public IdRepoSampleUtils() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public SSOToken realmLogin (String userid, String password, String realm)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SSOException, AuthLoginException, Exception
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOTokenManager mgr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String adminDN;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts String adminPassword;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOToken ssoToken = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.IndexType authType = AuthContext.IndexType.MODULE_INSTANCE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts lc = new AuthContext(realm);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (AuthLoginException le) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "IdRepoSampleUtils: could not get AuthContext for realm " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster realm);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw le;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lc.login();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (AuthLoginException le) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println("IdRepoSampleUtils: Failed to start login " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "for default authmodule");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw le;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts userID = userid;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Callback[] callbacks = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Hashtable values = new Hashtable();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster values.put(AuthXMLTags.NAME_CALLBACK, userid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster values.put(AuthXMLTags.PASSWORD_CALLBACK, password);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (lc.hasMoreRequirements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster callbacks = lc.getRequirements();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster fillCallbacks(callbacks, values);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lc.submitRequirements(callbacks);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println( "Failed to submit callbacks!");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster e.printStackTrace();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.Status istat = lc.getStatus();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (istat == AuthContext.Status.SUCCESS) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println("==>Authentication SUCCESSFUL for user " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster userid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (istat == AuthContext.Status.COMPLETED) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println("==>Authentication Status for user " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster userid+ " = " + istat);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ssoToken = lc.getSSOToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println( "Failed to get SSO token! " + e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw e;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ssoToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void logout () throws AuthLoginException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lc.logout();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (AuthLoginException alexc) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println ("IdRepoSampleUtils: logout failed for user '" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster userID + "'");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw alexc;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void fillCallbacks(Callback[] callbacks, Hashtable values)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws Exception
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < callbacks.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (callbacks[i] instanceof NameCallback) {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts NameCallback nc = (NameCallback) callbacks[i];
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts nc.setName((String)values.get(AuthXMLTags.NAME_CALLBACK));
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts } else if (callbacks[i] instanceof PasswordCallback) {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts PasswordCallback pc = (PasswordCallback) callbacks[i];
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts pc.setPassword(((String)values.get(
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts AuthXMLTags.PASSWORD_CALLBACK)).toCharArray());
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts } else if (callbacks[i] instanceof TextInputCallback) {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts TextInputCallback tic = (TextInputCallback) callbacks[i];
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts tic.setText((String)values.get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthXMLTags.TEXT_INPUT_CALLBACK));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (callbacks[i] instanceof ChoiceCallback) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ChoiceCallback cc = (ChoiceCallback) callbacks[i];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cc.setSelectedIndex(Integer.parseInt((String)values.get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthXMLTags.CHOICE_CALLBACK)));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getLine() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer buf = new StringBuffer(80);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int c;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((c = System.in.read()) != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster char ch = (char)c;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ch == '\r') {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster continue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ch == '\n') {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts break;
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts }
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts buf.append(ch);
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts }
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts } catch (IOException e) {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts System.err.println ("getLine: " + e.getMessage());
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts }
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts return (buf.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getLine (String prompt) {
73db2ddb960cafd7ffb6daf89eb697910d36c56dJames Phillpotts System.out.print (prompt);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (getLine());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getLine (String prompt, String defaultVal) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.print (prompt + " [" + defaultVal + "]: ");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tmp = getLine();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tmp.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tmp = defaultVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (tmp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * return integer value of String sVal; -1 if error
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public int getIntValue (String sVal) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int i = -1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster i = Integer.parseInt (sVal);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (NumberFormatException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.err.println ("'" + sVal +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "' does not appear to be an integer.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return i;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * can only create or delete AMIdentities of IdType user, agentgroup,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * agentonly
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public IdType getIdTypeToCreateOrDelete()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster IdType tType = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println (" Supported IdTypes:\n" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "\t0: user\n\t1: agent\n\t2: agentonly\n\t3: agentgroup\n\t4: realm\n\t5: No selection");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String answer = getLine ("Select type: [0..3]: ");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int i = getIntValue(answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster switch (i) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case 0: // user
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tType = IdType.USER;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case 1: // agent
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tType = IdType.AGENT;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case 2: // agentonly
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tType = IdType.AGENTONLY;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case 3: // agentgroup
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tType = IdType.AGENTGROUP;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
case 4: // realm
tType = IdType.REALM;
break;
case 5: // no selection
break;
default: // invalid selection
System.err.println(answer + " is an invalid selection.");
}
return tType;
}
/*
* get the IdType selected from the list of supported IdTypes for
* this AMIdentityRepository object. can be "null" if no selection
* made.
*/
public IdType getIdType(AMIdentityRepository idRepo) {
IdType tType = null;
String realmName = null;
try {
realmName = idRepo.getRealmIdentity().getRealm();
Set types = idRepo.getSupportedIdTypes();
Object[] idtypes = types.toArray();
System.out.println(" Supported IdTypes:");
int i = 0;
for (i = 0; i < idtypes.length; i++) {
tType = (IdType)idtypes[i];
System.out.println("\t" + i + ": " + tType.getName());
}
System.out.println ("\t" + i + ": No selection");
String answer = getLine ("Select type: [0.." +
idtypes.length + "]: ");
i = getIntValue(answer);
tType = (IdType)idtypes[0];
if (i == idtypes.length) {
return (null);
} else if ((i >= 0) && (i < idtypes.length)) {
tType = (IdType)idtypes[i];
} else {
System.err.println(answer + " is an invalid selection.");
return (null);
}
} catch (IdRepoException ire) {
System.err.println("getIdType: IdRepoException" +
" getting Supported IdTypes for '" + realmName + "': " +
ire.getMessage());
} catch (SSOException ssoe) {
System.err.println("getIdType: SSOException" +
" getting Supported IdTypes for '" + realmName + "': " +
ssoe.getMessage());
}
return (tType);
}
/*
* print out elements in the Set "results". header and trailer
* titling Strings. more generic (i.e., usually expecting Strings)
* than other printResults(String, Set).
*/
public void printResults (
String header,
Set results,
String trailer)
{
if (results.isEmpty()) {
System.out.println(header + " has no " + trailer);
} else {
System.out.println (header + " has " + results.size() + " " +
trailer + ":");
for (Iterator it = results.iterator(); it.hasNext(); ) {
System.out.println (" " + it.next());
}
}
System.out.println("");
return;
}
/*
* print out elements in the Set "results". header and trailer
* titling Strings. more generic (i.e., usually expecting Strings)
* than other printResults(String, Set).
*/
public void printResultsRealm (
String header,
Set results,
String trailer)
{
if (results.isEmpty()) {
System.out.println(header + " has no " + trailer);
} else {
System.out.println (header + " has " + results.size() + " " +
trailer + ":");
for (Iterator it = results.iterator(); it.hasNext(); ) {
AMIdentity amid = (AMIdentity)it.next();
System.out.println (" " + amid.getRealm());
}
}
System.out.println("");
return;
}
/*
* for the Set of IdTypes specified in "results", get and print
* 1. the IdTypes it can be a member of
* 2. the IdTypes it can have as members
* 3. the IdTypes it can add to itself
*/
public void printIdTypeResults(
String header,
Set results,
String trailer)
{
if (results.isEmpty()) {
System.out.println(header + " has no " + trailer);
} else {
System.out.println(header + " has " + results.size() + " " +
trailer + ":");
IdType itype = null;
Set idSet = null;
for (Iterator it = results.iterator(); it.hasNext(); ) {
itype = (IdType)it.next();
System.out.println (" IdType " + itype.getName());
idSet = itype.canBeMemberOf();
printIdTypeSet("BE a member of IdType(s):", idSet);
idSet = itype.canHaveMembers();
printIdTypeSet("HAVE a member of IdType(s):", idSet);
idSet = itype.canAddMembers();
printIdTypeSet("ADD members of IdType(s):", idSet);
}
}
System.out.println("");
return;
}
/*
* used by printIdTypeResults(), above, to print out
* AMIdentity names of elements in the Set.
*/
private void printIdTypeSet (
String header,
Set idSet)
{
System.out.print ("\tcan " + header);
if (idSet.size() > 0) {
for (Iterator it = idSet.iterator(); it.hasNext(); ) {
System.out.print (" " + ((IdType)it.next()).getName());
}
System.out.print("\n");
} else {
System.out.println (" [NONE]");
}
}
/*
* print the objects (String or AMIdentity.getName()) in the
* specified Array, and return the index of the one selected.
* -1 if none selected.
*/
public int selectFromArray (
Object[] objs,
String hdr,
String prompt)
{
AMIdentity amid = null;
String ans = null;
boolean isIdType = false;
boolean isString = false;
if (objs.length <= 0) {
return (-1);
}
System.out.println(hdr);
int i = -1;
String objclass = objs[0].getClass().getName();
if (objclass.indexOf("AMIdentity") >= 0) {
isIdType = true;
} else if (objclass.indexOf("String") >= 0) {
isString = true;
}
for (i = 0; i < objs.length; i++) {
if (isIdType) {
amid = (AMIdentity)objs[i];
System.out.println("\t" + i + ": " + amid.getName());
} else if (isString) {
System.out.println("\t" + i + ": " + (String)objs[i]);
} else {
System.out.println("\t" + i + ": Class = " + objclass);
}
}
System.out.println("\t" + i + ": No Selection");
ans = getLine (prompt + ": [0.." + objs.length + "]: ");
i = getIntValue(ans);
return i;
}
/*
* print the objects (String or AMIdentity.getName()) in the
* specified Set, and return the object of the one selected.
* null if none selected.
*/
public Object selectFromSet (Set itemSet)
{
Object[] objs = itemSet.toArray();
AMIdentity amid = null;
AMIdentity amid2 = null;
int setsize = itemSet.size();
int i;
boolean isAMId = false;
boolean isString = false;
String str = null;
if (setsize <= 0) {
return null;
}
String objclass = objs[0].getClass().getName();
if (objclass.indexOf("AMIdentity") >= 0) {
isAMId = true;
} else if (objclass.indexOf("String") >= 0) {
isString = true;
}
if (setsize > 0) {
System.out.println("Available selections:");
for (i = 0; i < setsize; i++) {
if (isAMId) {
amid = (AMIdentity)objs[i];
System.out.println("\t" + i + ": " + amid.getName());
} else if (isString) {
System.out.println("\t" + i + ": " + (String)objs[i]);
} else {
System.out.println("\t" + i + ": Class = " + objclass);
}
}
System.out.println ("\t" + i + ": No selection");
String answer = getLine("Select identity: [0.." + setsize + "]: ");
int ians = getIntValue(answer);
if ((ians >= 0) && (ians < setsize)) {
return (objs[ians]);
} else if (ians == setsize) {
} else {
System.err.println ("'" + answer +
"' is invalid.");
}
}
return null;
}
public void waitForReturn() {
waitForReturn("Hit <return> when ready: ");
String answer = getLine();
}
public void waitForReturn(String prompt) {
System.out.print (prompt);
String answer = getLine();
}
}