Commit 8ae047f5 authored by Brian Carlstrom's avatar Brian Carlstrom Committed by Android (Google) Code Review
Browse files

Merge "Change Engine.getInstance interfaces to make usage less error prone"

parents 490a5fc2 6cdb6b7e
......@@ -90,12 +90,9 @@ public class AlgorithmParameterGenerator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new AlgorithmParameterGenerator(
(AlgorithmParameterGeneratorSpi) ENGINE.getSpi(), ENGINE.getProvider(),
algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) sap.spi,
sap.provider, algorithm);
}
/**
......@@ -154,11 +151,9 @@ public class AlgorithmParameterGenerator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new AlgorithmParameterGenerator(
(AlgorithmParameterGeneratorSpi) ENGINE.getSpi(), provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) spi, provider,
algorithm);
}
/**
......
......@@ -94,11 +94,8 @@ public class AlgorithmParameters {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new AlgorithmParameters((AlgorithmParametersSpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new AlgorithmParameters((AlgorithmParametersSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -156,11 +153,8 @@ public class AlgorithmParameters {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new AlgorithmParameters((AlgorithmParametersSpi) ENGINE.getSpi(),
provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new AlgorithmParameters((AlgorithmParametersSpi) spi, provider, algorithm);
}
/**
......
......@@ -78,10 +78,8 @@ public class KeyFactory {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new KeyFactory((KeyFactorySpi) ENGINE.getSpi(), ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new KeyFactory((KeyFactorySpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -135,10 +133,8 @@ public class KeyFactory {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new KeyFactory((KeyFactorySpi) ENGINE.getSpi(), provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new KeyFactory((KeyFactorySpi) spi, provider, algorithm);
}
/**
......
......@@ -82,13 +82,9 @@ public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
if (algorithm == null) {
throw new NullPointerException();
}
Object spi;
Provider provider;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
spi = ENGINE.getSpi();
provider = ENGINE.getProvider();
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
Object spi = sap.spi;
Provider provider = sap.provider;
if (spi instanceof KeyPairGenerator) {
KeyPairGenerator result = (KeyPairGenerator) spi;
result.algorithm = algorithm;
......@@ -149,11 +145,7 @@ public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
if (algorithm == null) {
throw new NullPointerException();
}
Object spi;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
spi = ENGINE.getSpi();
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
if (spi instanceof KeyPairGenerator) {
KeyPairGenerator result = (KeyPairGenerator) spi;
result.algorithm = algorithm;
......
......@@ -111,13 +111,11 @@ public class KeyStore {
if (type == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
try {
ENGINE.getInstance(type, null);
return new KeyStore((KeyStoreSpi) ENGINE.getSpi(), ENGINE.getProvider(), type);
} catch (NoSuchAlgorithmException e) {
try {
Engine.SpiAndProvider sap = ENGINE.getInstance(type, null);
return new KeyStore((KeyStoreSpi) sap.spi, sap.provider, type);
} catch (NoSuchAlgorithmException e) {
throw new KeyStoreException(e.getMessage());
}
}
}
......@@ -187,14 +185,12 @@ public class KeyStore {
throw new NullPointerException();
}
// return KeyStore instance
synchronized (ENGINE) {
try {
ENGINE.getInstance(type, provider, null);
return new KeyStore((KeyStoreSpi) ENGINE.getSpi(), provider, type);
} catch (Exception e) {
try {
Object spi = ENGINE.getInstance(type, provider, null);
return new KeyStore((KeyStoreSpi) spi, provider, type);
} catch (Exception e) {
// override exception
throw new KeyStoreException(e.getMessage());
}
throw new KeyStoreException(e.getMessage());
}
}
......
......@@ -88,20 +88,16 @@ public abstract class MessageDigest extends MessageDigestSpi {
if (algorithm == null) {
throw new NullPointerException();
}
Object spi;
Provider provider;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
spi = ENGINE.getSpi();
provider = ENGINE.getProvider();
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
Object spi = sap.spi;
Provider provider = sap.provider;
if (spi instanceof MessageDigest) {
MessageDigest result = (MessageDigest) spi;
result.algorithm = algorithm;
result.provider = provider;
return result;
}
return new MessageDigestImpl((MessageDigestSpi) spi, provider, algorithm);
return new MessageDigestImpl((MessageDigestSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -158,11 +154,7 @@ public abstract class MessageDigest extends MessageDigestSpi {
if (algorithm == null) {
throw new NullPointerException();
}
Object spi;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
spi = ENGINE.getSpi();
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
if (spi instanceof MessageDigest) {
MessageDigest result = (MessageDigest) spi;
result.algorithm = algorithm;
......
......@@ -126,12 +126,8 @@ public abstract class Policy {
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, params);
return new PolicyDelegate((PolicySpi) ENGINE.getSpi(),
ENGINE.getProvider(), type, params);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(type, params);
return new PolicyDelegate((PolicySpi) sap.spi, sap.provider, type, params);
} catch (NoSuchAlgorithmException e) {
if (e.getCause() == null) {
throw e;
......@@ -242,11 +238,8 @@ public abstract class Policy {
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, provider, params);
return new PolicyDelegate((PolicySpi) ENGINE.getSpi(), provider,
type, params);
}
Object spi = ENGINE.getInstance(type, provider, params);
return new PolicyDelegate((PolicySpi) spi, provider, type, params);
} catch (NoSuchAlgorithmException e) {
if (e.getCause() == null) {
throw e;
......
......@@ -124,11 +124,9 @@ public class SecureRandom extends Random {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new SecureRandom((SecureRandomSpi) ENGINE.getSpi(), ENGINE.getProvider(),
algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new SecureRandom((SecureRandomSpi) sap.spi, sap.provider,
algorithm);
}
/**
......@@ -185,10 +183,8 @@ public class SecureRandom extends Random {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new SecureRandom((SecureRandomSpi) ENGINE.getSpi(), provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new SecureRandom((SecureRandomSpi) spi, provider, algorithm);
}
/**
......
......@@ -101,13 +101,9 @@ public abstract class Signature extends SignatureSpi {
if (algorithm == null) {
throw new NullPointerException();
}
Object spi;
Provider provider;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
spi = ENGINE.getSpi();
provider = ENGINE.getProvider();
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
Object spi = sap.spi;
Provider provider = sap.provider;
if (spi instanceof Signature) {
Signature result = (Signature) spi;
result.algorithm = algorithm;
......@@ -179,11 +175,7 @@ public abstract class Signature extends SignatureSpi {
private static Signature getSignatureInstance(String algorithm,
Provider provider) throws NoSuchAlgorithmException {
Object spi;
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
spi = ENGINE.getSpi();
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
if (spi instanceof Signature) {
Signature result = (Signature) spi;
result.algorithm = algorithm;
......
......@@ -105,11 +105,8 @@ public class CertPathBuilder {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new CertPathBuilder((CertPathBuilderSpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new CertPathBuilder((CertPathBuilderSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -165,10 +162,8 @@ public class CertPathBuilder {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new CertPathBuilder((CertPathBuilderSpi) ENGINE.getSpi(), provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new CertPathBuilder((CertPathBuilderSpi) spi, provider, algorithm);
}
/**
......
......@@ -104,11 +104,8 @@ public class CertPathValidator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new CertPathValidator((CertPathValidatorSpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new CertPathValidator((CertPathValidatorSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -166,11 +163,8 @@ public class CertPathValidator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new CertPathValidator((CertPathValidatorSpi) ENGINE.getSpi(),
provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new CertPathValidator((CertPathValidatorSpi) spi, provider, algorithm);
}
/**
......
......@@ -101,11 +101,8 @@ public class CertStore {
throw new NullPointerException();
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, params);
return new CertStore((CertStoreSpi) ENGINE.getSpi(), ENGINE.getProvider(),
type, params);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(type, params);
return new CertStore((CertStoreSpi) sap.spi, sap.provider, type, params);
} catch (NoSuchAlgorithmException e) {
Throwable th = e.getCause();
if (th == null) {
......@@ -182,11 +179,8 @@ public class CertStore {
throw new NullPointerException();
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, provider, params);
return new CertStore((CertStoreSpi) ENGINE.getSpi(), provider, type,
params);
}
Object spi = ENGINE.getInstance(type, provider, params);
return new CertStore((CertStoreSpi) spi, provider, type, params);
} catch (NoSuchAlgorithmException e) {
Throwable th = e.getCause();
if (th == null) {
......
......@@ -87,11 +87,8 @@ public class CertificateFactory {
throw new NullPointerException();
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, null);
return new CertificateFactory((CertificateFactorySpi) ENGINE.getSpi(),
ENGINE.getProvider(), type);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(type, null);
return new CertificateFactory((CertificateFactorySpi) sap.spi, sap.provider, type);
} catch (NoSuchAlgorithmException e) {
throw new CertificateException(e);
}
......@@ -156,11 +153,8 @@ public class CertificateFactory {
throw new NullPointerException();
}
try {
synchronized (ENGINE) {
ENGINE.getInstance(type, provider, null);
return new CertificateFactory((CertificateFactorySpi) ENGINE.getSpi(),
provider, type);
}
Object spi = ENGINE.getInstance(type, provider, null);
return new CertificateFactory((CertificateFactorySpi) spi, provider, type);
} catch (NoSuchAlgorithmException e) {
throw new CertificateException(e.getMessage());
}
......
......@@ -264,51 +264,54 @@ public class Cipher {
boolean needSetPadding = false;
boolean needSetMode = false;
Object engineSpi;
Provider engineProvider;
synchronized (ENGINE) {
if (transf[1] == null && transf[2] == null) { // "algorithm"
if (provider == null) {
ENGINE.getInstance(transf[0], null);
} else {
ENGINE.getInstance(transf[0], provider, null);
}
Object engineSpi = null;
Provider engineProvider = provider;
if (transf[1] == null && transf[2] == null) { // "algorithm"
if (provider == null) {
Engine.SpiAndProvider sap = ENGINE.getInstance(transf[0], null);
engineSpi = sap.spi;
engineProvider = sap.provider;
} else {
String[] searhOrder = {
transf[0] + "/" + transf[1] + "/" + transf[2], // "algorithm/mode/padding"
transf[0] + "/" + transf[1], // "algorithm/mode"
transf[0] + "//" + transf[2], // "algorithm//padding"
transf[0] // "algorithm"
};
int i;
for (i = 0; i < searhOrder.length; i++) {
try {
if (provider == null) {
ENGINE.getInstance(searhOrder[i], null);
} else {
ENGINE.getInstance(searhOrder[i], provider, null);
}
break;
} catch (NoSuchAlgorithmException e) {
if ( i == searhOrder.length-1) {
throw new NoSuchAlgorithmException(transformation);
}
engineSpi = ENGINE.getInstance(transf[0], provider, null);
}
} else {
String[] searchOrder = {
transf[0] + "/" + transf[1] + "/" + transf[2], // "algorithm/mode/padding"
transf[0] + "/" + transf[1], // "algorithm/mode"
transf[0] + "//" + transf[2], // "algorithm//padding"
transf[0] // "algorithm"
};
int i;
for (i = 0; i < searchOrder.length; i++) {
try {
if (provider == null) {
Engine.SpiAndProvider sap = ENGINE.getInstance(searchOrder[i], null);
engineSpi = sap.spi;
engineProvider = sap.provider;
} else {
engineSpi = ENGINE.getInstance(searchOrder[i], provider, null);
}
break;
} catch (NoSuchAlgorithmException e) {
if (i == searchOrder.length-1) {
throw new NoSuchAlgorithmException(transformation);
}
}
switch (i) {
case 1: // "algorithm/mode"
needSetPadding = true;
break;
case 2: // "algorithm//padding"
needSetMode = true;
break;
case 3: // "algorithm"
needSetPadding = true;
needSetMode = true;
}
}
engineSpi = ENGINE.getSpi();
engineProvider = ENGINE.getProvider();
switch (i) {
case 1: // "algorithm/mode"
needSetPadding = true;
break;
case 2: // "algorithm//padding"
needSetMode = true;
break;
case 3: // "algorithm"
needSetPadding = true;
needSetMode = true;
}
}
if (engineSpi == null || engineProvider == null) {
throw new NoSuchAlgorithmException(transformation);
}
if (!(engineSpi instanceof CipherSpi)) {
throw new NoSuchAlgorithmException(engineSpi.getClass().getName());
......
......@@ -100,11 +100,8 @@ public class ExemptionMechanism {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new ExemptionMechanism((ExemptionMechanismSpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new ExemptionMechanism((ExemptionMechanismSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -167,11 +164,8 @@ public class ExemptionMechanism {
if (provider == null) {
throw new IllegalArgumentException("provider == null");
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new ExemptionMechanism((ExemptionMechanismSpi) ENGINE.getSpi(),
provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new ExemptionMechanism((ExemptionMechanismSpi) spi, provider, algorithm);
}
/**
......
......@@ -101,11 +101,8 @@ public class KeyAgreement {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new KeyAgreement((KeyAgreementSpi) ENGINE.getSpi(), ENGINE.getProvider(),
algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new KeyAgreement((KeyAgreementSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -166,11 +163,8 @@ public class KeyAgreement {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new KeyAgreement((KeyAgreementSpi) ENGINE.getSpi(), provider,
algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new KeyAgreement((KeyAgreementSpi) spi, provider, algorithm);
}
/**
......
......@@ -100,11 +100,8 @@ public class KeyGenerator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new KeyGenerator((KeyGeneratorSpi) ENGINE.getSpi(), ENGINE.getProvider(),
algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new KeyGenerator((KeyGeneratorSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -163,11 +160,8 @@ public class KeyGenerator {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new KeyGenerator((KeyGeneratorSpi) ENGINE.getSpi(), provider,
algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new KeyGenerator((KeyGeneratorSpi) spi, provider, algorithm);
}
/**
......
......@@ -103,10 +103,8 @@ public class Mac implements Cloneable {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new Mac((MacSpi) ENGINE.getSpi(), ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new Mac((MacSpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -167,10 +165,8 @@ public class Mac implements Cloneable {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new Mac((MacSpi) ENGINE.getSpi(), provider, algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new Mac((MacSpi) spi, provider, algorithm);
}
/**
......
......@@ -105,11 +105,8 @@ public class SecretKeyFactory {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new SecretKeyFactory((SecretKeyFactorySpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new SecretKeyFactory((SecretKeyFactorySpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -170,11 +167,8 @@ public class SecretKeyFactory {
if (algorithm == null) {
throw new NullPointerException();
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new SecretKeyFactory((SecretKeyFactorySpi) ENGINE.getSpi(), provider,
algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new SecretKeyFactory((SecretKeyFactorySpi) spi, provider, algorithm);
}
/**
......
......@@ -76,11 +76,8 @@ public class KeyManagerFactory {
if (algorithm == null) {
throw new NullPointerException("algorithm is null");
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, null);
return new KeyManagerFactory((KeyManagerFactorySpi) ENGINE.getSpi(),
ENGINE.getProvider(), algorithm);
}
Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
return new KeyManagerFactory((KeyManagerFactorySpi) sap.spi, sap.provider, algorithm);
}
/**
......@@ -138,11 +135,8 @@ public class KeyManagerFactory {
if (algorithm == null) {
throw new NullPointerException("algorithm is null");
}
synchronized (ENGINE) {
ENGINE.getInstance(algorithm, provider, null);
return new KeyManagerFactory((KeyManagerFactorySpi) ENGINE.getSpi(), provider,
algorithm);
}
Object spi = ENGINE.getInstance(algorithm, provider, null);
return new KeyManagerFactory((KeyManagerFactorySpi) spi, provider, algorithm);
}
// Store used provider
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment