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 2017/06/18 18:41:10 UTC

svn commit: r1799114 - in /sling/whiteboard/cziegeler: feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java

Author: cziegeler
Date: Sun Jun 18 18:41:10 2017
New Revision: 1799114

URL: http://svn.apache.org/viewvc?rev=1799114&view=rev
Log:
Add feature id handling

Modified:
    sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java
    sling/whiteboard/cziegeler/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java

Modified: sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java?rev=1799114&r1=1799113&r2=1799114&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java (original)
+++ sling/whiteboard/cziegeler/feature/src/main/java/org/apache/sling/feature/json/FeatureJSONReader.java Sun Jun 18 18:41:10 2017
@@ -49,8 +49,25 @@ public class FeatureJSONReader extends J
      */
     public static Feature read(final Reader reader, final String location)
     throws IOException {
+        return read(reader, null, location);
+    }
+
+    /**
+     * Read a new feature from the reader
+     * The reader is not closed. It is up to the caller to close the reader.
+     *
+     * @param reader The reader for the feature
+     * @param providedId Optional artifact id
+     * @param location Optional location
+     * @return The read feature
+     * @throws IOException If an IO errors occurs or the JSON is invalid.
+     */
+    public static Feature read(final Reader reader,
+            final ArtifactId providedId,
+            final String location)
+    throws IOException {
         try {
-            final FeatureJSONReader mr = new FeatureJSONReader(location);
+            final FeatureJSONReader mr = new FeatureJSONReader(providedId, location);
             return mr.readFeature(reader);
         } catch (final IllegalStateException | IllegalArgumentException e) {
             throw new IOException(e);
@@ -60,12 +77,17 @@ public class FeatureJSONReader extends J
     /** The read feature. */
     private Feature feature;
 
+    /** The provided id. */
+    private final ArtifactId providedId;
+
     /**
      * Private constructor
+     * @param pId Optional id
      * @param location Optional location
      */
-    private FeatureJSONReader(final String location) {
+    private FeatureJSONReader(final ArtifactId pId, final String location) {
         super(location);
+        this.providedId = pId;
     }
 
     /**
@@ -81,13 +103,18 @@ public class FeatureJSONReader extends J
         @SuppressWarnings("unchecked")
         final Map<String, Object> map = (Map<String, Object>) JSONUtil.getValue(json);
 
+        final ArtifactId fId;
         if ( !map.containsKey(JSONConstants.FEATURE_ID) ) {
-            throw new IOException(this.exceptionPrefix + "Feature id is missing");
+            if ( this.providedId == null ) {
+                throw new IOException(this.exceptionPrefix + "Feature id is missing");
+            }
+            fId = this.providedId;
+        } else {
+            final Object idObj = map.get(JSONConstants.FEATURE_ID);
+            checkType(JSONConstants.FEATURE_ID, idObj, String.class);
+            fId = ArtifactId.fromMvnId(idObj.toString());
         }
-        final Object idObj = map.get(JSONConstants.FEATURE_ID);
-        checkType(JSONConstants.FEATURE_ID, idObj, String.class);
-
-        this.feature = new Feature(ArtifactId.fromMvnId(idObj.toString()));
+        this.feature = new Feature(fId);
         this.feature.setLocation(this.location);
 
         this.readBundles(map, feature.getBundles(), feature.getConfigurations());

Modified: sling/whiteboard/cziegeler/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/cziegeler/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java?rev=1799114&r1=1799113&r2=1799114&view=diff
==============================================================================
--- sling/whiteboard/cziegeler/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java (original)
+++ sling/whiteboard/cziegeler/osgifeature-maven-plugin/src/main/java/org/apache/sling/feature/maven/Preprocessor.java Sun Jun 18 18:41:10 2017
@@ -47,8 +47,14 @@ public class Preprocessor {
     public void process(final Environment env) {
         for(final ProjectInfo info : env.modelProjects.values()) {
             if ( info instanceof FeatureProjectInfo ) {
-                process(env, (FeatureProjectInfo)info, FeatureProjectConfig.getMainConfig((FeatureProjectInfo)info));
-                process(env, (FeatureProjectInfo)info, FeatureProjectConfig.getTestConfig((FeatureProjectInfo)info));
+                final FeatureProjectInfo finfo = (FeatureProjectInfo)info;
+                process(env, finfo, FeatureProjectConfig.getMainConfig(finfo));
+                process(env, finfo, FeatureProjectConfig.getTestConfig(finfo));
+                if ( FeatureConstants.PACKAGING_FEATURE.equals(info.project.getPackaging()) && finfo.feature == null ) {
+                    throw new RuntimeException("Feature project has no feature defined: " + info.project.getId());
+                }
+
+                ProjectHelper.storeProjectInfo(finfo);
             }
         }
     }
@@ -64,23 +70,19 @@ public class Preprocessor {
             final FeatureProjectInfo info,
             final FeatureProjectConfig config) {
         if ( info.done == true ) {
-            env.logger.debug("Return assembled feature for " + info.project);
+            env.logger.debug("Return assembled feature for " + info.project.getId());
             return;
         }
         // prevent recursion and multiple processing
         info.done = true;
-        env.logger.debug("Processing " + config.getName() + " in project " + info.project);
+        env.logger.debug("Processing " + config.getName() + " in project " + info.project.getId());
 
-        // read project model, either inlined or as file
-        // check if file exists
-        final File featureFile = new File(info.project.getBasedir(), config.getFeatureFileName());
-        env.logger.debug("Checking feature file " + config.getFeatureFileName() + " : " + featureFile.exists());
-        env.logger.debug("Inlined feature : " + (config.getInlinedFeature() != null));
-
-        if ( config.getInlinedFeature() != null && featureFile.exists() ) {
-            throw new RuntimeException("Only one (feature file or inlined feature) can be specified - but not both");
+        // read project feature, either inlined or from file
+        final Feature feature = readProjectFeature(env.logger, info.project, config);
+        if ( feature == null ) {
+            env.logger.debug("No " + config.getName() + " found in project " + info.project.getId());
+            return;
         }
-        final Feature feature = readLocalFeature(info.project, config.getInlinedFeature(), featureFile, env.logger);
         if ( config.isTestConfig() ) {
             info.testFeature = feature;
         } else {
@@ -162,8 +164,6 @@ public class Preprocessor {
         } else {
             addDependenciesFromFeature(env, info, assembledFeature, config.getScope());
         }
-
-        ProjectHelper.storeProjectInfo(info);
     }
 
     /**
@@ -213,36 +213,78 @@ public class Preprocessor {
     }
 
     /**
-     * Read feature file
+     * Read the feature for a feature project.
+     * The feature is either inlined in the pom or stored in a file in the project.
      *
-     * @param project The current maven project
-     * @param inlinedModel the inlined feature (may be null)
-     * @param featureFileName The file name if the feature
      * @param logger The logger
+     * @param project The current maven project
+     * @param config The configuration
+     * @return The feature or {@code null}
      */
-    protected Feature readLocalFeature(
+    protected Feature readProjectFeature(
+            final Logger logger,
             final MavenProject project,
-            final String inlinedModel,
-            final File featureFile,
-            final Logger logger) {
-        if ( inlinedModel != null ) {
+            final FeatureProjectConfig config) {
+        final File featureFile = new File(project.getBasedir(), config.getFeatureFileName());
+        logger.debug("Checking feature file " + config.getFeatureFileName() + " : " + featureFile.exists());
+        logger.debug("Inlined feature : " + (config.getInlinedFeature() != null));
+
+        if ( config.getInlinedFeature() != null && featureFile.exists() ) {
+            throw new RuntimeException("Only one (feature file or inlined feature) can be specified - but not both");
+        }
+
+        final String classifier;
+        if ( config.isTestConfig() ) {
+            classifier = FeatureConstants.CLASSIFIER_TEST_FEATURE;
+        } else if ( FeatureConstants.PACKAGING_FEATURE.equals(project.getPackaging()) ) {
+            classifier = null;
+        } else {
+            classifier = FeatureConstants.CLASSIFIER_FEATURE;
+        }
+        final ArtifactId id = new ArtifactId(project.getGroupId(),
+                project.getArtifactId(),
+                project.getVersion(),
+                classifier,
+                FeatureConstants.PACKAGING_FEATURE);
+
+        final Feature feature;
+        if ( config.getInlinedFeature() != null ) {
             logger.debug("Reading inlined model from project " + project.getId());
             try {
-                try (final Reader reader = new StringReader(inlinedModel)) {
-                    return postProcessReadFeature(FeatureJSONReader.read(reader, null));
+                try (final Reader reader = new StringReader(config.getInlinedFeature())) {
+                    feature = FeatureJSONReader.read(reader, id, null);
                 }
             } catch ( final IOException io) {
                 throw new RuntimeException("Unable to read inlined feature", io);
             }
-        }
-        logger.debug("Reading feature " + featureFile + " in project " + project.getId());
-        try {
-            try (final FileReader reader = new FileReader(featureFile)) {
-                return postProcessReadFeature(FeatureJSONReader.read(reader, featureFile.getAbsolutePath()));
+        } else {
+            if ( !featureFile.exists() ) {
+                logger.debug("Feature file " + featureFile + " in project " + project.getId() + " does not exist.");
+                return null;
             }
-        } catch ( final IOException io) {
-            throw new RuntimeException("Unable to read feature " + featureFile, io);
+            logger.debug("Reading feature " + featureFile + " in project " + project.getId());
+            try {
+                try (final FileReader reader = new FileReader(featureFile)) {
+                    feature = FeatureJSONReader.read(reader, id, featureFile.getAbsolutePath());
+                }
+            } catch ( final IOException io) {
+                throw new RuntimeException("Unable to read feature " + featureFile, io);
+            }
+        }
+
+        // check feature id
+        if ( !id.getGroupId().equals(feature.getId().getGroupId()) ) {
+            throw new RuntimeException("Wrong group id for feature. It should be " + id.getGroupId() + " but is " + feature.getId().getGroupId());
         }
+        if ( !id.getArtifactId().equals(feature.getId().getArtifactId()) ) {
+            throw new RuntimeException("Wrong artifact id for feature. It should be " + id.getArtifactId() + " but is " + feature.getId().getArtifactId());
+        }
+        if ( !id.getVersion().equals(feature.getId().getVersion()) ) {
+            throw new RuntimeException("Wrong version for feature. It should be " + id.getVersion() + " but is " + feature.getId().getVersion());
+        }
+
+        // post process and retutrn
+        return postProcessReadFeature(feature);
     }
 
     /**