From c2548022a1a0e8a18e37df6818836b5a842fef20 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 31 Oct 2000 00:48:13 +0000 Subject: [PATCH] Created a class for properties related utility functions. Created a function that extracts sub-properties from a global properties file. git-svn-id: https://samskivert.googlecode.com/svn/trunk@11 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/util/Makefile | 3 +- .../com/samskivert/util/PropertiesUtil.java | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 projects/samskivert/src/java/com/samskivert/util/PropertiesUtil.java diff --git a/projects/samskivert/src/java/com/samskivert/util/Makefile b/projects/samskivert/src/java/com/samskivert/util/Makefile index b5772716..d113faf1 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Makefile +++ b/projects/samskivert/src/java/com/samskivert/util/Makefile @@ -1,9 +1,10 @@ # -# $Id: Makefile,v 1.1 2000/10/31 00:04:15 mdb Exp $ +# $Id: Makefile,v 1.2 2000/10/31 00:48:13 mdb Exp $ ROOT = ../../.. SRCS = \ + PropertiesUtil.java \ StringUtil.java \ include $(ROOT)/build/Makefile.java diff --git a/projects/samskivert/src/java/com/samskivert/util/PropertiesUtil.java b/projects/samskivert/src/java/com/samskivert/util/PropertiesUtil.java new file mode 100644 index 00000000..688ca04c --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/PropertiesUtil.java @@ -0,0 +1,70 @@ +// +// $Id: PropertiesUtil.java,v 1.1 2000/10/31 00:48:13 mdb Exp $ + +package com.samskivert.util; + +import java.util.Enumeration; +import java.util.Properties; + +/** + * Utility functions related to properties objects. + */ +public class PropertiesUtil +{ + /** + * Extracts all properties from the supplied properties object with + * the specified prefix, removes the prefix from the key for those + * properties and inserts them into a new properties object which is + * then returned. This is useful for extracting properties from a + * global configuration object that must be passed to a service that + * expects it's own private properties (JDBC for example). + * + * The property file might look like so: + * + *
+     * my_happy_param=my_happy_value
+     * ...
+     * jdbc.driver=foo.bar.Driver
+     * jdbc.url=jdbc://blahblah
+     * jdbc.username=bob
+     * jdbc.password=is your uncle
+     * ...
+     * my_happy_other_param=my_happy_other_value
+     * 
+ * + * This can be supplied to getSubProperties() with a + * prefix of "jdbc" and the following properties would be + * returned: + * + *
+     * driver=foo.bar.Driver
+     * url=jdbc://blahblah
+     * username=bob
+     * password=is your uncle
+     * 
+ */ + public Properties getSubProperties (Properties source, String prefix) + { + Properties dest = new Properties(); + + // extend the prefix to contain a dot + prefix = prefix + "."; + int preflen = prefix.length(); + + // scan the source properties + Enumeration names = source.propertyNames(); + while (names.hasMoreElements()) { + String name = (String)names.nextElement(); + + // skip unrelated properties + if (!name.startsWith(prefix)) { + continue; + } + + // insert the value into the new properties minus the prefix + dest.put(name.substring(preflen), source.getProperty(name)); + } + + return dest; + } +}