Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/smartcardio/TerminalFactory.java
38827 views
/*1* Copyright (c) 2005, 2011, 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.*;3334import sun.security.action.GetPropertyAction;3536/**37* A factory for CardTerminal objects.38*39* It allows an application to40* <ul>41* <li>obtain a TerminalFactory by calling42* one of the static factory methods in this class43* ({@linkplain #getDefault} or {@linkplain #getInstance getInstance()}).44* <li>use this TerminalFactory object to access the CardTerminals by45* calling the {@linkplain #terminals} method.46* </ul>47*48* <p>Each TerminalFactory has a <code>type</code> indicating how it49* was implemented. It must be specified when the implementation is obtained50* using a {@linkplain #getInstance getInstance()} method and can be retrieved51* via the {@linkplain #getType} method.52*53* <P>The following standard type names have been defined:54* <dl>55* <dt><code>PC/SC</code>56* <dd>an implementation that calls into the PC/SC Smart Card stack57* of the host platform.58* Implementations do not require parameters and accept "null" as argument59* in the getInstance() calls.60* <dt><code>None</code>61* <dd>an implementation that does not supply any CardTerminals. On platforms62* that do not support other implementations,63* {@linkplain #getDefaultType} returns <code>None</code> and64* {@linkplain #getDefault} returns an instance of a <code>None</code>65* TerminalFactory. Factories of this type cannot be obtained by calling the66* <code>getInstance()</code> methods.67* </dl>68* Additional standard types may be defined in the future.69*70* <p><strong>Note:</strong>71* Provider implementations that accept initialization parameters via the72* <code>getInstance()</code> methods are strongly73* encouraged to use a {@linkplain java.util.Properties} object as the74* representation for String name-value pair based parameters whenever75* possible. This allows applications to more easily interoperate with76* multiple providers than if each provider used different provider77* specific class as parameters.78*79* <P>TerminalFactory utilizes an extensible service provider framework.80* Service providers that wish to add a new implementation should see the81* {@linkplain TerminalFactorySpi} class for more information.82*83* @see CardTerminals84* @see Provider85*86* @since 1.687* @author Andreas Sterbenz88* @author JSR 268 Expert Group89*/90public final class TerminalFactory {9192private final static String PROP_NAME =93"javax.smartcardio.TerminalFactory.DefaultType";9495private final static String defaultType;9697private final static TerminalFactory defaultFactory;9899static {100// lookup up the user specified type, default to PC/SC101String type = AccessController.doPrivileged102(new GetPropertyAction(PROP_NAME, "PC/SC")).trim();103TerminalFactory factory = null;104try {105factory = TerminalFactory.getInstance(type, null);106} catch (Exception e) {107// ignore108}109if (factory == null) {110// if that did not work, try the Sun PC/SC factory111try {112type = "PC/SC";113Provider sun = Security.getProvider("SunPCSC");114if (sun == null) {115Class<?> clazz = Class.forName("sun.security.smartcardio.SunPCSC");116sun = (Provider)clazz.newInstance();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.0d, "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* <code>javax.smartcardio.TerminalFactory.DefaultType</code>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* @param type the type of the requested TerminalFactory234* @param params the parameters to pass to the TerminalFactorySpi235* implementation, or null if no parameters are needed236* @return a TerminalFactory of the specified type237*238* @throws NullPointerException if type is null239* @throws NoSuchAlgorithmException if no Provider supports a240* TerminalFactorySpi of the specified type241*/242public static TerminalFactory getInstance(String type, Object params)243throws NoSuchAlgorithmException {244Instance instance = GetInstance.getInstance("TerminalFactory",245TerminalFactorySpi.class, type, params);246return new TerminalFactory((TerminalFactorySpi)instance.impl,247instance.provider, type);248}249250/**251* Returns a TerminalFactory of the specified type that is initialized252* with the specified parameters.253*254* <p> A new TerminalFactory object encapsulating the255* TerminalFactorySpi implementation from the specified provider256* is returned. The specified provider must be registered257* in the security provider list.258*259* <p> Note that the list of registered providers may be retrieved via260* the {@linkplain Security#getProviders() Security.getProviders()} method.261*262* <p>The <code>TerminalFactory</code> is initialized with the263* specified parameters Object. The type of parameters264* needed may vary between different types of <code>TerminalFactory</code>s.265*266* @param type the type of the requested TerminalFactory267* @param params the parameters to pass to the TerminalFactorySpi268* implementation, or null if no parameters are needed269* @param provider the name of the provider270* @return a TerminalFactory of the specified type271*272* @throws NullPointerException if type is null273* @throws IllegalArgumentException if provider is null or the empty String274* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation275* of the specified type is not available from the specified provider276* @throws NoSuchAlgorithmException if no TerminalFactory of the277* specified type could be found278* @throws NoSuchProviderException if the specified provider could not279* be found280*/281public static TerminalFactory getInstance(String type, Object params,282String provider) throws NoSuchAlgorithmException, NoSuchProviderException {283Instance instance = GetInstance.getInstance("TerminalFactory",284TerminalFactorySpi.class, type, params, provider);285return new TerminalFactory((TerminalFactorySpi)instance.impl,286instance.provider, type);287}288289/**290* Returns a TerminalFactory of the specified type that is initialized291* with the specified parameters.292*293* <p> A new TerminalFactory object encapsulating the294* TerminalFactorySpi implementation from the specified provider object295* is returned. Note that the specified provider object does not have to be296* registered in the provider list.297*298* <p>The <code>TerminalFactory</code> is initialized with the299* specified parameters Object. The type of parameters300* needed may vary between different types of <code>TerminalFactory</code>s.301*302* @param type the type of the requested TerminalFactory303* @param params the parameters to pass to the TerminalFactorySpi304* implementation, or null if no parameters are needed305* @param provider the provider306* @return a TerminalFactory of the specified type307*308* @throws NullPointerException if type is null309* @throws IllegalArgumentException if provider is null310* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation311* of the specified type is not available from the specified Provider312*/313public static TerminalFactory getInstance(String type, Object params,314Provider provider) throws NoSuchAlgorithmException {315Instance instance = GetInstance.getInstance("TerminalFactory",316TerminalFactorySpi.class, type, params, provider);317return new TerminalFactory((TerminalFactorySpi)instance.impl,318instance.provider, type);319}320321/**322* Returns the provider of this TerminalFactory.323*324* @return the provider of this TerminalFactory.325*/326public Provider getProvider() {327return provider;328}329330/**331* Returns the type of this TerminalFactory. This is the value that was332* specified in the getInstance() method that returned this object.333*334* @return the type of this TerminalFactory335*/336public String getType() {337return type;338}339340/**341* Returns a new CardTerminals object encapsulating the terminals342* supported by this factory.343* See the class comment of the {@linkplain CardTerminals} class344* regarding how the returned objects can be shared and reused.345*346* @return a new CardTerminals object encapsulating the terminals347* supported by this factory.348*/349public CardTerminals terminals() {350return spi.engineTerminals();351}352353/**354* Returns a string representation of this TerminalFactory.355*356* @return a string representation of this TerminalFactory.357*/358public String toString() {359return "TerminalFactory for type " + type + " from provider "360+ provider.getName();361}362363}364365366