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 c4df854  [cp2fm] input/output defined
c4df854 is described below

commit c4df854f4ca8feef690630515b0b3232fb31cb2c
Author: Simo Tripodi <[email protected]>
AuthorDate: Thu Feb 28 11:34:21 2019 +0100

    [cp2fm] input/output defined
---
 content-package-2-feature-model/pom.xml            | 50 +++++++++++++++-
 .../ContentPackage2FeatureModelConverter.java      | 69 +++++++++++++++++++++-
 ...ntentPackage2FeatureModelConverterLauncher.java | 12 ++--
 3 files changed, 120 insertions(+), 11 deletions(-)

diff --git a/content-package-2-feature-model/pom.xml 
b/content-package-2-feature-model/pom.xml
index 26299a8..b62381a 100644
--- a/content-package-2-feature-model/pom.xml
+++ b/content-package-2-feature-model/pom.xml
@@ -61,20 +61,66 @@
     </dependency>
 
     <!--
+     | Content-Package
+    -->
+    <dependency>
+      <groupId>org.apache.jackrabbit.vault</groupId>
+      <artifactId>org.apache.jackrabbit.vault</artifactId>
+      <version>3.2.6</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <!--
      | Sling Feature Model libraries
     -->
     <dependency>
       <groupId>org.apache.sling</groupId>
-      <artifactId>org.apache.sling.feature</artifactId>
+      <artifactId>org.apache.sling.feature.io</artifactId>
       <version>1.0.0</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.converter</artifactId>
+      <version>1.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.configurator</artifactId>
+      <version>1.0.4</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.utils</artifactId>
+      <version>1.11.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-json_1.0_spec</artifactId>
+      <version>1.0-alpha-1</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.sling</groupId>
-      <artifactId>org.apache.sling.feature.io</artifactId>
+      <artifactId>org.apache.sling.feature</artifactId>
       <version>1.0.0</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.annotation.versioning</artifactId>
+      <version>1.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>osgi.core</artifactId>
+      <version>6.0.0</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
 
   <build>
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 11ab246..c71abdd 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
@@ -16,10 +16,75 @@
  */
 package org.apache.sling.cp2fm;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.apache.jackrabbit.vault.packaging.PackageId;
+import org.apache.jackrabbit.vault.packaging.PackageManager;
+import org.apache.jackrabbit.vault.packaging.VaultPackage;
+import org.apache.jackrabbit.vault.packaging.impl.PackageManagerImpl;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONWriter;
+
 public final class ContentPackage2FeatureModelConverter {
 
-    public void convert() {
-        // TODO
+    private static final String JSON_FILE_EXTENSION = ".json";
+
+    private final PackageManager packageManager = new PackageManagerImpl();
+
+    private boolean strictValidation = false;
+
+    public ContentPackage2FeatureModelConverter setStrictValidation(boolean 
strictValidation) {
+        this.strictValidation = strictValidation;
+        return this;
+    }
+
+    public void convert(File contentPackage, File outputDirectory) throws 
IOException {
+        if (contentPackage == null) {
+            throw new IllegalArgumentException("Null content-package can not 
be converted.");
+        }
+        if (outputDirectory == null) {
+            throw new IllegalArgumentException("Null output directory not 
supported, it must be specified.");
+        }
+
+        if (!contentPackage.exists() || !contentPackage.isFile()) {
+            throw new IllegalArgumentException("Content-package "
+                                               + contentPackage
+                                               + " does not exist or it is not 
a valid file.");
+        }
+
+        if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
+            throw new IllegalStateException("output directory "
+                                            + outputDirectory
+                                            + " does not exist and can not be 
created, please make sure current user '"
+                                            + System.getProperty("")
+                                            + " has enough rights to write on 
the File System.");
+        }
+
+        VaultPackage vaultPackage = null;
+        Feature targetFeature = null;
+
+        try {
+            vaultPackage = packageManager.open(contentPackage, 
strictValidation);
+
+            PackageId packageId = vaultPackage.getId();
+            targetFeature = new Feature(new ArtifactId(packageId.getGroup(), 
+                                                       packageId.getName(),
+                                                       
packageId.getVersionString(),
+                                                       null,
+                                                       null));
+
+            File targetFile = new File(outputDirectory, packageId.getName() + 
JSON_FILE_EXTENSION);
+            try (FileWriter targetWriter = new FileWriter(targetFile)) {
+                FeatureJSONWriter.write(targetWriter, targetFeature);
+            }
+        } finally {
+            if (vaultPackage != null && !vaultPackage.isClosed()) {
+                vaultPackage.close();
+            }
+        }
     }
 
 }
diff --git 
a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverterLauncher.java
 
b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverterLauncher.java
index 20696ed..5eaecce 100644
--- 
a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverterLauncher.java
+++ 
b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverterLauncher.java
@@ -45,9 +45,12 @@ public final class 
ContentPackage2FeatureModelConverterLauncher implements Runna
     @Option(names = { "-v", "--version" }, description = "Display version 
information.")
     private boolean printVersion;
 
-    @Option(names = { "-cp", "--content-package" }, description = "The 
content-package input file.", required = true)
+    @Option(names = { "-c", "--content-package" }, description = "The 
content-package input file.", required = true)
     private File contentPackage;
 
+    @Option(names = { "-o", "--output-directory" }, description = "The output 
directory where the Feature File and the bundles will be deployed.", required = 
true)
+    private File outputDirectory;
+
     @Override
     public void run() {
         if (quiet) {
@@ -72,14 +75,9 @@ public final class 
ContentPackage2FeatureModelConverterLauncher implements Runna
         logger.info(appName);
         logger.info("");
 
-        if (!contentPackage.exists() || !contentPackage.isFile()) {
-            logger.error("Content package does not exist or is not a valid 
file");
-            System.exit(1);
-        }
-
         try {
             ContentPackage2FeatureModelConverter converter = new 
ContentPackage2FeatureModelConverter();
-            converter.convert();
+            converter.convert(contentPackage, outputDirectory);
 
             logger.info( 
"+-----------------------------------------------------+" );
             logger.info("{} SUCCESS", appName);

Reply via email to