You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2019/12/20 09:42:55 UTC

[sling-org-apache-sling-installer-core] 01/01: SLING-8419 JSON serializer for configurations

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/json-config-writeback
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-core.git

commit 8ac98c20db3e5a27b6401bd5ce659591a43eea85
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Dec 20 10:42:40 2019 +0100

    SLING-8419 JSON serializer for configurations
    
    Config serializers are now part of installer core so that all providers
    can just use them.
---
 pom.xml                                            | 25 +++++-
 .../api/serializer/ConfigurationSerializer.java    | 36 +++++++++
 .../serializer/ConfigurationSerializerFactory.java | 55 ++++++++++++++
 .../installer/api/serializer/package-info.java     | 21 ++++++
 .../core/impl/ConfigConfigurationSerializer.java   | 35 +++++++++
 .../core/impl/JsonConfigurationSerializer.java     | 45 +++++++++++
 .../impl/PropertiesConfigurationSerializer.java    | 51 +++++++++++++
 .../serializer/ConfigurationSerializerTest.java    | 88 ++++++++++++++++++++++
 .../sling/installer/api/serializer/config1.cfg     |  2 +
 .../sling/installer/api/serializer/config1.config  |  1 +
 .../sling/installer/api/serializer/config1.json    |  3 +
 .../sling/installer/api/serializer/config1.xml     |  5 ++
 12 files changed, 365 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 12095a2..4d84218 100644
--- a/pom.xml
+++ b/pom.xml
@@ -124,8 +124,17 @@
                     </excludePackageNames>
                 </configuration>
             </plugin>
-		</plugins>
-	</build>
+            <plugin>
+                <groupId>org.apache.rat</groupId>
+                <artifactId>apache-rat-plugin</artifactId>
+                <configuration>
+                    <excludes>
+                        <exclude>src/test/resources/**</exclude>
+                    </excludes>
+                </configuration>
+             </plugin>
+        </plugins>
+    </build>
 
     <dependencies>
         <dependency>
@@ -180,6 +189,12 @@
             <version>1.0.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.io</artifactId>
+            <version>1.1.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
       <!-- Basic dependencies for Unit Tests -->
         <dependency>
             <groupId>junit</groupId>
@@ -201,5 +216,11 @@
             <version>2.13.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializer.java
new file mode 100644
index 0000000..ec6d11d
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializer.java
@@ -0,0 +1,36 @@
+/*
+ * 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.installer.api.serializer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Dictionary;
+
+import org.jetbrains.annotations.NotNull;
+
+public interface ConfigurationSerializer {
+    /**
+     * Serializes the given dictionary (usually retrieved from OSGi Config Admin) into a specific format.
+     * Leaves the output stream open.
+     * @param dictionary
+     * @param outputStream
+     * @throws IOException
+     */
+    void serialize(@NotNull Dictionary<String, Object> dictionary, @NotNull OutputStream outputStream) throws IOException;
+}
diff --git a/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerFactory.java b/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerFactory.java
new file mode 100644
index 0000000..0e1d03c
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerFactory.java
@@ -0,0 +1,55 @@
+/*
+ * 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.installer.api.serializer;
+
+import org.apache.sling.installer.core.impl.ConfigConfigurationSerializer;
+import org.apache.sling.installer.core.impl.JsonConfigurationSerializer;
+import org.apache.sling.installer.core.impl.PropertiesConfigurationSerializer;
+import org.jetbrains.annotations.NotNull;
+
+public class ConfigurationSerializerFactory {
+    public enum Format {
+        JSON,
+        CONFIG,
+        PROPERTY,
+        PROPERTY_XML
+    };
+    
+    private ConfigurationSerializerFactory() {
+    }
+
+    public static @NotNull ConfigurationSerializer create(@NotNull Format format) {
+        final ConfigurationSerializer serializer;
+        switch (format) {
+            case CONFIG:
+                serializer = new ConfigConfigurationSerializer();
+                break;
+            case PROPERTY:
+                serializer = new PropertiesConfigurationSerializer(false);
+                break;
+            case PROPERTY_XML:
+                serializer = new PropertiesConfigurationSerializer(true);
+                break;
+            default:
+                serializer = new JsonConfigurationSerializer();
+                break;
+        }
+        return serializer;
+    }
+}
diff --git a/src/main/java/org/apache/sling/installer/api/serializer/package-info.java b/src/main/java/org/apache/sling/installer/api/serializer/package-info.java
new file mode 100644
index 0000000..45452bd
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/api/serializer/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.apache.sling.installer.api.serializer;
diff --git a/src/main/java/org/apache/sling/installer/core/impl/ConfigConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/ConfigConfigurationSerializer.java
new file mode 100644
index 0000000..6be1aff
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/ConfigConfigurationSerializer.java
@@ -0,0 +1,35 @@
+/*
+ * 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.installer.core.impl;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Dictionary;
+
+import org.apache.felix.cm.file.ConfigurationHandler;
+import org.apache.sling.installer.api.serializer.ConfigurationSerializer;
+import org.jetbrains.annotations.NotNull;
+
+public class ConfigConfigurationSerializer implements ConfigurationSerializer {
+
+    @Override
+    public void serialize(@NotNull Dictionary<String, Object> dictionary, @NotNull OutputStream outputStream) throws IOException {
+        ConfigurationHandler.write(outputStream, dictionary);
+    }
+}
diff --git a/src/main/java/org/apache/sling/installer/core/impl/JsonConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/JsonConfigurationSerializer.java
new file mode 100644
index 0000000..74ac6ad
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/JsonConfigurationSerializer.java
@@ -0,0 +1,45 @@
+/*
+ * 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.installer.core.impl;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.util.Dictionary;
+
+import org.apache.sling.feature.io.json.ConfigurationJSONWriter;
+import org.apache.sling.installer.api.serializer.ConfigurationSerializer;
+
+/** Serializes dictionary configuration objects (as specified in the configuration admin) into JSON format.
+ * 
+ * @see <a href="https://osgi.org/specification/osgi.cmpn/7.0.0/service.configurator.html">OSGi Configurator Spec</a>
+ * @see <a href=
+ *      "https://github.com/apache/felix/blob/trunk/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java">JSONUtil</a> */
+public class JsonConfigurationSerializer implements ConfigurationSerializer {
+
+    @Override
+    public void serialize(Dictionary<String, Object> dictionary, OutputStream outputStream) throws IOException {
+        Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
+        ConfigurationJSONWriter.writeConfiguration(writer, dictionary);
+        writer.flush();
+        // do not close the writer to prevent closing the underlying outputstream
+    }
+}
diff --git a/src/main/java/org/apache/sling/installer/core/impl/PropertiesConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/PropertiesConfigurationSerializer.java
new file mode 100644
index 0000000..54dbf42
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/PropertiesConfigurationSerializer.java
@@ -0,0 +1,51 @@
+/*
+ * 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.installer.core.impl;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import org.apache.sling.installer.api.serializer.ConfigurationSerializer;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.util.converter.Converter;
+import org.osgi.util.converter.Converters;
+
+public class PropertiesConfigurationSerializer implements ConfigurationSerializer {
+
+    private final boolean isXml;
+    
+    public PropertiesConfigurationSerializer(boolean isXml) {
+        this.isXml = isXml;
+    }
+
+    @Override
+    public void serialize(@NotNull Dictionary<String, Object> dictionary, @NotNull OutputStream outputStream) throws IOException {
+        // convert to properties object
+        Converter converter = Converters.standardConverter();
+        Properties properties = converter.convert(dictionary).to(Properties.class);
+        if (!isXml) {
+            properties.store(outputStream, null);
+        } else {
+            properties.storeToXML(outputStream, null);
+        }
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerTest.java b/src/test/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerTest.java
new file mode 100644
index 0000000..7f34fa3
--- /dev/null
+++ b/src/test/java/org/apache/sling/installer/api/serializer/ConfigurationSerializerTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.installer.api.serializer;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ConfigurationSerializerTest {
+
+    @Parameters(name = "Serializer:{0}")
+    public static Iterable<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                { ConfigurationSerializerFactory.Format.JSON, "config1.json" },
+                { ConfigurationSerializerFactory.Format.CONFIG, "config1.config" },
+                { ConfigurationSerializerFactory.Format.PROPERTY, "config1.cfg" },
+                { ConfigurationSerializerFactory.Format.PROPERTY_XML, "config1.xml" }
+        });
+    }
+
+    private final ConfigurationSerializer serializer;
+    private final LineIterator lineIterator;
+
+    public ConfigurationSerializerTest(ConfigurationSerializerFactory.Format format, String resource) throws IOException {
+        this.serializer = ConfigurationSerializerFactory.create(format);
+        InputStream input = this.getClass().getResourceAsStream(resource);
+        lineIterator = IOUtils.lineIterator(input, StandardCharsets.UTF_8);
+    }
+
+    @After
+    public void tearDown() throws IOException {
+        lineIterator.close();
+    }
+
+    @Test
+    public void testSerializer() throws IOException {
+        Dictionary<String, Object> dictionary = new Hashtable<>();
+
+        dictionary.put("String-value", "test");
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        serializer.serialize(dictionary, output);
+        output.close();
+
+        try (ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray())) {
+            LineIterator actualLineIterator = IOUtils.lineIterator(input, StandardCharsets.UTF_8);
+            // compare line by line
+            while (lineIterator.hasNext()) {
+                String expectedLine = lineIterator.nextLine();
+                String actualLine = actualLineIterator.nextLine();
+                // ignore lines starting with "*"
+                if (!expectedLine.equals("*")) {
+                    Assert.assertEquals(expectedLine, actualLine);
+                }
+            }
+        }
+    }
+}
diff --git a/src/test/resources/org/apache/sling/installer/api/serializer/config1.cfg b/src/test/resources/org/apache/sling/installer/api/serializer/config1.cfg
new file mode 100644
index 0000000..f091c94
--- /dev/null
+++ b/src/test/resources/org/apache/sling/installer/api/serializer/config1.cfg
@@ -0,0 +1,2 @@
+*
+String-value=test
diff --git a/src/test/resources/org/apache/sling/installer/api/serializer/config1.config b/src/test/resources/org/apache/sling/installer/api/serializer/config1.config
new file mode 100644
index 0000000..382fc81
--- /dev/null
+++ b/src/test/resources/org/apache/sling/installer/api/serializer/config1.config
@@ -0,0 +1 @@
+String-value="test"
diff --git a/src/test/resources/org/apache/sling/installer/api/serializer/config1.json b/src/test/resources/org/apache/sling/installer/api/serializer/config1.json
new file mode 100644
index 0000000..1817599
--- /dev/null
+++ b/src/test/resources/org/apache/sling/installer/api/serializer/config1.json
@@ -0,0 +1,3 @@
+{
+  "String-value":"test"
+}
\ No newline at end of file
diff --git a/src/test/resources/org/apache/sling/installer/api/serializer/config1.xml b/src/test/resources/org/apache/sling/installer/api/serializer/config1.xml
new file mode 100644
index 0000000..4b030a0
--- /dev/null
+++ b/src/test/resources/org/apache/sling/installer/api/serializer/config1.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+<properties>
+<entry key="String-value">test</entry>
+</properties>