schaefa commented on a change in pull request #41: SLING-8875 - Created new 
Goal to create an FM Descriptor from its POM
URL: 
https://github.com/apache/sling-slingfeature-maven-plugin/pull/41#discussion_r370865642
 
 

 ##########
 File path: 
src/main/java/org/apache/sling/feature/maven/mojos/manager/DefaultFeaturesManager.java
 ##########
 @@ -0,0 +1,294 @@
+/*
+ * 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.feature.maven.mojos.manager;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Artifacts;
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Extensions;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import static java.util.Objects.requireNonNull;
+
+public class DefaultFeaturesManager implements FeaturesManager {
+
+    public static final String ZIP_TYPE = "zip";
+
+    private static final String JAVA_IO_TMPDIR_PROPERTY = "java.io.tmpdir";
+
+    private static final String CONTENT_PACKAGES = "content-packages";
+
+    private static final String SLING_OSGI_FEATURE_TILE_TYPE = 
"slingosgifeature";
+
+    private static final String JSON_FILE_EXTENSION = ".json";
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    private final Map<String, Feature> runModes = new HashMap<>();
+
+    private final VariablesInterpolator interpolator = new 
SimpleVariablesInterpolator();
+
+    private final boolean mergeConfigurations;
+
+    private final int bundlesStartOrder;
+
+    private final File featureModelsOutputDirectory;
+
+    private final String artifactIdOverride;
+
+    private final String prefix;
+
+    private final Map<String, String> properties;
+
+    private final List<String> targetAPIRegions = new ArrayList<>();
+
+    private Feature targetFeature = null;
+
+    public DefaultFeaturesManager() {
+        this(true, 20, new File(System.getProperty(JAVA_IO_TMPDIR_PROPERTY)), 
null, null, null);
+    }
+
+    public DefaultFeaturesManager(boolean mergeConfigurations,
+                                  int bundlesStartOrder,
+                                  File featureModelsOutputDirectory,
+                                  String artifactIdOverride,
+                                  String prefix,
+                                  Map<String, String> properties) {
+        this.mergeConfigurations = mergeConfigurations;
+        this.bundlesStartOrder = bundlesStartOrder;
+        this.featureModelsOutputDirectory = featureModelsOutputDirectory;
+        this.artifactIdOverride = artifactIdOverride;
+        this.prefix = prefix;
+        this.properties = properties;
+    }
+
+    public void init(String groupId, String artifactId, String version) {
+        targetFeature = new Feature(new ArtifactId(groupId, artifactId, 
version, null, SLING_OSGI_FEATURE_TILE_TYPE));
+
+        initAPIRegions(targetFeature);
+
+        runModes.clear();
+    }
+
+    private void initAPIRegions(Feature feature) {
+        if (targetAPIRegions.size() > 0) {
+            Extension apiRegions = new Extension(ExtensionType.JSON, 
"api-regions", false);
+            StringBuilder jsonBuilder = new StringBuilder("[");
+            for (String apiRegion : targetAPIRegions) {
+                if (jsonBuilder.length() > 1) {
+                    jsonBuilder.append(',');
+                }
+                jsonBuilder.append("{\"name\":\"");
+                jsonBuilder.append(apiRegion);
+                jsonBuilder.append("\",\"exports\":[]}");
+            }
+            jsonBuilder.append("]");
+            apiRegions.setJSON(jsonBuilder.toString());
+            feature.getExtensions().add(apiRegions);
+        }
+    }
+
+    public Feature getTargetFeature() {
+        return targetFeature;
+    }
+
+    public Feature getRunMode(String runMode) {
+        if (getTargetFeature() == null) {
+            throw new IllegalStateException("Target Feature not initialized 
yet, please make sure convert() method was invoked first.");
+        }
+
+        if (runMode == null) {
+            return getTargetFeature();
+        }
+
+        ArtifactId newId = appendRunmode(getTargetFeature().getId(), runMode);
+
+        return runModes.computeIfAbsent(runMode, k -> {
+            Feature f = new Feature(newId);
+            initAPIRegions(f);
+            return f;
+        });
+    }
+
+    public void addArtifact(String runMode, ArtifactId id) {
+        addArtifact(runMode, id, null);
+    }
+
+    public void addArtifact(String runMode, ArtifactId id, Integer startOrder) 
{
+        requireNonNull(id, "Artifact can not be attached to a feature without 
specifying a valid ArtifactId.");
+
+        Artifact artifact = new Artifact(id);
+
+        Feature targetFeature = getRunMode(runMode);
+        Artifacts artifacts;
+
+        if (ZIP_TYPE.equals(id.getType()) ) {
+            Extensions extensions = targetFeature.getExtensions();
+            Extension extension = extensions.getByName(CONTENT_PACKAGES);
+
+            if (extension == null) {
+                extension = new Extension(ExtensionType.ARTIFACTS, 
CONTENT_PACKAGES, true);
+                extensions.add(extension);
+            }
+
+            artifacts = extension.getArtifacts();
+        } else {
+            int startOrderForBundle = startOrder != null ? 
startOrder.intValue() : bundlesStartOrder;
+            artifact.setStartOrder(startOrderForBundle);
+            artifacts = targetFeature.getBundles();
+        }
+
+        artifacts.add(artifact);
+    }
+
+    private ArtifactId appendRunmode(ArtifactId id, String runMode) {
+        ArtifactId newId;
+        if (runMode == null) {
+            newId = id;
+        } else {
+            final String classifier;
+            if (id.getClassifier() != null && !id.getClassifier().isEmpty()) {
+                classifier = id.getClassifier() + '-' + runMode;
+            } else {
+                classifier = runMode;
+            }
+
+            newId = new ArtifactId(id.getGroupId(), id.getArtifactId(), 
id.getVersion(), classifier, id.getType());
+        }
+        return newId;
+    }
+
+    public void addConfiguration(String runMode, String pid, 
Dictionary<String, Object> configurationProperties) {
 
 Review comment:
   Class removed

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to