Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/CardTerminals.java
41153 views
/*1* Copyright (c) 2006, 2015, 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* state.97*98* @throws NullPointerException if state 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* @param name the terminal name108* @return the terminal with the specified name or null if no such109* terminal exists.110*111* @throws NullPointerException if name is null112*/113public CardTerminal getTerminal(String name) {114if (name == null) {115throw new NullPointerException();116}117try {118for (CardTerminal terminal : list()) {119if (terminal.getName().equals(name)) {120return terminal;121}122}123return null;124} catch (CardException e) {125return null;126}127}128129/**130* Waits for card insertion or removal in any of the terminals of this131* object.132*133* <p>This call is equivalent to calling134* {@linkplain #waitForChange(long) waitForChange(0)}.135*136* @throws IllegalStateException if this <code>CardTerminals</code>137* object does not contain any terminals138* @throws CardException if the card operation failed139*/140public void waitForChange() throws CardException {141waitForChange(0);142}143144/**145* Waits for card insertion or removal in any of the terminals of this146* object or until the timeout expires.147*148* <p>This method examines each CardTerminal of this object.149* If a card was inserted into or removed from a CardTerminal since the150* previous call to <code>waitForChange()</code>, it returns151* immediately.152* Otherwise, or if this is the first call to <code>waitForChange()</code>153* on this object, it blocks until a card is inserted into or removed from154* a CardTerminal.155*156* <p>If <code>timeout</code> is greater than 0, the method returns after157* <code>timeout</code> milliseconds even if there is no change in state.158* In that case, this method returns <code>false</code>; otherwise it159* returns <code>true</code>.160*161* <p>This method is often used in a loop in combination with162* {@link #list(CardTerminals.State) list(State.CARD_INSERTION)},163* for example:164* <pre>165* TerminalFactory factory = ...;166* CardTerminals terminals = factory.terminals();167* while (true) {168* for (CardTerminal terminal : terminals.list(CARD_INSERTION)) {169* // examine Card in terminal, return if it matches170* }171* terminals.waitForChange();172* }</pre>173*174* @param timeout if positive, block for up to <code>timeout</code>175* milliseconds; if zero, block indefinitely; must not be negative176* @return false if the method returns due to an expired timeout,177* true otherwise.178*179* @throws IllegalStateException if this <code>CardTerminals</code>180* object does not contain any terminals181* @throws IllegalArgumentException if timeout is negative182* @throws CardException if the card operation failed183*/184public abstract boolean waitForChange(long timeout) throws CardException;185186/**187* Enumeration of attributes of a CardTerminal.188* It is used as a parameter to the {@linkplain CardTerminals#list} method.189*190* @since 1.6191*/192public static enum State {193/**194* All CardTerminals.195*/196ALL,197/**198* CardTerminals in which a card is present.199*/200CARD_PRESENT,201/**202* CardTerminals in which a card is not present.203*/204CARD_ABSENT,205/**206* CardTerminals for which a card insertion was detected during the207* latest call to {@linkplain State#waitForChange waitForChange()}208* call.209*/210CARD_INSERTION,211/**212* CardTerminals for which a card removal was detected during the213* latest call to {@linkplain State#waitForChange waitForChange()}214* call.215*/216CARD_REMOVAL,217}218219}220221222