Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/smartcardio/CardTerminals.java
38827 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.smartcardio;2627import java.util.*;2829/**30* The set of terminals supported by a TerminalFactory.31* This class allows applications to enumerate the available CardTerminals,32* obtain a specific CardTerminal, or wait for the insertion or removal of33* cards.34*35* <p>This class is multi-threading safe and can be used by multiple36* threads concurrently. However, this object keeps track of the card37* presence state of each of its terminals. Multiple objects should be used38* if independent calls to {@linkplain #waitForChange} are required.39*40* <p>Applications can obtain instances of this class by calling41* {@linkplain TerminalFactory#terminals}.42*43* @see TerminalFactory44* @see CardTerminal45*46* @since 1.647* @author Andreas Sterbenz48* @author JSR 268 Expert Group49*/50public abstract class CardTerminals {5152/**53* Constructs a new CardTerminals object.54*55* <p>This constructor is called by subclasses only. Application should56* call {@linkplain TerminalFactory#terminals}57* to obtain a CardTerminals object.58*/59protected CardTerminals() {60// empty61}6263/**64* Returns an unmodifiable list of all available terminals.65*66* @return an unmodifiable list of all available terminals.67*68* @throws CardException if the card operation failed69*/70public List<CardTerminal> list() throws CardException {71return list(State.ALL);72}7374/**75* Returns an unmodifiable list of all terminals matching the specified76* state.77*78* <p>If state is {@link State#ALL State.ALL}, this method returns79* all CardTerminals encapsulated by this object.80* If state is {@link State#CARD_PRESENT State.CARD_PRESENT} or81* {@link State#CARD_ABSENT State.CARD_ABSENT}, it returns all82* CardTerminals where a card is currently present or absent, respectively.83*84* <p>If state is {@link State#CARD_INSERTION State.CARD_INSERTION} or85* {@link State#CARD_REMOVAL State.CARD_REMOVAL}, it returns all86* CardTerminals for which an insertion (or removal, respectively)87* was detected during the last call to {@linkplain #waitForChange}.88* If <code>waitForChange()</code> has not been called on this object,89* <code>CARD_INSERTION</code> is equivalent to <code>CARD_PRESENT</code>90* and <code>CARD_REMOVAL</code> is equivalent to <code>CARD_ABSENT</code>.91* For an example of the use of <code>CARD_INSERTION</code>,92* see {@link #waitForChange}.93*94* @param state the State95* @return an unmodifiable list of all terminals matching the specified96* attribute.97*98* @throws NullPointerException if attr is null99* @throws CardException if the card operation failed100*/101public abstract List<CardTerminal> list(State state) throws CardException;102103/**104* Returns the terminal with the specified name or null if no such105* terminal exists.106*107* @return the terminal with the specified name or null if no such108* terminal exists.109*110* @throws NullPointerException if name is null111*/112public CardTerminal getTerminal(String name) {113if (name == null) {114throw new NullPointerException();115}116try {117for (CardTerminal terminal : list()) {118if (terminal.getName().equals(name)) {119return terminal;120}121}122return null;123} catch (CardException e) {124return null;125}126}127128/**129* Waits for card insertion or removal in any of the terminals of this130* object.131*132* <p>This call is equivalent to calling133* {@linkplain #waitForChange(long) waitForChange(0)}.134*135* @throws IllegalStateException if this <code>CardTerminals</code>136* object does not contain any terminals137* @throws CardException if the card operation failed138*/139public void waitForChange() throws CardException {140waitForChange(0);141}142143/**144* Waits for card insertion or removal in any of the terminals of this145* object or until the timeout expires.146*147* <p>This method examines each CardTerminal of this object.148* If a card was inserted into or removed from a CardTerminal since the149* previous call to <code>waitForChange()</code>, it returns150* immediately.151* Otherwise, or if this is the first call to <code>waitForChange()</code>152* on this object, it blocks until a card is inserted into or removed from153* a CardTerminal.154*155* <p>If <code>timeout</code> is greater than 0, the method returns after156* <code>timeout</code> milliseconds even if there is no change in state.157* In that case, this method returns <code>false</code>; otherwise it158* returns <code>true</code>.159*160* <p>This method is often used in a loop in combination with161* {@link #list(CardTerminals.State) list(State.CARD_INSERTION)},162* for example:163* <pre>164* TerminalFactory factory = ...;165* CardTerminals terminals = factory.terminals();166* while (true) {167* for (CardTerminal terminal : terminals.list(CARD_INSERTION)) {168* // examine Card in terminal, return if it matches169* }170* terminals.waitForChange();171* }</pre>172*173* @param timeout if positive, block for up to <code>timeout</code>174* milliseconds; if zero, block indefinitely; must not be negative175* @return false if the method returns due to an expired timeout,176* true otherwise.177*178* @throws IllegalStateException if this <code>CardTerminals</code>179* object does not contain any terminals180* @throws IllegalArgumentException if timeout is negative181* @throws CardException if the card operation failed182*/183public abstract boolean waitForChange(long timeout) throws CardException;184185/**186* Enumeration of attributes of a CardTerminal.187* It is used as a parameter to the {@linkplain CardTerminals#list} method.188*189* @since 1.6190*/191public static enum State {192/**193* All CardTerminals.194*/195ALL,196/**197* CardTerminals in which a card is present.198*/199CARD_PRESENT,200/**201* CardTerminals in which a card is not present.202*/203CARD_ABSENT,204/**205* CardTerminals for which a card insertion was detected during the206* latest call to {@linkplain State#waitForChange waitForChange()}207* call.208*/209CARD_INSERTION,210/**211* CardTerminals for which a card removal was detected during the212* latest call to {@linkplain State#waitForChange waitForChange()}213* call.214*/215CARD_REMOVAL,216}217218}219220221