You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/05/27 12:20:51 UTC

[sling-org-apache-sling-installer-factory-feature] branch master updated: Refactor code by splitting up large method into smaller ones

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-factory-feature.git


The following commit(s) were added to refs/heads/master by this push:
     new 311c3cf  Refactor code by splitting up large method into smaller ones
311c3cf is described below

commit 311c3cf25d73f8e74a2149f4c6b99e569f456e60
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri May 27 14:20:46 2022 +0200

    Refactor code by splitting up large method into smaller ones
---
 .vscode/settings.json                              |   3 +
 .../feature/spi/context/ExtensionHandler.java      |   2 +
 .../spi/context/ExtensionHandlerContext.java       |  10 +-
 .../sling/feature/spi/context/package-info.java    |   3 +-
 .../model/impl/FeatureModelInstallerPlugin.java    | 103 +++++++++++++--------
 5 files changed, 76 insertions(+), 45 deletions(-)

diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..c5f3f6b
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "java.configuration.updateBuildConfiguration": "interactive"
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandler.java b/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandler.java
index da4ad62..4457a73 100644
--- a/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandler.java
+++ b/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandler.java
@@ -20,10 +20,12 @@ package org.apache.sling.feature.spi.context;
 
 import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Feature;
+import org.osgi.annotation.versioning.ConsumerType;
 
 /**
  * Service interface to handle feature model extensions in the feature installer
  */
+@ConsumerType
 public interface ExtensionHandler {
     /**
      * Called on registered services when an extension is encountered
diff --git a/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandlerContext.java b/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandlerContext.java
index 384279e..2137bbe 100644
--- a/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandlerContext.java
+++ b/src/main/java/org/apache/sling/feature/spi/context/ExtensionHandlerContext.java
@@ -20,6 +20,7 @@ package org.apache.sling.feature.spi.context;
 
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.builder.ArtifactProvider;
+import org.osgi.annotation.versioning.ProviderType;
 
 import java.net.URL;
 import java.util.Dictionary;
@@ -28,6 +29,7 @@ import java.util.Map;
 /**
  * This context is provided with calls to {@link ExtensionHandler} services.
  */
+@ProviderType
 public interface ExtensionHandlerContext {
     /**
      * Add a bundle to be installed by the launcher.
@@ -35,7 +37,7 @@ public interface ExtensionHandlerContext {
      * @param startLevel The start level for the bundle.
      * @param file The file with the bundle.
      */
-    public void addBundle(ArtifactId id, URL file, Integer startLevel);
+    void addBundle(ArtifactId id, URL file, Integer startLevel);
 
     /**
      * Add an artifact to be installed by the launcher
@@ -43,7 +45,7 @@ public interface ExtensionHandlerContext {
      * @param url The url to the Artifact resource
      * @param props Additional installation metadata
      */
-    public void addInstallableArtifact(ArtifactId id, final URL url, final Map<String,Object> props);
+    void addInstallableArtifact(ArtifactId id, final URL url, final Map<String,Object> props);
 
     /**
      * Add a configuration to be installed by the launcher
@@ -51,11 +53,11 @@ public interface ExtensionHandlerContext {
      * @param factoryPid The factory pid
      * @param properties The propertis
      */
-    public void addConfiguration(final String pid, final String factoryPid, final Dictionary<String, Object> properties);
+    void addConfiguration(final String pid, final String factoryPid, final Dictionary<String, Object> properties);
 
     /**
      * Obtain the artifact provider.
      * @return The artifact provider.
      */
-    public ArtifactProvider getArtifactProvider();
+    ArtifactProvider getArtifactProvider();
 }
diff --git a/src/main/java/org/apache/sling/feature/spi/context/package-info.java b/src/main/java/org/apache/sling/feature/spi/context/package-info.java
index 8d0d1e1..a888b72 100644
--- a/src/main/java/org/apache/sling/feature/spi/context/package-info.java
+++ b/src/main/java/org/apache/sling/feature/spi/context/package-info.java
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-@org.osgi.annotation.versioning.ProviderType
-@org.osgi.annotation.versioning.Version("1.0.0")
+@org.osgi.annotation.versioning.Version("1.0.1")
 package org.apache.sling.feature.spi.context;
 
diff --git a/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java b/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
index facc649..0493a94 100644
--- a/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
+++ b/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
@@ -144,17 +144,19 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
         }
     }
 
-    @Override
-    public TransformationResult[] transform(final RegisteredResource resource) {
+    /**
+     * Check if the resource is a feature file or feature archive
+     * @param resource The resource
+     * @return The list of features
+     */
+    private List<Feature> getFeatures(final RegisteredResource resource) {
         final List<Feature> features = new ArrayList<>();
-        boolean isFeatureArchive = true;
         if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(FILE_EXTENSION)) {
             try (final Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
                 features.add(FeatureJSONReader.read(reader, resource.getURL()));
             } catch (final IOException ioe) {
                 logger.info("Unable to read feature model from " + resource.getURL(), ioe);
             }
-            isFeatureArchive = false;
         } else if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".far")) {
             try (final InputStream is = resource.getInputStream()) {
                 features.addAll(ArchiveReader.read(is, null));
@@ -162,22 +164,15 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
                 logger.info("Unable to read feature model from " + resource.getURL(), ioe);
             }
         }
+        return features;
+    }
+
+    @Override
+    public TransformationResult[] transform(final RegisteredResource resource) {
+        final boolean isFeatureArchive = resource.getURL().endsWith(".far");
+        final List<Feature> features = getFeatures(resource);
         if (!features.isEmpty()) {
-            // persist all features to the file system
-            if (this.storageDirectory != null) {
-                for (Feature feature : features) {
-                    final File featureFile = new File(this.storageDirectory,
-                            feature.getId().toMvnPath().replace('/', File.separatorChar));
-                    if (!featureFile.exists()) {
-                        featureFile.getParentFile().mkdirs();
-                        try (final Writer writer = new FileWriter(featureFile)) {
-                            FeatureJSONWriter.write(writer, feature);
-                        } catch (final IOException ioe) {
-                            logger.error("Unable to write feature to " + featureFile + ":" + ioe.getMessage(), ioe);
-                        }
-                    }
-                }
-            }
+            this.persistFeatures(features);
 
             boolean error = false;
             final List<TransformationResult> result = new ArrayList<>();
@@ -186,26 +181,7 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
                     continue;
                 }
 
-                // assemble feature now
-                if (!feature.isAssembled()) {
-                    final BuilderContext ctx = new BuilderContext(this.artifactManager.toFeatureProvider());
-                    ctx.setArtifactProvider(this.artifactManager);
-
-                    // Set all merge extensions here from the service registry?
-
-                    feature = FeatureBuilder.assemble(feature, ctx);
-                }
-
-                FeatureBuilder.resolveVariables(feature, null);
-
-                String featureJson = null;
-                try (final StringWriter sw = new StringWriter()) {
-                    FeatureJSONWriter.write(sw, feature);
-                    featureJson = sw.toString();
-                } catch (final IOException ioe) {
-                    logger.info("Unable to read feature model from " + resource.getURL(), ioe);
-                }
-
+                final String featureJson = this.getFeatureJSON(feature);
                 if (featureJson != null) {
                     final TransformationResult tr = new TransformationResult();
                     tr.setResourceType(TYPE_FEATURE_MODEL);
@@ -263,6 +239,55 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
         return select;
     }
 
+    /**
+     * Persist all features in the file system (if storage dir is specified)
+     * @param features The list of features
+     */
+    private void persistFeatures(final List<Feature> features) {
+        if (this.storageDirectory != null) {
+            for (final Feature feature : features) {
+                final File featureFile = new File(this.storageDirectory, feature.getId().toMvnPath().replace('/', File.separatorChar));
+                if (!featureFile.exists()) {
+                    featureFile.getParentFile().mkdirs();
+                    try (final Writer writer = new FileWriter(featureFile)) {
+                        FeatureJSONWriter.write(writer, feature);
+                    } catch (final IOException ioe) {
+                        logger.error("Unable to write feature to " + featureFile + ":" + ioe.getMessage(), ioe);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Get the feature json.
+     * Assemble feature and resolve variables
+     * @param feature The feature
+     * @return The json string or {@code null}
+     */
+    private String getFeatureJSON(Feature feature) {
+        // assemble feature now
+        if (!feature.isAssembled()) {
+            final BuilderContext ctx = new BuilderContext(this.artifactManager.toFeatureProvider());
+            ctx.setArtifactProvider(this.artifactManager);
+
+            // Set all merge extensions here from the service registry?
+            feature = FeatureBuilder.assemble(feature, ctx);
+        }
+
+        FeatureBuilder.resolveVariables(feature, null);
+
+        String featureJson = null;
+        try (final StringWriter sw = new StringWriter()) {
+            FeatureJSONWriter.write(sw, feature);
+            featureJson = sw.toString();
+        } catch (final IOException ioe) {
+            logger.info("Unable to process feature model " + feature.getId().toMvnId(), ioe);
+        }
+
+        return featureJson;
+    }
+
     private static String toRegexPattern(String pattern) {
         StringBuilder stringBuilder = new StringBuilder("^");
         int index = 0;