Improve public key caching
also remove synchronized from hashCode()
private BasicConstraints basicConstraints;
private boolean[] keyUsage;
- private volatile PublicKey publicKeyValue;
+ private Object cacheLock = new Object();
+ private PublicKey publicKeyValue;
+
private volatile boolean hashValueSet;
private volatile int hashValue;
@@ -534,12 +536,26 @@ public PublicKey getPublicKey()
try
{
// Cache the public key to support repeated-use optimizations
- if (publicKeyValue == null)
+ synchronized (cacheLock)
+ {
+ if (null != publicKeyValue)
{
- publicKeyValue = BouncyCastleProvider.getPublicKey(c.getSubjectPublicKeyInfo());
+ return publicKeyValue;
}
+ }
+
+ PublicKey temp = BouncyCastleProvider.getPublicKey(c.getSubjectPublicKeyInfo());
+
+ synchronized (cacheLock)
+ {
+ if (null == publicKeyValue)
+ {
+ publicKeyValue = temp;
+ }
+
return publicKeyValue;
}
+ }
catch (IOException e)
{
return null; // should never happen...
@@ -585,7 +601,7 @@ public boolean equals(
return super.equals(o);
}
- public synchronized int hashCode()
+ public int hashCode()
{
if (!hashValueSet)
{
