@@ -99,8 +99,8 @@ public abstract class SecurityHelper {
99
99
private static String BC_PROVIDER_CLASS = "org.bouncycastle.jce.provider.BouncyCastleProvider" ;
100
100
static boolean setBouncyCastleProvider = true ; // (package access for tests)
101
101
static Provider securityProvider ; // 'BC' provider (package access for tests)
102
- private static Boolean registerProvider = null ;
103
- private static final Map <String , Class > implEngines = new ConcurrentHashMap <String , Class >(16 , 0.75f , 1 );
102
+ private static volatile Boolean registerProvider = null ;
103
+ static final Map <String , Class > implEngines = new ConcurrentHashMap <String , Class >(16 , 0.75f , 1 );
104
104
105
105
/**
106
106
* inject under a given name a cipher. also ensures that the registered
@@ -152,8 +152,10 @@ private static Provider newBouncyCastleProvider() {
152
152
return null ;
153
153
}
154
154
155
- public static synchronized void setRegisterProvider (boolean register ) {
156
- registerProvider = Boolean .valueOf (register ); doRegisterProvider ();
155
+ public static synchronized void setRegisterProvider (final boolean register ) {
156
+ registerProvider = Boolean .valueOf (register );
157
+ if ( register ) getSecurityProvider (); // so that securityProvider != null
158
+ // getSecurityProvider does doRegisterProvider();
157
159
}
158
160
159
161
static boolean isProviderAvailable (final String name ) {
@@ -168,13 +170,14 @@ static boolean isProviderRegistered() {
168
170
private static void doRegisterProvider () {
169
171
if ( registerProvider != null ) {
170
172
synchronized (SecurityHelper .class ) {
171
- if ( registerProvider != null && registerProvider .booleanValue () ) {
173
+ final Boolean register = registerProvider ;
174
+ if ( register != null && register .booleanValue () ) {
172
175
if ( securityProvider != null ) {
173
176
Security .addProvider (securityProvider );
177
+ registerProvider = null ;
174
178
}
175
179
}
176
180
}
177
- registerProvider = null ;
178
181
}
179
182
}
180
183
0 commit comments