I'm using org.apache.java.util.Configurations in my Turbine development and
am using a patch on it that is proving quite useful to me.

it is a getConfigurations(String prefix) function.

having a Configurations with the following values:

other.service.name=TheOtherService
yetanother.service.name=YetAnotherService
my.service.name=MyService
my.service.value=352
my.service.anothervalue=0

Calling getConfigurations("my.service") returns a Configurations containing
the values:

name=MyService
value=352
anothervalue=0

It is very valuable for me to have the option to pass configurations with
values that are not dependent on the service in question, and lacking a
better place, I submit the patch here, since it is not being developed in
it's original place (jserv).

Magnus



Index: src/java/org/apache/java/util/Configurations.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/java/util/Configurations.j
ava,v
retrieving revision 1.2
diff -u -r1.2 Configurations.java
--- src/java/org/apache/java/util/Configurations.java   2000/09/06 16:50:23
1.2
+++ src/java/org/apache/java/util/Configurations.java   2000/12/01 08:27:46
@@ -291,6 +291,66 @@
     }

     /**
+     * Get a new Configurations object containing only those
+     * Configurations that are specified by the prefix String.
+     * The method can also strip the prefix String off the
+     * keys in the returned Configurations.
+     *
+     * @param prefix The prefix to select
+     * @param stripPrefix If true, the prefix will be stripped
+     *        from the keys in the new Configurations
+     * @return The new configurations
+     */
+    public Configurations getConfigurations(String prefix,
+                                            boolean stripPrefix)
+    {
+        ConfigurationsRepository newRepository =
+                new ExtendedProperties();
+
+        Enumeration e = getKeys(prefix);
+        while(e.hasMoreElements())
+        {
+            String key = (String)e.nextElement();
+            String newKey = stripPrefix ? stripPrefix(key, prefix)
+                                        : key;
+
+            newRepository.put(newKey, repository.get(key));
+        }
+        return new Configurations(newRepository);
+    }
+
+    /**
+     * Returns a new Configuration object containing only those
+     * Configurations that are specified by the prefix String,
+     * stripping the prefix.
+     *
+     * @param prefix The prefix to select
+     * @param stripPrefix If true, the prefix will be stripped
+     *        from the keys in the new Configurations
+     * @return The new configurations
+     */
+    public Configurations getConfigurations(String prefix)
+    {
+        return getConfigurations(prefix, true);
+    }
+
+    /**
+     * Strips a prefix from a property key.  The method will throw
+     * an IllegalArgumentException if the key doesn't start with
+     * prefix.
+     *
+     */
+    private String stripPrefix(String key, String prefix)
+    {
+        if ( !key.startsWith(prefix) )
+        {
+            throw new IllegalArgumentException(
+                    "Key must start with prefix");
+        }
+        return key.substring(prefix.length()+1); //Strip the dot also
+    }
+
+    /**
      * Get an array of strings associated with the given configuration
      * key.
      *



------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to