jdk/src/share/classes/javax/security/auth/callback/ChoiceCallback.java
author juh
Tue, 16 Jul 2013 12:19:41 -0700
changeset 18830 90956ead732f
parent 5506 202f599c92aa
permissions -rw-r--r--
8020557: javadoc cleanup in javax.security Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth.callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <p> Underlying security services instantiate and pass a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    30
 * {@code ChoiceCallback} to the {@code handle}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    31
 * method of a {@code CallbackHandler} to display a list of choices
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * and to retrieve the selected choice(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see javax.security.auth.callback.CallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ChoiceCallback implements Callback, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final long serialVersionUID = -3975664071579892167L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @serial the list of choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private String[] choices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @serial the choice to be used as the default choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int defaultChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @serial whether multiple selections are allowed from the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean multipleSelectionsAllowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @serial the selected choices, represented as indexes into the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    63
     *          {@code choices} list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int[] selections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    69
     * Construct a {@code ChoiceCallback} with a prompt,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * a list of choices, a default choice, and a boolean specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * whether or not multiple selections from the list of choices are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param prompt the prompt used to describe the list of choices. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param choices the list of choices. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param defaultChoice the choice to be used as the default choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *                  when the list of choices are displayed.  This value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *                  is represented as an index into the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    82
     *                  {@code choices} array. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param multipleSelectionsAllowed boolean specifying whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *                  not multiple selections can be made from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *                  list of choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    88
     * @exception IllegalArgumentException if {@code prompt} is null,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    89
     *                  if {@code prompt} has a length of 0,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    90
     *                  if {@code choices} is null,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    91
     *                  if {@code choices} has a length of 0,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    92
     *                  if any element from {@code choices} is null,
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    93
     *                  if any element from {@code choices}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    94
     *                  has a length of 0 or if {@code defaultChoice}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *                  does not fall within the array boundaries of
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    96
     *                  {@code choices}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public ChoiceCallback(String prompt, String[] choices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                int defaultChoice, boolean multipleSelectionsAllowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (prompt == null || prompt.length() == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            choices == null || choices.length == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            defaultChoice < 0 || defaultChoice >= choices.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (choices[i] == null || choices[i].length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.prompt = prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.choices = choices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        this.defaultChoice = defaultChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.multipleSelectionsAllowed = multipleSelectionsAllowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Get the prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public String getPrompt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return prompt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Get the list of choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return the list of choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public String[] getChoices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return choices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Get the defaultChoice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the defaultChoice, represented as an index into
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   145
     *          the {@code choices} list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public int getDefaultChoice() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return defaultChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Get the boolean determining whether multiple selections from
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   153
     * the {@code choices} list are allowed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return whether multiple selections are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean allowMultipleSelections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return multipleSelectionsAllowed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Set the selected choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param selection the selection represented as an index into the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   169
     *          {@code choices} list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see #getSelectedIndexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void setSelectedIndex(int selection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.selections = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.selections[0] = selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Set the selected choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param selections the selections represented as indexes into the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   184
     *          {@code choices} list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception UnsupportedOperationException if multiple selections are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *          not allowed, as determined by
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   188
     *          {@code allowMultipleSelections}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #getSelectedIndexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void setSelectedIndexes(int[] selections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!multipleSelectionsAllowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.selections = selections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Get the selected choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the selected choices, represented as indexes into the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   204
     *          {@code choices} list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @see #setSelectedIndexes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public int[] getSelectedIndexes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return selections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}