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 sun.security.tools;
27
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34
35 import java.io.StreamTokenizer;
36 import java.io.StringReader;
37 import java.net.URL;
38
39 import java.security.KeyStore;
40
41 import java.security.cert.X509Certificate;
42 import java.text.Collator;
43
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Properties;
49
50 import sun.security.util.PropertyExpander;
51
52 /**
53 * <p> This class provides several utilities to <code>KeyStore</code>.
54 *
55 * @since 1.6.0
56 */
57 public class KeyStoreUtil {
58
59 private KeyStoreUtil() {
60 // this class is not meant to be instantiated
61 }
62
63 private static final String JKS = "jks";
64
65 private static final Collator collator = Collator.getInstance();
66 static {
67 // this is for case insensitive string comparisons
68 collator.setStrength(Collator.PRIMARY);
242 s2 = p.getProperty(tool + "." + c2.substring(1));
243 }
244 if (s1 != null && s2 != null) {
245 throw new IOException("Cannot have both " + c1 + " and "
246 + c2 + " as pre-configured options");
247 }
248 if (s1 == null) {
249 s1 = s2;
250 }
251 if (s1 != null) {
252 parseArgsLine(result, s1);
253 }
254
255 if (result.isEmpty()) {
256 return args;
257 } else {
258 result.addAll(Arrays.asList(args));
259 return result.toArray(new String[result.size()]);
260 }
261 }
262 }
|
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 sun.security.tools;
27
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34
35 import java.io.StreamTokenizer;
36 import java.io.StringReader;
37 import java.net.URL;
38
39 import java.security.KeyStore;
40
41 import java.security.Provider;
42 import java.security.Security;
43 import java.security.cert.X509Certificate;
44 import java.text.Collator;
45
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.List;
49 import java.util.Locale;
50 import java.util.Properties;
51 import java.util.ServiceLoader;
52
53 import sun.security.util.PropertyExpander;
54
55 /**
56 * <p> This class provides several utilities to <code>KeyStore</code>.
57 *
58 * @since 1.6.0
59 */
60 public class KeyStoreUtil {
61
62 private KeyStoreUtil() {
63 // this class is not meant to be instantiated
64 }
65
66 private static final String JKS = "jks";
67
68 private static final Collator collator = Collator.getInstance();
69 static {
70 // this is for case insensitive string comparisons
71 collator.setStrength(Collator.PRIMARY);
245 s2 = p.getProperty(tool + "." + c2.substring(1));
246 }
247 if (s1 != null && s2 != null) {
248 throw new IOException("Cannot have both " + c1 + " and "
249 + c2 + " as pre-configured options");
250 }
251 if (s1 == null) {
252 s1 = s2;
253 }
254 if (s1 != null) {
255 parseArgsLine(result, s1);
256 }
257
258 if (result.isEmpty()) {
259 return args;
260 } else {
261 result.addAll(Arrays.asList(args));
262 return result.toArray(new String[result.size()]);
263 }
264 }
265
266 /**
267 * Loads a security provider in a module with its name.
268 *
269 * @param provName the name
270 * @param arg optional arg
271 * @param debug if true, print some log
272 * @throws IllegalAccessException if a provider cannot be added
273 */
274 public static void loadProviderByName(String provName, String arg, boolean debug) {
275 for (Provider p: ServiceLoader.load(Provider.class)) {
276 if (p.getName().equals(provName)) {
277 if (arg != null) {
278 p = p.configure(arg);
279 }
280 Security.addProvider(p);
281 if (debug) {
282 System.out.println("loadProviderByName: " + p);
283 }
284 return;
285 }
286 }
287 throw new IllegalArgumentException(provName);
288 }
289
290 /**
291 * Loads a security provider with its full-qualified name.
292 *
293 * @param provClass the class name
294 * @param arg optional arg
295 * @param cl optional class loader
296 * @param debug if true, print some log
297 * @throws IllegalAccessException if a provider cannot be added
298 */
299 public static void loadProviderByClass(String provClass, String arg,
300 ClassLoader cl, boolean debug) {
301 Provider prov = null;
302 for (Provider p: ServiceLoader.load(Provider.class)) {
303 // A provider in module can also be use class name
304 if (p.getClass().getName().equals(provClass)) {
305 prov = p;
306 break;
307 }
308 }
309 if (prov == null) {
310 // legacy provider on classpath
311 try {
312 Class<?> clazz;
313 if (cl != null) {
314 clazz = cl.loadClass(provClass);
315 } else {
316 clazz = Class.forName(provClass);
317 }
318 KeyStoreUtil.class.getModule().addReads(clazz.getModule());
319 Object obj = clazz.newInstance();
320 if (!(obj instanceof Provider)) {
321 throw new IllegalArgumentException(provClass);
322 }
323 prov = (Provider) obj;
324 } catch (Exception e) {
325 throw new IllegalArgumentException(provClass, e);
326 }
327 }
328 if (arg != null) {
329 prov = prov.configure(arg);
330 }
331 Security.addProvider(prov);
332 if (debug) {
333 System.out.println("loadProviderByClass: " + prov);
334 }
335 }
336 }
|