You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/03/04 12:08:23 UTC

[sling-whiteboard] branch master updated: [cp2fm] added *.cfg, *.properties and *.xml OSGi configuration handler

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

simonetripodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new aab1cff  [cp2fm] added *.cfg, *.properties and *.xml OSGi configuration handler
aab1cff is described below

commit aab1cffb4b868ee0f0a238925425a5296833e41e
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Mon Mar 4 13:08:15 2019 +0100

    [cp2fm] added *.cfg, *.properties and *.xml OSGi configuration handler
---
 .../ContentPackage2FeatureModelConverter.java      |  7 +-
 .../AbstractConfigurationEntryHandler.java         | 74 +++++++++++++++++++
 .../sling/cp2fm/handlers/BundleEntryHandler.java   |  2 +-
 .../cp2fm/handlers/ConfigurationEntryHandler.java  | 31 +-------
 .../cp2fm/handlers/ContentPackageEntryHandler.java |  4 +-
 .../PropertiesConfigurationEntryHandler.java}      | 21 +++---
 .../handlers/XmlConfigurationEntryHandler.java     | 82 ++++++++++++++++++++++
 .../org/apache/sling/cp2fm/spi/EntryHandler.java   |  4 +-
 .../org.apache.sling.cp2fm.spi.EntryHandler        |  2 +
 9 files changed, 180 insertions(+), 47 deletions(-)

diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
index ec4354a..ee894eb 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
@@ -18,7 +18,6 @@ package org.apache.sling.cp2fm;
 
 import java.io.File;
 import java.io.FileWriter;
-import java.io.IOException;
 import java.util.Iterator;
 import java.util.ServiceLoader;
 
@@ -102,7 +101,7 @@ public final class ContentPackage2FeatureModelConverter {
         return targetFeature;
     }
 
-    public void convert() throws IOException {
+    public void convert() throws Exception {
         if (contentPackage == null) {
             throw new IllegalStateException("Null content-package can not be converted.");
         }
@@ -164,7 +163,7 @@ public final class ContentPackage2FeatureModelConverter {
         }
     }
 
-    public void process(Archive archive) throws IOException {
+    public void process(Archive archive) throws Exception {
         try {
             archive.open(strictValidation);
 
@@ -175,7 +174,7 @@ public final class ContentPackage2FeatureModelConverter {
         }
     }
 
-    private void traverse(Archive archive, Entry entry) throws IOException {
+    private void traverse(Archive archive, Entry entry) throws Exception {
         if (entry.isDirectory()) {
             for (Entry child : entry.getChildren()) {
                 traverse(archive, child);
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
new file mode 100644
index 0000000..f331fa2
--- /dev/null
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
@@ -0,0 +1,74 @@
+/*
+ * 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.cp2fm.handlers;
+
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.apache.jackrabbit.vault.fs.io.Archive;
+import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
+import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.Configuration;
+
+abstract class AbstractConfigurationEntryHandler extends AbstractRegexEntryHandler {
+
+    public AbstractConfigurationEntryHandler(String regex) {
+        super(regex);
+    }
+
+    @Override
+    public final void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception {
+        String name = entry.getName().substring(0, entry.getName().lastIndexOf('.'));
+
+        logger.info("Processing configuration '{}'.", name);
+
+        Dictionary<Object, Object> parsedConfiguration;
+
+        try (InputStream input = archive.openInputStream(entry)) {
+            parsedConfiguration = parseConfiguration(input);
+        }
+
+        if (!parsedConfiguration.isEmpty()) {
+            Configuration configuration = null;
+
+            dance : for (Configuration addedConfiguration : converter.getTargetFeature().getConfigurations()) {
+                if (name.equals(addedConfiguration.getPid())) {
+                    configuration = addedConfiguration;
+                    break dance;
+                }
+            }
+
+            if (configuration == null) {
+                configuration = new Configuration(name);
+                converter.getTargetFeature().getConfigurations().add(configuration);
+            }
+
+            Enumeration<Object> keys = parsedConfiguration.keys();
+            while (keys.hasMoreElements()) {
+                String key = keys.nextElement().toString();
+                Object value = parsedConfiguration.get(key);
+                configuration.getProperties().put(key, value);
+            }
+        } else {
+            logger.warn("{} configuration is empty, omitting it from the Feature File", name);
+        }
+    }
+
+    protected abstract Dictionary<Object, Object> parseConfiguration(InputStream input) throws Exception;
+
+}
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
index d8d6585..ac1a775 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
@@ -50,7 +50,7 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
     }
 
     @Override
-    public void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws IOException {
+    public void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception {
         logger.info("Processing bundle {}...", entry.getName());
 
         Properties properties = new Properties();
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandler.java
index 7582414..d4a90ef 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandler.java
@@ -16,18 +16,12 @@
  */
 package org.apache.sling.cp2fm.handlers;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.Dictionary;
-import java.util.Enumeration;
 
 import org.apache.felix.cm.file.ConfigurationHandler;
-import org.apache.jackrabbit.vault.fs.io.Archive;
-import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
-import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
-import org.apache.sling.feature.Configuration;
 
-public final class ConfigurationEntryHandler extends AbstractRegexEntryHandler {
+public final class ConfigurationEntryHandler extends AbstractConfigurationEntryHandler {
 
     public ConfigurationEntryHandler() {
         super(".+\\.config");
@@ -35,27 +29,8 @@ public final class ConfigurationEntryHandler extends AbstractRegexEntryHandler {
 
     @Override
     @SuppressWarnings("unchecked")
-    public void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws IOException {
-        String name = entry.getName().substring(0, entry.getName().lastIndexOf('.'));
-
-        logger.info("Processing configuration '{}'.", name);
-
-        Configuration configuration = new Configuration(name);
-
-        Dictionary<String, Object> parsedConfiguration;
-
-        try (InputStream input = archive.openInputStream(entry)) {
-            parsedConfiguration = ConfigurationHandler.read(input);
-        }
-
-        Enumeration<String> keys = parsedConfiguration.keys();
-        while (keys.hasMoreElements()) {
-            String key = keys.nextElement();
-            Object value = parsedConfiguration.get(key);
-            configuration.getProperties().put(key, value);
-        }
-
-        converter.getTargetFeature().getConfigurations().add(configuration);
+    protected Dictionary<Object, Object> parseConfiguration(InputStream input) throws Exception {
+        return ConfigurationHandler.read(input);
     }
 
 }
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ContentPackageEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ContentPackageEntryHandler.java
index 0280a67..12134c2 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ContentPackageEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/ContentPackageEntryHandler.java
@@ -16,8 +16,6 @@
  */
 package org.apache.sling.cp2fm.handlers;
 
-import java.io.IOException;
-
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.jackrabbit.vault.fs.io.ZipStreamArchive;
@@ -30,7 +28,7 @@ public final class ContentPackageEntryHandler extends AbstractRegexEntryHandler
     }
 
     @Override
-    public void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws IOException {
+    public void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception {
         logger.info("Processing sub-content package '{}'...", entry.getName());
 
         Archive subArchive = new ZipStreamArchive(archive.openInputStream(entry));
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/PropertiesConfigurationEntryHandler.java
similarity index 58%
copy from content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java
copy to content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/PropertiesConfigurationEntryHandler.java
index aa60ed4..d7f08e0 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/PropertiesConfigurationEntryHandler.java
@@ -14,18 +14,23 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package org.apache.sling.cp2fm.spi;
+package org.apache.sling.cp2fm.handlers;
 
-import java.io.IOException;
+import java.io.InputStream;
+import java.util.Dictionary;
 
-import org.apache.jackrabbit.vault.fs.io.Archive;
-import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
-import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.felix.utils.properties.ConfigurationHandler;
 
-public interface EntryHandler {
+public final class PropertiesConfigurationEntryHandler extends AbstractConfigurationEntryHandler {
 
-    boolean matches(String sourceSystemId);
+    public PropertiesConfigurationEntryHandler() {
+        super("[^/]+\\.(cfg|properties)");
+    }
 
-    void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws IOException;
+    @Override
+    @SuppressWarnings("unchecked")
+    protected Dictionary<Object, Object> parseConfiguration(InputStream input) throws Exception {
+        return ConfigurationHandler.read(input);
+    }
 
 }
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/XmlConfigurationEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/XmlConfigurationEntryHandler.java
new file mode 100644
index 0000000..1f75d03
--- /dev/null
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/XmlConfigurationEntryHandler.java
@@ -0,0 +1,82 @@
+/*
+ * 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.cp2fm.handlers;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public final class XmlConfigurationEntryHandler extends AbstractConfigurationEntryHandler {
+
+    private static final String JCR_ROOT = "jcr:root";
+
+    private static final String JCR_PREFIX = "jcr:";
+
+    private static final String XMLNS_PREFIX = "xmlns:";
+
+    private final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
+
+    public XmlConfigurationEntryHandler() {
+        super("[^.][^/]+\\.xml");
+    }
+
+    @Override
+    protected Dictionary<Object, Object> parseConfiguration(InputStream input) throws Exception {
+        SAXParser saxParser = saxParserFactory.newSAXParser();
+        JcrConfigurationHandler configurationHandler = new JcrConfigurationHandler();
+        saxParser.parse(input, configurationHandler);
+        return configurationHandler.getConfiguration();
+    }
+
+    private static final class JcrConfigurationHandler extends DefaultHandler {
+
+        private final Properties configuration = new Properties();
+
+        public Properties getConfiguration() {
+            return configuration;
+        }
+
+        @Override
+        public void startElement(String uri, String localName, String qName, Attributes attributes)
+                throws SAXException {
+            String primaryType = attributes.getValue(JCR_PRIMARYTYPE);
+
+            if (JCR_ROOT.equals(qName) && primaryType != null && !primaryType.isEmpty()) {
+                for (int i = 0; i < attributes.getLength(); i++) {
+                    String attributeQName = attributes.getQName(i);
+
+                    if (!attributeQName.startsWith(JCR_PREFIX) && !attributeQName.startsWith(XMLNS_PREFIX)) {
+                        String attributeVale = attributes.getValue(i);
+
+                        configuration.put(attributeQName, attributeVale);
+                    }
+                }
+            }
+        }
+
+    }
+
+}
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java
index aa60ed4..3132f8f 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/spi/EntryHandler.java
@@ -16,8 +16,6 @@
  */
 package org.apache.sling.cp2fm.spi;
 
-import java.io.IOException;
-
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
@@ -26,6 +24,6 @@ public interface EntryHandler {
 
     boolean matches(String sourceSystemId);
 
-    void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws IOException;
+    void handle(Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception;
 
 }
diff --git a/content-package-2-feature-model/src/main/resources/META-INF/services/org.apache.sling.cp2fm.spi.EntryHandler b/content-package-2-feature-model/src/main/resources/META-INF/services/org.apache.sling.cp2fm.spi.EntryHandler
index b441426..40956f0 100644
--- a/content-package-2-feature-model/src/main/resources/META-INF/services/org.apache.sling.cp2fm.spi.EntryHandler
+++ b/content-package-2-feature-model/src/main/resources/META-INF/services/org.apache.sling.cp2fm.spi.EntryHandler
@@ -1,3 +1,5 @@
 org.apache.sling.cp2fm.handlers.BundleEntryHandler
 org.apache.sling.cp2fm.handlers.ConfigurationEntryHandler
 org.apache.sling.cp2fm.handlers.ContentPackageEntryHandler
+org.apache.sling.cp2fm.handlers.PropertiesConfigurationEntryHandler
+org.apache.sling.cp2fm.handlers.XmlConfigurationEntryHandler