summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Gampe <agampe@google.com>2015-04-13 21:09:07 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-04-13 21:09:08 +0000
commit5b398cacaaf6deafed24cf8b851ee5d5b5b6473f (patch)
tree773bc9b4c8c5c99585fce783debf193715634aaa
parenta936d452cdb6111ce623c0d365b101acd22d03be (diff)
parent46a77743404e903bb1257999adfe046427804230 (diff)
downloadlibcore-5b398cacaaf6deafed24cf8b851ee5d5b5b6473f.zip
libcore-5b398cacaaf6deafed24cf8b851ee5d5b5b6473f.tar.gz
libcore-5b398cacaaf6deafed24cf8b851ee5d5b5b6473f.tar.bz2
Merge "Refactor security classes for static initialization"
-rw-r--r--luni/src/main/java/java/security/Security.java22
-rw-r--r--luni/src/main/java/org/apache/harmony/security/fortress/Services.java36
2 files changed, 45 insertions, 13 deletions
diff --git a/luni/src/main/java/java/security/Security.java b/luni/src/main/java/java/security/Security.java
index b859f9a..aeb189f 100644
--- a/luni/src/main/java/java/security/Security.java
+++ b/luni/src/main/java/java/security/Security.java
@@ -19,6 +19,8 @@ package java.security;
import java.io.BufferedInputStream;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
@@ -47,16 +49,25 @@ public final class Security {
// - load security properties files
// - load statically registered providers
// - if no provider description file found then load default providers
+ // Note: Getting the input stream for the security.properties file is factored into its own
+ // function, which will be intercepted during boot image creation.
static {
boolean loaded = false;
+ Reader input = null;
try {
- InputStream configStream = Security.class.getResourceAsStream("security.properties");
- InputStream input = new BufferedInputStream(configStream);
+ input = getSecurityPropertiesReader();
secprops.load(input);
loaded = true;
- configStream.close();
} catch (Exception ex) {
System.logE("Could not load 'security.properties'", ex);
+ } finally {
+ if (input != null) {
+ try {
+ input.close();
+ } catch (Exception ex) {
+ System.logW("Could not close 'security.properties'", ex);
+ }
+ }
}
if (!loaded) {
registerDefaultProviders();
@@ -64,6 +75,11 @@ public final class Security {
Engine.door = new SecurityDoor();
}
+ private static Reader getSecurityPropertiesReader() throws Exception {
+ InputStream configStream = Security.class.getResourceAsStream("security.properties");
+ return new InputStreamReader(new BufferedInputStream(configStream), "ISO-8859-1");
+ }
+
/**
* This class can't be instantiated.
*/
diff --git a/luni/src/main/java/org/apache/harmony/security/fortress/Services.java b/luni/src/main/java/org/apache/harmony/security/fortress/Services.java
index 30f4839..c81bf6b 100644
--- a/luni/src/main/java/org/apache/harmony/security/fortress/Services.java
+++ b/luni/src/main/java/org/apache/harmony/security/fortress/Services.java
@@ -63,6 +63,24 @@ public class Services {
private static final ArrayList<Provider> providers = new ArrayList<Provider>(20);
/**
+ * Try to load and register a provider by name from the given class-loader.
+ */
+ private static boolean initProvider(String providerClassName, ClassLoader classLoader) {
+ try {
+ Class<?> providerClass = Class.forName(providerClassName.trim(), true, classLoader);
+ Provider p = (Provider) providerClass.newInstance();
+ providers.add(p);
+ providersNames.put(p.getName(), p);
+ initServiceInfo(p);
+ return true;
+ } catch (ClassNotFoundException ignored) {
+ } catch (IllegalAccessException ignored) {
+ } catch (InstantiationException ignored) {
+ }
+ return false;
+ }
+
+ /**
* Hash for quick provider access by name.
*/
private static final HashMap<String, Provider> providersNames
@@ -70,18 +88,16 @@ public class Services {
static {
String providerClassName = null;
int i = 1;
- ClassLoader cl = ClassLoader.getSystemClassLoader();
+ ClassLoader cl = Services.class.getClassLoader();
while ((providerClassName = Security.getProperty("security.provider." + i++)) != null) {
- try {
- Class<?> providerClass = Class.forName(providerClassName.trim(), true, cl);
- Provider p = (Provider) providerClass.newInstance();
- providers.add(p);
- providersNames.put(p.getName(), p);
- initServiceInfo(p);
- } catch (ClassNotFoundException ignored) {
- } catch (IllegalAccessException ignored) {
- } catch (InstantiationException ignored) {
+ if (!initProvider(providerClassName, cl)) {
+ // Not on the boot classpath. Try the system class-loader.
+ // Note: DO NOT USE A LOCAL FOR GETSYSTEMCLASSLOADER! This will break compile-time
+ // initialization.
+ if (!initProvider(providerClassName, ClassLoader.getSystemClassLoader())) {
+ // TODO: Logging?
+ }
}
}
Engine.door.renumProviders();