* Use sub-properties for getting DbSource settings and comparing DbSource instances.

Fixes http://helma.org/bugs/show_bug.cgi?id=500 and improves the code.
This commit is contained in:
hns 2007-02-21 10:45:24 +00:00
parent 43165e0bab
commit cecd2af590

View file

@ -32,7 +32,7 @@ public class DbSource {
private static ResourceProperties defaultProps = null; private static ResourceProperties defaultProps = null;
private Properties conProps; private Properties conProps;
private String name; private String name;
private ResourceProperties props; private ResourceProperties props, subProps;
protected String url; protected String url;
private String driver; private String driver;
private boolean isOracle, isMySQL, isPostgreSQL; private boolean isOracle, isMySQL, isPostgreSQL;
@ -103,11 +103,13 @@ public class DbSource {
lastRead = (defaultProps == null) ? props.lastModified() lastRead = (defaultProps == null) ? props.lastModified()
: Math.max(props.lastModified(), : Math.max(props.lastModified(),
defaultProps.lastModified()); defaultProps.lastModified());
// refresh sub-properties for this DbSource
subProps = props.getSubProperties(name + '.');
// use properties hashcode for ourselves // use properties hashcode for ourselves
hashcode = props.hashCode(); hashcode = subProps.hashCode();
// get JDBC URL and driver class name // get JDBC URL and driver class name
url = props.getProperty(name + ".url"); url = subProps.getProperty("url");
driver = props.getProperty(name + ".driver"); driver = subProps.getProperty("driver");
// sanity checks // sanity checks
if (url == null) { if (url == null) {
throw new NullPointerException(name+".url is not defined in db.properties"); throw new NullPointerException(name+".url is not defined in db.properties");
@ -125,25 +127,19 @@ public class DbSource {
// set up driver connection properties // set up driver connection properties
conProps=new Properties(); conProps=new Properties();
String prop = props.getProperty(name + ".user"); String prop = subProps.getProperty("user");
if (prop != null) { if (prop != null) {
conProps.put("user", prop); conProps.put("user", prop);
} }
prop = props.getProperty(name + ".password"); prop = subProps.getProperty("password");
if (prop != null) { if (prop != null) {
conProps.put("password", prop); conProps.put("password", prop);
} }
// read any remaining extra properties to be passed to the driver // read any remaining extra properties to be passed to the driver
for (Enumeration e = props.keys(); e.hasMoreElements(); ) { for (Enumeration e = subProps.keys(); e.hasMoreElements(); ) {
String fullkey = (String) e.nextElement(); String key = (String) e.nextElement();
int dot = fullkey.indexOf('.');
// filter out properties that don't belong to this data source
if (dot < 0 || !fullkey.substring(0, dot).equalsIgnoreCase(name)) {
continue;
}
String key = fullkey.substring(dot+1);
// filter out properties we alread have // filter out properties we alread have
if ("url".equalsIgnoreCase(key) || if ("url".equalsIgnoreCase(key) ||
"driver".equalsIgnoreCase(key) || "driver".equalsIgnoreCase(key) ||
@ -151,7 +147,7 @@ public class DbSource {
"password".equalsIgnoreCase(key)) { "password".equalsIgnoreCase(key)) {
continue; continue;
} }
conProps.setProperty(key, props.getProperty(fullkey)); conProps.setProperty(key, subProps.getProperty(key));
} }
} }
@ -240,6 +236,6 @@ public class DbSource {
* Indicates whether some other object is "equal to" this one. * Indicates whether some other object is "equal to" this one.
*/ */
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof DbSource && props.equals(((DbSource) obj).props); return obj instanceof DbSource && subProps.equals(((DbSource) obj).subProps);
} }
} }