jdk/src/share/classes/javax/smartcardio/CardTerminals.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *   attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws NullPointerException if attr is null
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return the terminal with the specified name or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * terminal exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws NullPointerException if name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public CardTerminal getTerminal(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            for (CardTerminal terminal : list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (terminal.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    return terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (CardException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return null;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Waits for card insertion or removal in any of the terminals of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p>This call is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@linkplain #waitForChange(long) waitForChange(0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws IllegalStateException if this <code>CardTerminals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *   object does not contain any terminals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void waitForChange() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        waitForChange(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Waits for card insertion or removal in any of the terminals of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * object or until the timeout expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>This method examines each CardTerminal of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * If a card was inserted into or removed from a CardTerminal since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * previous call to <code>waitForChange()</code>, it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Otherwise, or if this is the first call to <code>waitForChange()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * on this object, it blocks until a card is inserted into or removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * a CardTerminal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>If <code>timeout</code> is greater than 0, the method returns after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>timeout</code> milliseconds even if there is no change in state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * In that case, this method returns <code>false</code>; otherwise it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>This method is often used in a loop in combination with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * {@link #list(CardTerminals.State) list(State.CARD_INSERTION)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  TerminalFactory factory = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  CardTerminals terminals = factory.terminals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *      for (CardTerminal terminal : terminals.list(CARD_INSERTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *          // examine Card in terminal, return if it matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *      terminals.waitForChange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *  }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param timeout if positive, block for up to <code>timeout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *   milliseconds; if zero, block indefinitely; must not be negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return false if the method returns due to an expired timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *   true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws IllegalStateException if this <code>CardTerminals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *   object does not contain any terminals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws IllegalArgumentException if timeout is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public abstract boolean waitForChange(long timeout) throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Enumeration of attributes of a CardTerminal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * It is used as a parameter to the {@linkplain CardTerminals#list} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static enum State {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * All CardTerminals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * CardTerminals in which a card is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        CARD_PRESENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * CardTerminals in which a card is not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        CARD_ABSENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * CardTerminals for which a card insertion was detected during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * latest call to {@linkplain State#waitForChange waitForChange()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        CARD_INSERTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * CardTerminals for which a card removal was detected during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * latest call to {@linkplain State#waitForChange waitForChange()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        CARD_REMOVAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}