Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/TerminalFactory.java
41153 views
1
/*
2
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.smartcardio;
27
28
import java.util.*;
29
30
import java.security.*;
31
32
import sun.security.jca.*;
33
import sun.security.jca.GetInstance.*;
34
35
/**
36
* A factory for CardTerminal objects.
37
*
38
* It allows an application to
39
* <ul>
40
* <li>obtain a TerminalFactory by calling
41
* one of the static factory methods in this class
42
* ({@linkplain #getDefault} or {@linkplain #getInstance getInstance()}).
43
* <li>use this TerminalFactory object to access the CardTerminals by
44
* calling the {@linkplain #terminals} method.
45
* </ul>
46
*
47
* <p>Each TerminalFactory has a <code>type</code> indicating how it
48
* was implemented. It must be specified when the implementation is obtained
49
* using a {@linkplain #getInstance getInstance()} method and can be retrieved
50
* via the {@linkplain #getType} method.
51
*
52
* <P>The following standard type names have been defined:
53
* <dl>
54
* <dt><code>PC/SC</code>
55
* <dd>an implementation that calls into the PC/SC Smart Card stack
56
* of the host platform.
57
* Implementations do not require parameters and accept "null" as argument
58
* in the getInstance() calls.
59
* <dt><code>None</code>
60
* <dd>an implementation that does not supply any CardTerminals. On platforms
61
* that do not support other implementations,
62
* {@linkplain #getDefaultType} returns <code>None</code> and
63
* {@linkplain #getDefault} returns an instance of a <code>None</code>
64
* TerminalFactory. Factories of this type cannot be obtained by calling the
65
* <code>getInstance()</code> methods.
66
* </dl>
67
* Additional standard types may be defined in the future.
68
*
69
* <p><strong>Note:</strong>
70
* Provider implementations that accept initialization parameters via the
71
* <code>getInstance()</code> methods are strongly
72
* encouraged to use a {@linkplain java.util.Properties} object as the
73
* representation for String name-value pair based parameters whenever
74
* possible. This allows applications to more easily interoperate with
75
* multiple providers than if each provider used different provider
76
* specific class as parameters.
77
*
78
* <P>TerminalFactory utilizes an extensible service provider framework.
79
* Service providers that wish to add a new implementation should see the
80
* {@linkplain TerminalFactorySpi} class for more information.
81
*
82
* @see CardTerminals
83
* @see Provider
84
*
85
* @since 1.6
86
* @author Andreas Sterbenz
87
* @author JSR 268 Expert Group
88
*/
89
public final class TerminalFactory {
90
91
private final static String PROP_NAME =
92
"javax.smartcardio.TerminalFactory.DefaultType";
93
94
private final static String defaultType;
95
96
private final static TerminalFactory defaultFactory;
97
98
static {
99
// lookup up the user specified type, default to PC/SC
100
@SuppressWarnings("removal")
101
String type = AccessController.doPrivileged(
102
(PrivilegedAction<String>) () -> System.getProperty(PROP_NAME, "PC/SC")).trim();
103
TerminalFactory factory = null;
104
try {
105
factory = TerminalFactory.getInstance(type, null);
106
} catch (Exception e) {
107
// ignore
108
}
109
if (factory == null) {
110
// if that did not work, try the Sun PC/SC factory
111
try {
112
type = "PC/SC";
113
Provider sun = Security.getProvider("SunPCSC");
114
if (sun == null) {
115
@SuppressWarnings("deprecation")
116
Object o = Class.forName("sun.security.smartcardio.SunPCSC").newInstance();
117
sun = (Provider)o;
118
}
119
factory = TerminalFactory.getInstance(type, null, sun);
120
} catch (Exception e) {
121
// ignore
122
}
123
}
124
if (factory == null) {
125
type = "None";
126
factory = new TerminalFactory
127
(NoneFactorySpi.INSTANCE, NoneProvider.INSTANCE, "None");
128
}
129
defaultType = type;
130
defaultFactory = factory;
131
}
132
133
private static final class NoneProvider extends Provider {
134
135
private static final long serialVersionUID = 2745808869881593918L;
136
final static Provider INSTANCE = new NoneProvider();
137
private NoneProvider() {
138
super("None", "1.0", "none");
139
}
140
}
141
142
private static final class NoneFactorySpi extends TerminalFactorySpi {
143
final static TerminalFactorySpi INSTANCE = new NoneFactorySpi();
144
private NoneFactorySpi() {
145
// empty
146
}
147
protected CardTerminals engineTerminals() {
148
return NoneCardTerminals.INSTANCE;
149
}
150
}
151
152
private static final class NoneCardTerminals extends CardTerminals {
153
final static CardTerminals INSTANCE = new NoneCardTerminals();
154
private NoneCardTerminals() {
155
// empty
156
}
157
public List<CardTerminal> list(State state) throws CardException {
158
if (state == null) {
159
throw new NullPointerException();
160
}
161
return Collections.emptyList();
162
}
163
public boolean waitForChange(long timeout) throws CardException {
164
throw new IllegalStateException("no terminals");
165
}
166
}
167
168
private final TerminalFactorySpi spi;
169
170
private final Provider provider;
171
172
private final String type;
173
174
private TerminalFactory(TerminalFactorySpi spi, Provider provider, String type) {
175
this.spi = spi;
176
this.provider = provider;
177
this.type = type;
178
}
179
180
/**
181
* Get the default TerminalFactory type.
182
*
183
* <p>It is determined as follows:
184
*
185
* when this class is initialized, the system property
186
* {@systemProperty javax.smartcardio.TerminalFactory.DefaultType}
187
* is examined. If it is set, a TerminalFactory of this type is
188
* instantiated by calling the {@linkplain #getInstance
189
* getInstance(String,Object)} method passing
190
* <code>null</code> as the value for <code>params</code>. If the call
191
* succeeds, the type becomes the default type and the factory becomes
192
* the {@linkplain #getDefault default} factory.
193
*
194
* <p>If the system property is not set or the getInstance() call fails
195
* for any reason, the system defaults to an implementation specific
196
* default type and TerminalFactory.
197
*
198
* @return the default TerminalFactory type
199
*/
200
public static String getDefaultType() {
201
return defaultType;
202
}
203
204
/**
205
* Returns the default TerminalFactory instance. See
206
* {@linkplain #getDefaultType} for more information.
207
*
208
* <p>A default TerminalFactory is always available. However, depending
209
* on the implementation, it may not offer any terminals.
210
*
211
* @return the default TerminalFactory
212
*/
213
public static TerminalFactory getDefault() {
214
return defaultFactory;
215
}
216
217
/**
218
* Returns a TerminalFactory of the specified type that is initialized
219
* with the specified parameters.
220
*
221
* <p> This method traverses the list of registered security Providers,
222
* starting with the most preferred Provider.
223
* A new TerminalFactory object encapsulating the
224
* TerminalFactorySpi implementation from the first
225
* Provider that supports the specified type is returned.
226
*
227
* <p> Note that the list of registered providers may be retrieved via
228
* the {@linkplain Security#getProviders() Security.getProviders()} method.
229
*
230
* <p>The <code>TerminalFactory</code> is initialized with the
231
* specified parameters Object. The type of parameters
232
* needed may vary between different types of <code>TerminalFactory</code>s.
233
*
234
* @implNote
235
* The JDK Reference Implementation additionally uses the
236
* {@code jdk.security.provider.preferred}
237
* {@link Security#getProperty(String) Security} property to determine
238
* the preferred provider order for the specified algorithm. This
239
* may be different than the order of providers returned by
240
* {@link Security#getProviders() Security.getProviders()}.
241
*
242
* @param type the type of the requested TerminalFactory
243
* @param params the parameters to pass to the TerminalFactorySpi
244
* implementation, or null if no parameters are needed
245
* @return a TerminalFactory of the specified type
246
*
247
* @throws NullPointerException if type is null
248
* @throws NoSuchAlgorithmException if no Provider supports a
249
* TerminalFactorySpi of the specified type
250
*/
251
public static TerminalFactory getInstance(String type, Object params)
252
throws NoSuchAlgorithmException {
253
Instance instance = GetInstance.getInstance("TerminalFactory",
254
TerminalFactorySpi.class, type, params);
255
return new TerminalFactory((TerminalFactorySpi)instance.impl,
256
instance.provider, type);
257
}
258
259
/**
260
* Returns a TerminalFactory of the specified type that is initialized
261
* with the specified parameters.
262
*
263
* <p> A new TerminalFactory object encapsulating the
264
* TerminalFactorySpi implementation from the specified provider
265
* is returned. The specified provider must be registered
266
* in the security provider list.
267
*
268
* <p> Note that the list of registered providers may be retrieved via
269
* the {@linkplain Security#getProviders() Security.getProviders()} method.
270
*
271
* <p>The <code>TerminalFactory</code> is initialized with the
272
* specified parameters Object. The type of parameters
273
* needed may vary between different types of <code>TerminalFactory</code>s.
274
*
275
* @param type the type of the requested TerminalFactory
276
* @param params the parameters to pass to the TerminalFactorySpi
277
* implementation, or null if no parameters are needed
278
* @param provider the name of the provider
279
* @return a TerminalFactory of the specified type
280
*
281
* @throws NullPointerException if type is null
282
* @throws IllegalArgumentException if provider is null or the empty String
283
* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation
284
* of the specified type is not available from the specified provider
285
* @throws NoSuchAlgorithmException if no TerminalFactory of the
286
* specified type could be found
287
* @throws NoSuchProviderException if the specified provider could not
288
* be found
289
*/
290
public static TerminalFactory getInstance(String type, Object params,
291
String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
292
Instance instance = GetInstance.getInstance("TerminalFactory",
293
TerminalFactorySpi.class, type, params, provider);
294
return new TerminalFactory((TerminalFactorySpi)instance.impl,
295
instance.provider, type);
296
}
297
298
/**
299
* Returns a TerminalFactory of the specified type that is initialized
300
* with the specified parameters.
301
*
302
* <p> A new TerminalFactory object encapsulating the
303
* TerminalFactorySpi implementation from the specified provider object
304
* is returned. Note that the specified provider object does not have to be
305
* registered in the provider list.
306
*
307
* <p>The <code>TerminalFactory</code> is initialized with the
308
* specified parameters Object. The type of parameters
309
* needed may vary between different types of <code>TerminalFactory</code>s.
310
*
311
* @param type the type of the requested TerminalFactory
312
* @param params the parameters to pass to the TerminalFactorySpi
313
* implementation, or null if no parameters are needed
314
* @param provider the provider
315
* @return a TerminalFactory of the specified type
316
*
317
* @throws NullPointerException if type is null
318
* @throws IllegalArgumentException if provider is null
319
* @throws NoSuchAlgorithmException if a TerminalFactorySpi implementation
320
* of the specified type is not available from the specified Provider
321
*/
322
public static TerminalFactory getInstance(String type, Object params,
323
Provider provider) throws NoSuchAlgorithmException {
324
Instance instance = GetInstance.getInstance("TerminalFactory",
325
TerminalFactorySpi.class, type, params, provider);
326
return new TerminalFactory((TerminalFactorySpi)instance.impl,
327
instance.provider, type);
328
}
329
330
/**
331
* Returns the provider of this TerminalFactory.
332
*
333
* @return the provider of this TerminalFactory.
334
*/
335
public Provider getProvider() {
336
return provider;
337
}
338
339
/**
340
* Returns the type of this TerminalFactory. This is the value that was
341
* specified in the getInstance() method that returned this object.
342
*
343
* @return the type of this TerminalFactory
344
*/
345
public String getType() {
346
return type;
347
}
348
349
/**
350
* Returns a new CardTerminals object encapsulating the terminals
351
* supported by this factory.
352
* See the class comment of the {@linkplain CardTerminals} class
353
* regarding how the returned objects can be shared and reused.
354
*
355
* @return a new CardTerminals object encapsulating the terminals
356
* supported by this factory.
357
*/
358
public CardTerminals terminals() {
359
return spi.engineTerminals();
360
}
361
362
/**
363
* Returns a string representation of this TerminalFactory.
364
*
365
* @return a string representation of this TerminalFactory.
366
*/
367
public String toString() {
368
return "TerminalFactory for type " + type + " from provider "
369
+ provider.getName();
370
}
371
372
}
373
374