|
53 | 53 | import java.security.cert.CertificateFactory;
|
54 | 54 | import java.security.cert.CertificateFactorySpi;
|
55 | 55 | import java.security.cert.X509CRL;
|
| 56 | +import java.util.Map; |
56 | 57 | import java.util.StringTokenizer;
|
| 58 | +import java.util.concurrent.ConcurrentHashMap; |
57 | 59 |
|
58 | 60 | import javax.crypto.Cipher;
|
59 | 61 | import javax.crypto.CipherSpi;
|
@@ -83,6 +85,7 @@ public abstract class SecurityHelper {
|
83 | 85 | static boolean setBouncyCastleProvider = true; // (package access for tests)
|
84 | 86 | static Provider securityProvider; // 'BC' provider (package access for tests)
|
85 | 87 | private static Boolean registerProvider = null;
|
| 88 | + private static final Map<String, Class> implEngines = new ConcurrentHashMap<String, Class>(16, 0.75f, 1); |
86 | 89 |
|
87 | 90 | public static Provider getSecurityProvider() {
|
88 | 91 | if ( setBouncyCastleProvider && securityProvider == null ) {
|
@@ -607,31 +610,43 @@ private static Object getImplEngine(String baseName, String algorithm) {
|
607 | 610 | }
|
608 | 611 |
|
609 | 612 | private static Object findImplEngine(final String baseName, String algorithm) {
|
610 |
| - final Provider bcProvider = securityProvider; |
611 |
| - String alias; |
612 |
| - while ((alias = bcProvider.getProperty("Alg.Alias." + baseName + "." + algorithm)) != null) { |
613 |
| - algorithm = alias; |
614 |
| - } |
615 |
| - final String className = bcProvider.getProperty(baseName + "." + algorithm); |
616 |
| - if (className != null) { |
617 |
| - try { |
618 |
| - Class klass; |
619 |
| - ClassLoader loader = bcProvider.getClass().getClassLoader(); |
620 |
| - if (loader != null) { |
621 |
| - klass = loader.loadClass(className); |
622 |
| - } else { |
623 |
| - klass = Class.forName(className); |
624 |
| - } |
625 |
| - return klass.newInstance(); |
626 |
| - } |
627 |
| - catch (ClassNotFoundException e) { |
628 |
| - throw new IllegalStateException("algorithm " + algorithm + " in provider " + bcProvider.getName() + " but no class \"" + className + "\" found!"); |
| 613 | + Class implEngineClass = implEngines.get(baseName + ":" + algorithm); |
| 614 | + |
| 615 | + if (implEngineClass == null) { |
| 616 | + final Provider bcProvider = securityProvider; |
| 617 | + String alias; |
| 618 | + while ((alias = bcProvider.getProperty("Alg.Alias." + baseName + "." + algorithm)) != null) { |
| 619 | + algorithm = alias; |
629 | 620 | }
|
630 |
| - catch (Exception e) { |
631 |
| - throw new IllegalStateException("algorithm " + algorithm + " in provider " + bcProvider.getName() + " but class \"" + className + "\" inaccessible!"); |
| 621 | + final String className = bcProvider.getProperty(baseName + "." + algorithm); |
| 622 | + if (className != null) { |
| 623 | + try { |
| 624 | + ClassLoader loader = bcProvider.getClass().getClassLoader(); |
| 625 | + if (loader != null) { |
| 626 | + implEngineClass = loader.loadClass(className); |
| 627 | + } else { |
| 628 | + implEngineClass = Class.forName(className); |
| 629 | + } |
| 630 | + implEngineClass.newInstance(); // this instance is thrown away to test newInstance, but only once |
| 631 | + } catch (ClassNotFoundException e) { |
| 632 | + throw new IllegalStateException("algorithm " + algorithm + " in provider " + bcProvider.getName() + " but no class \"" + className + "\" found!"); |
| 633 | + } catch (Exception e) { |
| 634 | + throw new IllegalStateException("algorithm " + algorithm + " in provider " + bcProvider.getName() + " but class \"" + className + "\" inaccessible!"); |
| 635 | + } |
| 636 | + } else { |
| 637 | + return null; |
632 | 638 | }
|
| 639 | + |
| 640 | + implEngines.put(baseName + ":" + algorithm, implEngineClass); |
| 641 | + } |
| 642 | + |
| 643 | + try { |
| 644 | + return implEngineClass.newInstance(); |
| 645 | + } catch (Exception e) { |
| 646 | + final Provider bcProvider = securityProvider; |
| 647 | + String className = implEngineClass.getName(); |
| 648 | + throw new IllegalStateException("algorithm " + algorithm + " in provider " + bcProvider.getName() + " but class \"" + className + "\" inaccessible!"); |
633 | 649 | }
|
634 |
| - return null; |
635 | 650 | }
|
636 | 651 |
|
637 | 652 | // the obligratory "reflection crap" :
|
|
0 commit comments