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 2022/05/26 18:46:13 UTC

[sling-org-apache-sling-installer-console] 01/01: SLING-11344 embed Serializer Api/Impl from core

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

kwin pushed a commit to branch compat-with-installer-core3.9
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-console.git

commit 5a4cbe4fd2b16ad9ac7cd2ae3af8d5c5db373985
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu May 26 20:45:58 2022 +0200

    SLING-11344 embed Serializer Api/Impl from core
    
    Downgrade Installer Core dependency to 3.9.0
    Update to newest parent 47
---
 bnd.bnd                                            |  1 +
 pom.xml                                            | 37 +++++++++++++--
 .../api/serializer/ConfigurationSerializer.java    | 36 ++++++++++++++
 .../serializer/ConfigurationSerializerFactory.java | 55 ++++++++++++++++++++++
 .../serializer/ConfigConfigurationSerializer.java  | 35 ++++++++++++++
 .../serializer/JsonConfigurationSerializer.java    | 44 +++++++++++++++++
 .../PropertiesConfigurationSerializer.java         | 51 ++++++++++++++++++++
 7 files changed, 256 insertions(+), 3 deletions(-)

diff --git a/bnd.bnd b/bnd.bnd
new file mode 100644
index 0000000..a22f964
--- /dev/null
+++ b/bnd.bnd
@@ -0,0 +1 @@
+-includeresource: @org.apache.felix.cm.json-[0-9.]*.jar
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 0282e95..eea15bd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,12 +23,12 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling-bundle-parent</artifactId>
-        <version>38</version>
+        <version>47</version>
         <relativePath />
     </parent>
 
     <artifactId>org.apache.sling.installer.console</artifactId>
-    <version>1.1.1-SNAPSHOT</version>
+    <version>1.0.3-SNAPSHOT</version>
 
     <name>Apache Sling Installer WebConsole Plugin</name>
     <description> 
@@ -70,7 +70,12 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.installer.core</artifactId>
-            <version>3.10.0</version>
+            <version>3.9.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
             <scope>provided</scope>
         </dependency>
         <dependency>
@@ -78,5 +83,31 @@
             <artifactId>javax.servlet-api</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.util.converter</artifactId>
+            <version>1.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <!-- We use a class from the config admin implementation to read config files -->
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.configadmin</artifactId>
+            <version>1.8.12</version>
+            <scope>provided</scope>
+        </dependency>
+        <!-- JSON Configurations -->
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.cm.json</artifactId>
+            <version>1.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.johnzon</artifactId>
+            <version>1.2.0</version>
+            <scope>provided</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..48b59f3
--- /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.serializer.ConfigConfigurationSerializer;
+import org.apache.sling.installer.core.impl.serializer.JsonConfigurationSerializer;
+import org.apache.sling.installer.core.impl.serializer.PropertiesConfigurationSerializer;
+import org.jetbrains.annotations.NotNull;
+
+public class ConfigurationSerializerFactory {
+    public enum Format {
+        JSON,
+        CONFIG,
+        PROPERTIES,
+        PROPERTIES_XML
+    }
+    
+    private ConfigurationSerializerFactory() {
+    }
+
+    public static @NotNull ConfigurationSerializer create(@NotNull Format format) {
+        final ConfigurationSerializer serializer;
+        switch (format) {
+            case CONFIG:
+                serializer = new ConfigConfigurationSerializer();
+                break;
+            case PROPERTIES:
+                serializer = new PropertiesConfigurationSerializer(false);
+                break;
+            case PROPERTIES_XML:
+                serializer = new PropertiesConfigurationSerializer(true);
+                break;
+            default:
+                serializer = new JsonConfigurationSerializer();
+                break;
+        }
+        return serializer;
+    }
+}
diff --git a/src/main/java/org/apache/sling/installer/core/impl/serializer/ConfigConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/serializer/ConfigConfigurationSerializer.java
new file mode 100644
index 0000000..ced6744
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/serializer/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.serializer;
+
+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/serializer/JsonConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/serializer/JsonConfigurationSerializer.java
new file mode 100644
index 0000000..0f7d84e
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/serializer/JsonConfigurationSerializer.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serializer;
+
+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.felix.cm.json.Configurations;
+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>
+ */
+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);
+        Configurations.buildWriter().build(writer).writeConfiguration(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/serializer/PropertiesConfigurationSerializer.java b/src/main/java/org/apache/sling/installer/core/impl/serializer/PropertiesConfigurationSerializer.java
new file mode 100644
index 0000000..dac5877
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/core/impl/serializer/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.serializer;
+
+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);
+        }
+    }
+
+}