Finished the config loading stuff.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@47 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: ConfigUtil.java,v 1.1 2001/02/15 01:09:57 mdb Exp $
|
// $Id: ConfigUtil.java,v 1.2 2001/02/15 01:15:02 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
@@ -27,6 +27,9 @@ public class ConfigUtil
|
|||||||
* @param path The path to the properties file, relative to the root
|
* @param path The path to the properties file, relative to the root
|
||||||
* of the classpath entry from which it will be loaded
|
* of the classpath entry from which it will be loaded
|
||||||
* (e.g. /conf/foo.properties or perhaps just bar.properties).
|
* (e.g. /conf/foo.properties or perhaps just bar.properties).
|
||||||
|
*
|
||||||
|
* @return A properties object loaded with the contents of the
|
||||||
|
* specified file if the file could be found, null otherwise.
|
||||||
*/
|
*/
|
||||||
public static Properties loadProperties (String path)
|
public static Properties loadProperties (String path)
|
||||||
throws IOException
|
throws IOException
|
||||||
@@ -46,11 +49,22 @@ public class ConfigUtil
|
|||||||
* @param path The path to the properties file, relative to the root
|
* @param path The path to the properties file, relative to the root
|
||||||
* of the classpath entry from which it will be loaded
|
* of the classpath entry from which it will be loaded
|
||||||
* (e.g. /conf/foo.properties or perhaps just bar.properties).
|
* (e.g. /conf/foo.properties or perhaps just bar.properties).
|
||||||
|
*
|
||||||
|
* @return A properties object loaded with the contents of the
|
||||||
|
* specified file if the file could be found, null otherwise.
|
||||||
*/
|
*/
|
||||||
public static Properties loadProperties (String name, ClassLoader loader)
|
public static Properties loadProperties (String name, ClassLoader loader)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
return null;
|
InputStream in = getStream(name, loader);
|
||||||
|
Properties props = null;
|
||||||
|
|
||||||
|
if (in != null) {
|
||||||
|
props = new Properties();
|
||||||
|
props.load(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,10 +108,33 @@ public class ConfigUtil
|
|||||||
{
|
{
|
||||||
// first try the supplied class loader
|
// first try the supplied class loader
|
||||||
InputStream in = loader.getResourceAsStream(path);
|
InputStream in = loader.getResourceAsStream(path);
|
||||||
if (in == null) {
|
if (in != null) {
|
||||||
// if that didn't work, try the system classloader
|
return in;
|
||||||
in = Class.class.getResourceAsStream(path);
|
}
|
||||||
|
|
||||||
|
// try toggling the leading slash
|
||||||
|
String apath = togglePath(path);
|
||||||
|
in = loader.getResourceAsStream(apath);
|
||||||
|
if (in != null) {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if that didn't work, try the system classloader
|
||||||
|
in = Class.class.getResourceAsStream(path);
|
||||||
|
if (in != null) {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
// help us obi wan, you're our only hope
|
||||||
|
return Class.class.getResourceAsStream(apath);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String togglePath (String path)
|
||||||
|
{
|
||||||
|
if (path.startsWith("/")) {
|
||||||
|
return path.substring(1);
|
||||||
|
} else {
|
||||||
|
return "/" + path;
|
||||||
}
|
}
|
||||||
return in;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user