diff options
author | Mark Spruiell <mes@zeroc.com> | 2009-04-07 14:47:49 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2009-04-07 14:47:49 -0700 |
commit | b6434cdb0b59adf520c4d782d991cbd5fac818cd (patch) | |
tree | a54daae0d762bb8eab8aaa596d43e926bfddcb10 /java/src | |
parent | Changes update (diff) | |
download | ice-b6434cdb0b59adf520c4d782d991cbd5fac818cd.tar.bz2 ice-b6434cdb0b59adf520c4d782d991cbd5fac818cd.tar.xz ice-b6434cdb0b59adf520c4d782d991cbd5fac818cd.zip |
bug 849 - load properties from JAR file
Ice now attempts to load a configuration file as a class path
resource, and if that fails it opens the file in the local file
system.
bug 3613 - System properties
Ice for Java no longer uses Java system properties as default
values for Ice properties.
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/Ice/PropertiesI.java | 111 | ||||
-rw-r--r-- | java/src/IceInternal/Util.java | 36 | ||||
-rw-r--r-- | java/src/IceSSL/Instance.java | 49 |
3 files changed, 81 insertions, 115 deletions
diff --git a/java/src/Ice/PropertiesI.java b/java/src/Ice/PropertiesI.java index b187c358865..78846e25241 100644 --- a/java/src/Ice/PropertiesI.java +++ b/java/src/Ice/PropertiesI.java @@ -32,49 +32,31 @@ public final class PropertiesI implements Properties public synchronized String getProperty(String key) { - String result = null; PropertyValue pv = _properties.get(key); - if(pv == null) + if(pv != null) { - try - { - result = System.getProperty(key, ""); - } - catch(java.lang.SecurityException ex) - { - result = ""; - } + pv.used = true; + return pv.value; } else { - pv.used = true; - result = pv.value; + return ""; } - return result; } public synchronized String getPropertyWithDefault(String key, String value) { - String result = null; PropertyValue pv = _properties.get(key); - if(pv == null) + if(pv != null) { - try - { - result = System.getProperty(key, value); - } - catch(java.lang.SecurityException ex) - { - result = value; - } + pv.used = true; + return pv.value; } else { - pv.used = true; - result = pv.value; + return value; } - return result; } public int @@ -86,38 +68,23 @@ public final class PropertiesI implements Properties public synchronized int getPropertyAsIntWithDefault(String key, int value) { - String result = null; PropertyValue pv = _properties.get(key); - if(pv == null) + if(pv != null) { + pv.used = true; + try { - result = System.getProperty(key); + return Integer.parseInt(pv.value); } - catch(java.lang.SecurityException ex) + catch(NumberFormatException ex) { + Ice.Util.getProcessLogger().warning("numeric property " + key + + " set to non-numeric value, defaulting to " + value); } } - else - { - pv.used = true; - result = pv.value; - } - if(result == null) - { - return value; - } - try - { - return Integer.parseInt(result); - } - catch(NumberFormatException ex) - { - Ice.Util.getProcessLogger().warning("numeric property " + key + - " set to non-numeric value, defaulting to " + value); - return value; - } + return value; } public String[] @@ -134,39 +101,28 @@ public final class PropertiesI implements Properties value = new String[0]; } - String result = null; PropertyValue pv = _properties.get(key); - if(pv == null) + if(pv != null) { - try - { - result = System.getProperty(key); - } - catch(java.lang.SecurityException ex) - { - } + pv.used = true; + + String[] result = splitString(pv.value, ", \t\r\n"); if(result == null) { + Ice.Util.getProcessLogger().warning("mismatched quotes in property " + key + + "'s value, returning default value"); return value; } + if(result.length == 0) + { + result = value; + } + return result; } else { - pv.used = true; - result = pv.value; - } - - String[] arr = splitString(result, ", \t\r\n"); - if(arr == null) - { - Ice.Util.getProcessLogger().warning("mismatched quotes in property " + key - + "'s value, returning default value"); return value; } - else - { - return arr; - } } public synchronized java.util.Map<String, String> @@ -332,8 +288,14 @@ public final class PropertiesI implements Properties { try { - java.io.FileInputStream fis = new java.io.FileInputStream(file); - java.io.InputStreamReader isr = new java.io.InputStreamReader(fis, "UTF-8"); + java.io.InputStream is = IceInternal.Util.openResource(getClass().getClassLoader(), file); + if(is == null) + { + FileException fe = new FileException(); + fe.path = file; + throw fe; + } + java.io.InputStreamReader isr = new java.io.InputStreamReader(is, "UTF-8"); java.io.BufferedReader br = new java.io.BufferedReader(isr); parse(br); } @@ -657,7 +619,8 @@ public final class PropertiesI implements Properties // // Split string helper; returns null for unmatched quotes // - private String[] splitString(String str, String delim) + private String[] + splitString(String str, String delim) { java.util.List<String> l = new java.util.ArrayList<String>(); char[] arr = new char[str.length()]; diff --git a/java/src/IceInternal/Util.java b/java/src/IceInternal/Util.java index fbfc49e1c0f..2a1f70ce69c 100644 --- a/java/src/IceInternal/Util.java +++ b/java/src/IceInternal/Util.java @@ -23,4 +23,40 @@ public final class Util { return new ProtocolPluginFacadeI(communicator); } + + // + // Given a path name, first try to open it as a class path resource (the path is + // treated as absolute). If that fails, fall back to the file system. Returns null + // if the file does not exist and raises IOException if an error occurs. + // + public static java.io.InputStream + openResource(ClassLoader cl, String path) + throws java.io.IOException + { + // + // Calling getResourceAsStream on the class loader means all paths are absolute, + // whereas calling it on the class means all paths are relative to the class + // unless the path has a leading forward slash. We call it on the class loader. + // + // getResourceAsStream returns null if the resource can't be found. + // + java.io.InputStream stream = cl.getResourceAsStream(path); + if(stream == null) + { + try + { + java.io.File f = new java.io.File(path); + if(f.exists()) + { + stream = new java.io.FileInputStream(f); + } + } + catch(java.lang.SecurityException ex) + { + // Ignore - a security manager may forbid access to the local file system. + } + } + + return stream; + } } diff --git a/java/src/IceSSL/Instance.java b/java/src/IceSSL/Instance.java index e0af23f18f4..7314a839fe1 100644 --- a/java/src/IceSSL/Instance.java +++ b/java/src/IceSSL/Instance.java @@ -1087,53 +1087,20 @@ class Instance throws java.io.IOException { // - // We resolve the path as follows: - // - // 1. Try to open it as a class path resource - // 2. Try to open it in the file system - // 3. Prepend the value of IceSSL.DefaultDir (if defined) and try to open - // it in the file system + // This method wraps a call to IceInternal.Util.openResource. If the first call fails and + // IceSSL.DefaultDir is defined, prepend the default directory and try again. // + java.io.InputStream stream = IceInternal.Util.openResource(getClass().getClassLoader(), path); + if(stream == null && _defaultDir.length() > 0) + { + stream = IceInternal.Util.openResource(getClass().getClassLoader(), + _defaultDir + java.io.File.separator + path); + } - // - // Calling getResourceAsStream on the class loader means all paths are absolute, - // whereas calling it on the class requires you to prepend "/" to the path in - // order to make it absolute, otherwise the path is interpreted relative to the - // class. - // - // getResourceAsStream returns null if the resource can't be found. - // - java.io.InputStream stream = getClass().getClassLoader().getResourceAsStream(path); if(stream != null) { stream = new java.io.BufferedInputStream(stream); } - else - { - try - { - java.io.File f = new java.io.File(path); - if(f.exists()) - { - stream = new java.io.BufferedInputStream(new java.io.FileInputStream(f)); - } - else - { - if(_defaultDir.length() > 0) - { - f = new java.io.File(_defaultDir + java.io.File.separator + path); - if(f.exists()) - { - stream = new java.io.BufferedInputStream(new java.io.FileInputStream(f)); - } - } - } - } - catch(java.lang.SecurityException ex) - { - // Ignore - a security manager may forbid access to the local file system. - } - } return stream; } |