jdk/src/java.smartcardio/share/classes/javax/smartcardio/CardTerminals.java
author mullan
Fri, 08 May 2015 09:05:15 -0400
changeset 30437 2359af6accfe
parent 29928 6ad7fd5f4746
permissions -rw-r--r--
6470634: Typos in CardTerminals.list(CardTerminals.State) javadoc Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29928
6ad7fd5f4746 8077915: Fix doclint issues in javax.smartcardio
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 2006, 2015, 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.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The set of terminals supported by a TerminalFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class allows applications to enumerate the available CardTerminals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * obtain a specific CardTerminal, or wait for the insertion or removal of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * cards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>This class is multi-threading safe and can be used by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * threads concurrently. However, this object keeps track of the card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * presence state of each of its terminals. Multiple objects should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * if independent calls to {@linkplain #waitForChange} are required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>Applications can obtain instances of this class by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@linkplain TerminalFactory#terminals}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see TerminalFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see CardTerminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public abstract class CardTerminals {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Constructs a new CardTerminals object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * <p>This constructor is called by subclasses only. Application should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * call {@linkplain TerminalFactory#terminals}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * to obtain a CardTerminals object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected CardTerminals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Returns an unmodifiable list of all available terminals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return an unmodifiable list of all available terminals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public List<CardTerminal> list() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         return list(State.ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Returns an unmodifiable list of all terminals matching the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p>If state is {@link State#ALL State.ALL}, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * all CardTerminals encapsulated by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If state is {@link State#CARD_PRESENT State.CARD_PRESENT} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * {@link State#CARD_ABSENT State.CARD_ABSENT}, it returns all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * CardTerminals where a card is currently present or absent, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <p>If state is {@link State#CARD_INSERTION State.CARD_INSERTION} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * {@link State#CARD_REMOVAL State.CARD_REMOVAL}, it returns all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * CardTerminals for which an insertion (or removal, respectively)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * was detected during the last call to {@linkplain #waitForChange}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * If <code>waitForChange()</code> has not been called on this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <code>CARD_INSERTION</code> is equivalent to <code>CARD_PRESENT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * and <code>CARD_REMOVAL</code> is equivalent to <code>CARD_ABSENT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * For an example of the use of <code>CARD_INSERTION</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * see {@link #waitForChange}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param state the State
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return an unmodifiable list of all terminals matching the specified
30437
2359af6accfe 6470634: Typos in CardTerminals.list(CardTerminals.State) javadoc
mullan
parents: 29928
diff changeset
    97
     *   state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
30437
2359af6accfe 6470634: Typos in CardTerminals.list(CardTerminals.State) javadoc
mullan
parents: 29928
diff changeset
    99
     * @throws NullPointerException if state is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public abstract List<CardTerminal> list(State state) throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the terminal with the specified name or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * terminal exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
29928
6ad7fd5f4746 8077915: Fix doclint issues in javax.smartcardio
darcy
parents: 25859
diff changeset
   108
     * @param name the terminal name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return the terminal with the specified name or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * terminal exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws NullPointerException if name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public CardTerminal getTerminal(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            for (CardTerminal terminal : list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (terminal.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    return terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (CardException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Waits for card insertion or removal in any of the terminals of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>This call is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * {@linkplain #waitForChange(long) waitForChange(0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws IllegalStateException if this <code>CardTerminals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *   object does not contain any terminals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void waitForChange() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        waitForChange(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Waits for card insertion or removal in any of the terminals of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * object or until the timeout expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>This method examines each CardTerminal of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * If a card was inserted into or removed from a CardTerminal since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * previous call to <code>waitForChange()</code>, it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Otherwise, or if this is the first call to <code>waitForChange()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * on this object, it blocks until a card is inserted into or removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * a CardTerminal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p>If <code>timeout</code> is greater than 0, the method returns after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>timeout</code> milliseconds even if there is no change in state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * In that case, this method returns <code>false</code>; otherwise it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <p>This method is often used in a loop in combination with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * {@link #list(CardTerminals.State) list(State.CARD_INSERTION)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  TerminalFactory factory = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  CardTerminals terminals = factory.terminals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *  while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *      for (CardTerminal terminal : terminals.list(CARD_INSERTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          // examine Card in terminal, return if it matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *      terminals.waitForChange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *  }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param timeout if positive, block for up to <code>timeout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *   milliseconds; if zero, block indefinitely; must not be negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return false if the method returns due to an expired timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *   true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws IllegalStateException if this <code>CardTerminals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *   object does not contain any terminals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws IllegalArgumentException if timeout is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public abstract boolean waitForChange(long timeout) throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Enumeration of attributes of a CardTerminal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * It is used as a parameter to the {@linkplain CardTerminals#list} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static enum State {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * All CardTerminals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * CardTerminals in which a card is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        CARD_PRESENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * CardTerminals in which a card is not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        CARD_ABSENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * CardTerminals for which a card insertion was detected during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * latest call to {@linkplain State#waitForChange waitForChange()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        CARD_INSERTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * CardTerminals for which a card removal was detected during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * latest call to {@linkplain State#waitForChange waitForChange()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        CARD_REMOVAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}