Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/TerminalFactory.java
41153 views
/*1* Copyright (c) 2005, 2021, 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.*;2829import java.security.*;3031import sun.security.jca.*;32import sun.security.jca.GetInstance.*;3334/**35* A factory for CardTerminal objects.36*37* It allows an application to38* <ul>39* <li>obtain a TerminalFactory by calling40* one of the static factory methods in this class41* ({@linkplain #getDefault} or {@linkplain #getInstance getInstance()}).42* <li>use this TerminalFactory object to access the CardTerminals by43* calling the {@linkplain #terminals} method.44* </ul>45*46* <p>Each TerminalFactory has a <code>type</code> indicating how it47* was implemented. It must be specified when the implementation is obtained48* using a {@linkplain #getInstance getInstance()} method and can be retrieved49* via the {@linkplain #getType} method.50*51* <P>The following standard type names have been defined:52* <dl>53* <dt><code>PC/SC</code>54* <dd>an implementation that calls into the PC/SC Smart Card stack55* of the host platform.56* Implementations do not require parameters and accept "null" as argument57* in the getInstance() calls.58* <dt><code>None</code>59* <dd>an implementation that does not supply any CardTerminals. On platforms60* that do not support other implementations,61* {@linkplain #getDefaultType} returns <code>None</code> and62* {@linkplain #getDefault} returns an instance of a <code>None</code>63* TerminalFactory. Factories of this type cannot be obtained by calling the64* <code>getInstance()</code> methods.65* </dl>66* Additional standard types may be defined in the future.67*68* <p><strong>Note:</strong>69* Provider implementations that accept initialization parameters via the70* <code>getInstance()</code> methods are strongly71* encouraged to use a {@linkplain java.util.Properties} object as the72* representation for String name-value pair based parameters whenever73* possible. This allows applications to more easily interoperate with74* multiple providers than if each provider used different provider75* specific class as parameters.76*77* <P>TerminalFactory utilizes an extensible service provider framework.78* Service providers that wish to add a new implementation should see the79* {@linkplain TerminalFactorySpi} class for more information.80*81* @see CardTerminals82* @see Provider83*84* @since 1.685* @author Andreas Sterbenz86* @author JSR 268 Expert Group87*/88public final class TerminalFactory {8990private final static String PROP_NAME =91"javax.smartcardio.TerminalFactory.DefaultType";9293private final static String defaultType;9495private final static TerminalFactory defaultFactory;9697static {98// lookup up the user specified type, default to PC/SC99@SuppressWarnings("removal")100String type = AccessController.doPrivileged(101(PrivilegedAction<String>) () -> System.getProperty(PROP_NAME, "PC/SC")).trim();102TerminalFactory factory = null;103try {104factory = TerminalFactory.getInstance(type, null);105} catch (Exception e) {106// ignore107}108if (factory == null) {109// if that did not work, try the Sun PC/SC factory110try {111type = "PC/SC";112Provider sun = Security.getProvider("SunPCSC");113if (sun == null) {114@SuppressWarnings("deprecation")115Object o = Class.forName("sun.security.smartcardio.SunPCSC").newInstance();116sun = (Provider)o;117}118factory = TerminalFactory.getInstance(type, null, sun);119} catch (Exception e) {120// ignore121}122}123if (factory == null) {124type = "None";125factory = new TerminalFactory126(NoneFactorySpi.INSTANCE, NoneProvider.INSTANCE, "None");127}128defaultType = type;129defaultFactory = factory;130}131132private static final class NoneProvider extends Provider {133134private static final long serialVersionUID = 2745808869881593918L;135final static Provider INSTANCE = new NoneProvider();136private NoneProvider() {137super("None", "1.0", "none");138}139}140141private static final class NoneFactorySpi extends TerminalFactorySpi {142final static TerminalFactorySpi INSTANCE = new NoneFactorySpi();143private NoneFactorySpi() {144// empty145}146protected CardTerminals engineTerminals() {147return NoneCardTerminals.INSTANCE;148}149}150151private static final class NoneCardTerminals extends CardTerminals {152final static CardTerminals INSTANCE = new NoneCardTerminals();153private NoneCardTerminals() {154// empty155}156public List<CardTerminal> list(State state) throws CardException {157if (state == null) {158throw new NullPointerException();159}160return Collections.emptyList();161}162public boolean waitForChange(long timeout) throws CardException {163throw new IllegalStateException("no terminals");164}165}166167private final TerminalFactorySpi spi;168169private final Provider provider;170171private final String type;172173private TerminalFactory(TerminalFactorySpi spi, Provider provider, String type) {174this.spi = spi;175this.provider = provider;176this.type = type;177}178179/**180* Get the default TerminalFactory type.181*182* <p>It is determined as follows:183*184* when this class is initialized, the system property185* {@systemProperty javax.smartcardio.TerminalFactory.DefaultType}186* is examined. If it is set, a TerminalFactory of this type is187* instantiated by calling the {@linkplain #getInstance188* getInstance(String,Object)} method passing189* <code>null</code> as the value for <code>params</code>. If the call190* succeeds, the type becomes the default type and the factory becomes191* the {@linkplain #getDefault default} factory.192*193* <p>If the system property is not set or the getInstance() call fails194* for any reason, the system defaults to an implementation specific195* default type and TerminalFactory.196*197* @return the default TerminalFactory type198*/199public static String getDefaultType() {200return defaultType;201}202203/**204* Returns the default TerminalFactory instance. See205* {@linkplain #getDefaultType} for more information.206*207* <p>A default TerminalFactory is always available. However, depending208* on the implementation, it may not offer any terminals.209*210* @return the default TerminalFactory211*/212public static TerminalFactory getDefault() {213return defaultFactory;214}215216/**217* Returns a TerminalFactory of the specified type that is initialized218* with the specified parameters.219*220* <p> This method traverses the list of registered security Providers,221* starting with the most preferred Provider.222* A new TerminalFactory object encapsulating the223* TerminalFactorySpi implementation from the first224* Provider that supports the specified type is returned.225*226* <p> Note that the list of registered providers may be retrieved via227* the {@linkplain Security#getProviders() Security.getProviders()} method.228*229* <p>The <code>TerminalFactory</code> is initialized with the230* specified parameters Object. The type of parameters231* needed may vary between different types of <code>TerminalFactory</code>s.232*233* @implNote234* The JDK Reference Implementation additionally uses the235* {@code jdk.security.provider.preferred}236* {@link Security#getProperty(String) Security} property to determine237* the preferred provider order for the specified algorithm. This238* may be different than the order of providers returned by239* {@link Security#getProviders() Security.getProviders()}.240*241* @param type the type of the requested TerminalFactory242* @param params the parameters to pass to the TerminalFactorySpi243* implementation, or null if no parameters are needed244* @return a TerminalFactory of the specified type245*246* @throws NullPointerException if type is null247* @throws NoSuchAlgorithmException if no Provider supports a248* TerminalFactorySpi of the specified type249*/250public static TerminalFactory getInstance(String type, Object params)251throws NoSuchAlgorithmException {252Instance instance = GetInstance.getInstance("TerminalFactory",253TerminalFactorySpi.class, type, params);254return new TerminalFactory((TerminalFactorySpi)instance.impl,255instance.provider, type);256}257258/**259* Returns a TerminalFactory of the specified type that is initialized260* with the specified parameters.261*262* <p> A new TerminalFactory object encapsulating the263* TerminalFactorySpi implementation from the specified provider264* is returned. The specified provider must be registered265* in the security provider list.266*267* <p> Note that the list of registered providers may be retrieved via268* the {@linkplain Security#getProviders() Security.getProviders()} method.269*270* <p>The <code>TerminalFactory</code> is initialized with the271* specified parameters Object. The type of parameters272* needed may vary between different types of <code>TerminalFactory</code>s.273*274* @param type the type of the requested TerminalFactory275* @param params the parameters to pass to the TerminalFactorySpi276* implementation, or null if no parameters are needed277* @param provider the name of the provider278* @return a TerminalFactory of the specified type279*280* @throws NullPointerException if type is null281* @throws IllegalArgumentException if provider is null or the empty String282* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation283* of the specified type is not available from the specified provider284* @throws NoSuchAlgorithmException if no TerminalFactory of the285* specified type could be found286* @throws NoSuchProviderException if the specified provider could not287* be found288*/289public static TerminalFactory getInstance(String type, Object params,290String provider) throws NoSuchAlgorithmException, NoSuchProviderException {291Instance instance = GetInstance.getInstance("TerminalFactory",292TerminalFactorySpi.class, type, params, provider);293return new TerminalFactory((TerminalFactorySpi)instance.impl,294instance.provider, type);295}296297/**298* Returns a TerminalFactory of the specified type that is initialized299* with the specified parameters.300*301* <p> A new TerminalFactory object encapsulating the302* TerminalFactorySpi implementation from the specified provider object303* is returned. Note that the specified provider object does not have to be304* registered in the provider list.305*306* <p>The <code>TerminalFactory</code> is initialized with the307* specified parameters Object. The type of parameters308* needed may vary between different types of <code>TerminalFactory</code>s.309*310* @param type the type of the requested TerminalFactory311* @param params the parameters to pass to the TerminalFactorySpi312* implementation, or null if no parameters are needed313* @param provider the provider314* @return a TerminalFactory of the specified type315*316* @throws NullPointerException if type is null317* @throws IllegalArgumentException if provider is null318* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation319* of the specified type is not available from the specified Provider320*/321public static TerminalFactory getInstance(String type, Object params,322Provider provider) throws NoSuchAlgorithmException {323Instance instance = GetInstance.getInstance("TerminalFactory",324TerminalFactorySpi.class, type, params, provider);325return new TerminalFactory((TerminalFactorySpi)instance.impl,326instance.provider, type);327}328329/**330* Returns the provider of this TerminalFactory.331*332* @return the provider of this TerminalFactory.333*/334public Provider getProvider() {335return provider;336}337338/**339* Returns the type of this TerminalFactory. This is the value that was340* specified in the getInstance() method that returned this object.341*342* @return the type of this TerminalFactory343*/344public String getType() {345return type;346}347348/**349* Returns a new CardTerminals object encapsulating the terminals350* supported by this factory.351* See the class comment of the {@linkplain CardTerminals} class352* regarding how the returned objects can be shared and reused.353*354* @return a new CardTerminals object encapsulating the terminals355* supported by this factory.356*/357public CardTerminals terminals() {358return spi.engineTerminals();359}360361/**362* Returns a string representation of this TerminalFactory.363*364* @return a string representation of this TerminalFactory.365*/366public String toString() {367return "TerminalFactory for type " + type + " from provider "368+ provider.getName();369}370371}372373374