You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/10/22 15:49:33 UTC

svn commit: r707078 - in /incubator/sling/trunk/extensions/jcrinstall/src: main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/

Author: bdelacretaz
Date: Wed Oct 22 06:49:32 2008
New Revision: 707078

URL: http://svn.apache.org/viewvc?rev=707078&view=rev
Log:
SLING-707 - go back to Properties format for cfg files, enhanced for multi-value properties

Added:
    incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java   (with props)
    incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java   (with props)
Removed:
    incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessorTest.java
Modified:
    incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessor.java

Modified: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessor.java?rev=707078&r1=707077&r2=707078&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessor.java (original)
+++ incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/ConfigResourceProcessor.java Wed Oct 22 06:49:32 2008
@@ -54,7 +54,7 @@
     public int installOrUpdate(String uri, Map<String, Object> attributes, InputStream data) throws Exception {
         
         // Load configuration properties
-        final Dictionary dict = loadDictionary(data);
+        final Dictionary dict = DictionaryReader.load(data);
         
         // Get pids from node name
         final ConfigurationPid pid = new ConfigurationPid(uri);
@@ -79,16 +79,6 @@
         return result;
     }
     
-    Dictionary<?,?> loadDictionary(InputStream data) throws IOException {
-        Dictionary<?,?> dict = null;
-        try {
-            dict = ConfigurationHandler.read(data);
-        } finally {
-            data.close();
-        }
-        return dict;
-    }
-
     public void processResourceQueue() throws Exception {
         // TODO might need to retry installing configs, as
         // we do for bundles

Added: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java?rev=707078&view=auto
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java (added)
+++ incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java Wed Oct 22 06:49:32 2008
@@ -0,0 +1,93 @@
+package org.apache.sling.jcr.jcrinstall.osgi.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Properties;
+
+/** Reads a Dictionary from an InputStream, using the
+ *  syntax of the Properties class, enhanced to support
+ *  multivalued properties.
+ */
+public class DictionaryReader {
+    public static final String ARRAY_MARKER = "[]";
+    
+    /** Read Dictionary from the given InputStream,
+     *  which is *not* closed before returning
+     */
+    public static Dictionary<?,?> load(InputStream is) throws IOException {
+        final Properties p = new Properties();
+        p.load(is);
+        return convert(p);
+    }
+    
+    /** Convert Properties to Dictionary. Properties having
+     *  a name that ends with [] are assumed to be comma-separated
+     *  lists of values, and are converted to an Array. The []
+     *  is removed from the property name. 
+     */
+    public static Dictionary<?,?> convert(Properties p) {
+        final Hashtable <String, Object> result = new Hashtable<String, Object>();
+        
+        for(Map.Entry<Object, Object> e : p.entrySet()) {
+            final String key = (String)e.getKey();
+            if(key.trim().endsWith(ARRAY_MARKER)) {
+                final String newKey = key.substring(0, key.length() - ARRAY_MARKER.length()).trim();
+                result.put(newKey, convertValue((String)e.getValue()));
+            } else {
+                result.put(key, e.getValue());
+            }
+        }
+        
+        return result;
+    }
+    
+    /** Convert value to a String[], trimming all values */
+    public static String [] convertValue(String value) {
+        
+        if(value.trim().length() == 0) {
+            return new String[0];
+        }
+        
+        return splitWithEscapes(value, ',');
+    }
+    
+    /** Split string, ignoring separators that directly follow a backslash.
+     *  All values are trimmed
+     */
+    public static String [] splitWithEscapes(String str, char separator) {
+        final ArrayList<String> a = new ArrayList<String>();
+        StringBuffer current = new StringBuffer();
+        char lastChar = 0;
+        for(int i=0; i < str.length(); i++) {
+            final char c = str.charAt(i);
+            
+            if(c == separator) {
+                if(lastChar == '\\') {
+                    // replace lastchar with c
+                    current.setCharAt(current.length() - 1, c);
+                } else if(current.length() > 0) {
+                    a.add(current.toString());
+                    current = new StringBuffer();
+                }
+            } else {
+                current.append(c);
+            }
+            
+            lastChar = c;
+        }
+        if(current.length() > 0) {
+            a.add(current.toString());
+        }
+        
+        final String [] result = new String[a.size()];
+        int i=0;
+        for(String s : a) {
+            result[i++] = s.trim();
+        }
+        return result;
+    }
+}

Propchange: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/extensions/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReader.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java?rev=707078&view=auto
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java (added)
+++ incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java Wed Oct 22 06:49:32 2008
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.jcrinstall.osgi.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Properties;
+
+/** Test the DictionaryReader */
+public class DictionaryReaderTest {
+    
+    private void assertArray(String info, String [] expected, Object obj) {
+        assertTrue(info + ":obj (" + obj.getClass().getName() + ") must be a String[]", obj instanceof String[]);
+        final String [] actual = (String[])obj;
+        assertEquals(info + ": array sizes must match", expected.length, actual.length);
+        
+        for(int i=0; i < expected.length; i++) {
+            assertEquals(info + " at index " + i, expected[i], actual[i]);
+        }
+    }
+    
+    @org.junit.Test public void testConvertValue() {
+        assertArray("one two", new String[] { "one", "two" }, DictionaryReader.convertValue("one, two\t"));
+        assertArray("one", new String[] { "one" }, DictionaryReader.convertValue("\t one "));
+        assertArray("empty array", new String[] { }, DictionaryReader.convertValue("\t \n"));
+    }
+    
+    @org.junit.Test public void testConvertValueWithEscapes() {
+        assertArray("one two", new String[] { "one", "two,three" }, DictionaryReader.convertValue("one, two\\,three"));
+        assertArray("one", new String[] { "one,two,three" }, DictionaryReader.convertValue("one\\,two\\,three"));
+    }
+    
+    @org.junit.Test public void testSplitWithEscapes() {
+        assertArray("empty", new String[0], DictionaryReader.splitWithEscapes("", ','));
+        assertArray("a", new String[] { "a" }, DictionaryReader.splitWithEscapes("a", ','));
+        assertArray("multi", new String[] { "a\\,,b\\" }, DictionaryReader.splitWithEscapes("a\\\\,\\,b\\", ','));
+        assertArray("a,b", new String[] { "a", "b" }, DictionaryReader.splitWithEscapes("a, b\t", ','));
+        assertArray("a,b,c", new String[] { "a", "b, c" }, DictionaryReader.splitWithEscapes("a, b\\, c\t", ','));
+        assertArray("a,b,c,d", new String[] { "a", "b, c ,", "d" }, DictionaryReader.splitWithEscapes("a, b\\, c \\,,d ", ','));
+    }
+     
+    @org.junit.Test public void testConvertProperties() {
+        final Properties p = new Properties();
+        p.setProperty("a", "1");
+        p.setProperty("b", "2");
+        p.setProperty("c[]", "1, 2, 3");
+        p.setProperty("d []", "4, 5, 6");
+        
+        final Dictionary<?, ?> d = DictionaryReader.convert(p);
+        assertEquals("a", d.get("a"), "1");
+        assertEquals("b", d.get("b"), "2");
+        
+        assertArray("c", new String[] { "1", "2", "3" }, d.get("c"));
+        assertArray("d", new String[] { "4", "5", "6" }, d.get("d"));
+    }
+    
+    @org.junit.Test public void testFromStream() throws IOException {
+        final String data =
+            "a = 1\n"
+            + "b = this is B\n"
+            + "# a comment\n"
+            + "! another comment\n"
+            + "c[] = 1,2\\\\,A , 3 \n"
+            + "d=12\n"
+            ;
+        
+        final ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
+        Dictionary<?, ?> d = null;
+        try {
+            d = DictionaryReader.load(is);
+        } finally {
+            is.close();
+        }
+        
+        assertEquals("Number of entries must match", 4, d.size());
+        assertEquals("a", d.get("a"), "1");
+        assertEquals("b", d.get("b"), "this is B");
+        
+        assertArray("c", new String[] { "1", "2,A", "3" }, d.get("c"));
+        assertEquals("d", d.get("d"), "12");
+    }
+}

Propchange: incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/extensions/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/osgi/impl/DictionaryReaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL