0N/A/*
2362N/A * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.security.auth.callback;
0N/A
0N/A/**
0N/A * <p> Underlying security services instantiate and pass a
0N/A * <code>ChoiceCallback</code> to the <code>handle</code>
0N/A * method of a <code>CallbackHandler</code> to display a list of choices
0N/A * and to retrieve the selected choice(s).
0N/A *
0N/A * @see javax.security.auth.callback.CallbackHandler
0N/A */
0N/Apublic class ChoiceCallback implements Callback, java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = -3975664071579892167L;
0N/A
0N/A /**
0N/A * @serial
0N/A * @since 1.4
0N/A */
0N/A private String prompt;
0N/A /**
0N/A * @serial the list of choices
0N/A * @since 1.4
0N/A */
0N/A private String[] choices;
0N/A /**
0N/A * @serial the choice to be used as the default choice
0N/A * @since 1.4
0N/A */
0N/A private int defaultChoice;
0N/A /**
0N/A * @serial whether multiple selections are allowed from the list of
0N/A * choices
0N/A * @since 1.4
0N/A */
0N/A private boolean multipleSelectionsAllowed;
0N/A /**
0N/A * @serial the selected choices, represented as indexes into the
0N/A * <code>choices</code> list.
0N/A * @since 1.4
0N/A */
0N/A private int[] selections;
0N/A
0N/A /**
0N/A * Construct a <code>ChoiceCallback</code> with a prompt,
0N/A * a list of choices, a default choice, and a boolean specifying
0N/A * whether or not multiple selections from the list of choices are allowed.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param prompt the prompt used to describe the list of choices. <p>
0N/A *
0N/A * @param choices the list of choices. <p>
0N/A *
0N/A * @param defaultChoice the choice to be used as the default choice
0N/A * when the list of choices are displayed. This value
0N/A * is represented as an index into the
0N/A * <code>choices</code> array. <p>
0N/A *
0N/A * @param multipleSelectionsAllowed boolean specifying whether or
0N/A * not multiple selections can be made from the
0N/A * list of choices.
0N/A *
0N/A * @exception IllegalArgumentException if <code>prompt</code> is null,
0N/A * if <code>prompt</code> has a length of 0,
0N/A * if <code>choices</code> is null,
0N/A * if <code>choices</code> has a length of 0,
0N/A * if any element from <code>choices</code> is null,
0N/A * if any element from <code>choices</code>
0N/A * has a length of 0 or if <code>defaultChoice</code>
0N/A * does not fall within the array boundaries of
0N/A * <code>choices</code>.
0N/A */
0N/A public ChoiceCallback(String prompt, String[] choices,
0N/A int defaultChoice, boolean multipleSelectionsAllowed) {
0N/A
0N/A if (prompt == null || prompt.length() == 0 ||
0N/A choices == null || choices.length == 0 ||
0N/A defaultChoice < 0 || defaultChoice >= choices.length)
0N/A throw new IllegalArgumentException();
0N/A
0N/A for (int i = 0; i < choices.length; i++) {
0N/A if (choices[i] == null || choices[i].length() == 0)
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A this.prompt = prompt;
0N/A this.choices = choices;
0N/A this.defaultChoice = defaultChoice;
0N/A this.multipleSelectionsAllowed = multipleSelectionsAllowed;
0N/A }
0N/A
0N/A /**
0N/A * Get the prompt.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the prompt.
0N/A */
0N/A public String getPrompt() {
0N/A return prompt;
0N/A }
0N/A
0N/A /**
0N/A * Get the list of choices.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the list of choices.
0N/A */
0N/A public String[] getChoices() {
0N/A return choices;
0N/A }
0N/A
0N/A /**
0N/A * Get the defaultChoice.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the defaultChoice, represented as an index into
0N/A * the <code>choices</code> list.
0N/A */
0N/A public int getDefaultChoice() {
0N/A return defaultChoice;
0N/A }
0N/A
0N/A /**
0N/A * Get the boolean determining whether multiple selections from
0N/A * the <code>choices</code> list are allowed.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return whether multiple selections are allowed.
0N/A */
0N/A public boolean allowMultipleSelections() {
0N/A return multipleSelectionsAllowed;
0N/A }
0N/A
0N/A /**
0N/A * Set the selected choice.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param selection the selection represented as an index into the
0N/A * <code>choices</code> list.
0N/A *
0N/A * @see #getSelectedIndexes
0N/A */
0N/A public void setSelectedIndex(int selection) {
0N/A this.selections = new int[1];
0N/A this.selections[0] = selection;
0N/A }
0N/A
0N/A /**
0N/A * Set the selected choices.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param selections the selections represented as indexes into the
0N/A * <code>choices</code> list.
0N/A *
0N/A * @exception UnsupportedOperationException if multiple selections are
0N/A * not allowed, as determined by
0N/A * <code>allowMultipleSelections</code>.
0N/A *
0N/A * @see #getSelectedIndexes
0N/A */
0N/A public void setSelectedIndexes(int[] selections) {
0N/A if (!multipleSelectionsAllowed)
0N/A throw new UnsupportedOperationException();
0N/A this.selections = selections;
0N/A }
0N/A
0N/A /**
0N/A * Get the selected choices.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the selected choices, represented as indexes into the
0N/A * <code>choices</code> list.
0N/A *
0N/A * @see #setSelectedIndexes
0N/A */
0N/A public int[] getSelectedIndexes() {
0N/A return selections;
0N/A }
0N/A}