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 2020/01/04 17:52:44 UTC

[sling-org-apache-sling-installer-provider-file] 01/01: SLING-8419 write back configuration in OSGi JSON format

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-provider-file.git

commit 13588e833d90e6cb82c4293d78d4037235a294b4
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sat Jan 4 18:52:28 2020 +0100

    SLING-8419 write back configuration in OSGi JSON format
---
 pom.xml                                            | 19 +-------------
 .../provider/file/impl/FileInstaller.java          | 29 +++++++++++-----------
 2 files changed, 15 insertions(+), 33 deletions(-)

diff --git a/pom.xml b/pom.xml
index 25cd3c7..610f61e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -54,19 +54,9 @@
                         <Bundle-Activator>
                             org.apache.sling.installer.provider.file.impl.Activator
                         </Bundle-Activator>
-                        <!-- 
-                             We need at least 3.1.2 as this allows reading comments from a config
-                         -->
-                        <Import-Package>
-                            org.apache.sling.installer.api;version="[3.1.2,4)",
-                            *
-                        </Import-Package>
                         <Private-Package>
                             org.apache.sling.installer.provider.file.impl.*
                         </Private-Package>
-                        <Embed-Dependency>
-                            org.apache.felix.configadmin;inline="org/apache/felix/cm/file/ConfigurationHandler.*"
-                        </Embed-Dependency>
                     </instructions>
                 </configuration>
             </plugin>
@@ -85,7 +75,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.installer.core</artifactId>
-            <version>3.5.4</version>
+            <version>3.9.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
@@ -94,12 +84,5 @@
             <version>1.1.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.10</version>
-            <scope>provided</scope>
-        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java b/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
index a646f61..af9933c 100644
--- a/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
+++ b/src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java
@@ -18,22 +18,26 @@
  */
 package org.apache.sling.installer.provider.file.impl;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.felix.cm.file.ConfigurationHandler;
 import org.apache.sling.installer.api.InstallableResource;
 import org.apache.sling.installer.api.OsgiInstaller;
 import org.apache.sling.installer.api.UpdateHandler;
 import org.apache.sling.installer.api.UpdateResult;
+import org.apache.sling.installer.api.serializer.ConfigurationSerializer;
+import org.apache.sling.installer.api.serializer.ConfigurationSerializerFactory;
 import org.apache.sling.settings.SlingSettingsService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -172,7 +176,7 @@ public class FileInstaller
                 final String oldPath = url.substring(pos + 1);
                 prefix = url.substring(0, pos);
                 // ensure extension 'config'
-                if ( !oldPath.endsWith(".config") ) {
+                if ( !oldPath.endsWith(".cfg.json") ) {
                     final File file = new File(oldPath);
                     if ( file.exists() ) {
                         file.delete();
@@ -180,9 +184,9 @@ public class FileInstaller
                     final int lastDot = oldPath.lastIndexOf('.');
                     final int lastSlash = oldPath.lastIndexOf('/');
                     if ( lastDot <= lastSlash ) {
-                        path = oldPath + ".config";
+                        path = oldPath + ".cfg.jsono";
                     } else {
-                        path = oldPath.substring(0, lastDot) + ".config";
+                        path = oldPath.substring(0, lastDot) + ".cfg.json";
                     }
                 } else {
                     path = oldPath;
@@ -191,23 +195,18 @@ public class FileInstaller
             } else {
                 // add
                 final FileMonitor first = this.monitors.get(0);
-                path = first.getRoot().getAbsolutePath() + '/' + id + ".config";
+                path = first.getRoot().getAbsolutePath() + '/' + id + ".cfg.json";
                 prefix = first.getListener().getScheme();
                 logger.debug("Add of {} at {}", resourceType, path);
             }
 
             final File file = new File(path);
             file.getParentFile().mkdirs();
-            final FileOutputStream fos = new FileOutputStream(file);
-            try {
-                fos.write("# Configuration created by Apache Sling File Installer\n".getBytes("UTF-8"));
-                ConfigurationHandler.write(fos, dict);
-            } finally {
-                try {
-                    fos.close();
-                } catch (final IOException ignore) {}
-            }
-
+            try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) {
+                fos.write("# Configuration created by Apache Sling File Installer\n".getBytes(StandardCharsets.UTF_8));
+                ConfigurationSerializer serializer = ConfigurationSerializerFactory.create(ConfigurationSerializerFactory.Format.JSON);
+                serializer.serialize(dict, fos);
+            } 
             final UpdateResult result = new UpdateResult(prefix + ':' + path);
             result.setResourceIsMoved(true);
             return result;