You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gg...@apache.org on 2017/12/09 18:46:59 UTC

[karaf] 16/19: [KARAF-5376] Implement feature overriding and dependency flag overriding

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

ggrzybek pushed a commit to branch KARAF-5376-overrides_v2
in repository https://gitbox.apache.org/repos/asf/karaf.git

commit 889e8624d8a61994cc42979f631ac1bc484360dd
Author: Grzegorz Grzybek <gr...@gmail.com>
AuthorDate: Wed Dec 6 17:55:11 2017 +0100

    [KARAF-5376] Implement feature overriding and dependency flag overriding
---
 .../internal/service/FeaturesProcessorImpl.java    | 65 +++++++++++++++++++---
 .../internal/service/FeaturesProcessorTest.java    | 33 +++++++++++
 .../karaf/features/internal/service/fp04.xml       | 32 +++++++++++
 .../karaf/features/internal/service/fpi04.xml      | 31 +++++++++++
 .../karaf/features/internal/service/fpi05.xml      | 28 ++++++++++
 5 files changed, 180 insertions(+), 9 deletions(-)

diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesProcessorImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesProcessorImpl.java
index 87d26e6..6e8142a 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesProcessorImpl.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesProcessorImpl.java
@@ -28,13 +28,16 @@ import java.util.Properties;
 import java.util.Set;
 
 import org.apache.karaf.features.BundleInfo;
+import org.apache.karaf.features.FeaturePattern;
 import org.apache.karaf.features.LocationPattern;
 import org.apache.karaf.features.internal.model.Bundle;
 import org.apache.karaf.features.internal.model.Conditional;
 import org.apache.karaf.features.internal.model.Feature;
 import org.apache.karaf.features.internal.model.Features;
 import org.apache.karaf.features.internal.model.processing.BundleReplacements;
+import org.apache.karaf.features.internal.model.processing.FeatureReplacements;
 import org.apache.karaf.features.internal.model.processing.FeaturesProcessing;
+import org.apache.karaf.features.internal.model.processing.OverrideBundleDependency;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -134,23 +137,54 @@ public class FeaturesProcessorImpl implements FeaturesProcessor {
 
     @Override
     public void process(Features features) {
-        // blacklisting features
+        List<Feature> featureList = features.getFeature();
+        for (int i = 0; i < featureList.size(); i++) {
+            Feature f = featureList.get(i);
+            // overriding features first, so we can further override their bundles
+            for (FeatureReplacements.OverrideFeature override : getInstructions().getFeatureReplacements().getReplacements()) {
+                if (f.getId().equals(override.getFeature().getId())) {
+                    switch (override.getMode()) {
+                        case REPLACE:
+                            featureList.set(i, override.getFeature());
+                            break;
+                        case MERGE:
+                            f.getBundle().addAll(override.getFeature().getBundle());
+                            break;
+                        case REMOVE:
+                            // TODO
+                            break;
+                    }
+                }
+            }
+        }
+
         for (Feature feature : features.getFeature()) {
+            // blacklisting features
             boolean allBlacklisted = features.isBlacklisted();
             feature.setBlacklisted(allBlacklisted || isFeatureBlacklisted(feature));
-            // blacklisting bundles
-            processBundles(feature.getBundle(), allBlacklisted);
+
+            // override dependency flag (null - don't touch, false - change to false, true - change to true)
+            Boolean dependency = null;
+            for (OverrideBundleDependency.OverrideFeatureDependency overrideFeatureDependency : getInstructions().getOverrideBundleDependency().getFeatures()) {
+                FeaturePattern pattern = new FeaturePattern(overrideFeatureDependency.getName() + "/" + overrideFeatureDependency.getVersion());
+                if (pattern.matches(feature.getName(), feature.getVersion())) {
+                    dependency = overrideFeatureDependency.isDependency();
+                }
+            }
+
+            // blacklisting bundles and processing bundles
+            processBundles(feature.getBundle(), allBlacklisted, dependency);
             for (Conditional c : feature.getConditional()) {
-                processBundles(c.getBundle(), allBlacklisted);
+                processBundles(c.getBundle(), allBlacklisted, dependency);
             }
-        }
 
-        // TODO: changing "dependency" flag of features
-        // TODO: changing "dependency" flag of bundles
-        // TODO: overriding features
+            // TODO: think about overriding at repository level
+//            for (OverrideBundleDependency.OverrideDependency overrideDependency : getInstructions().getOverrideBundleDependency().getRepositories()) {
+//            }
+        }
     }
 
-    private void processBundles(List<Bundle> bundles, boolean allBlacklisted) {
+    private void processBundles(List<Bundle> bundles, boolean allBlacklisted, Boolean forceDependency) {
         for (Bundle bundle : bundles) {
             boolean bundleBlacklisted = allBlacklisted || isBundleBlacklisted(bundle.getLocation());
             if (bundleBlacklisted) {
@@ -159,6 +193,19 @@ public class FeaturesProcessorImpl implements FeaturesProcessor {
             } else {
                 // if not blacklisted, it may be overriden
                 staticOverrideBundle(bundle);
+                // and may have dependency flag altered
+                if (forceDependency != null) {
+                    // set at feature level
+                    bundle.setDependency(forceDependency);
+                } else {
+                    // may have dependency overriden at bundle level
+                    for (OverrideBundleDependency.OverrideDependency overrideBundleDependency : getInstructions().getOverrideBundleDependency().getBundles()) {
+                        LocationPattern pattern = new LocationPattern(overrideBundleDependency.getUri());
+                        if (pattern.matches(bundle.getLocation())) {
+                            bundle.setDependency(overrideBundleDependency.isDependency());
+                        }
+                    }
+                }
             }
         }
     }
diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesProcessorTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesProcessorTest.java
index 45955b2..9f2c27a 100644
--- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesProcessorTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesProcessorTest.java
@@ -204,6 +204,39 @@ public class FeaturesProcessorTest {
     }
 
     @Test
+    public void replaceFeatures() {
+        FeaturesProcessorImpl processor = new FeaturesProcessorImpl(new FeaturesServiceConfig(
+                null, null,
+                "file:src/test/resources/org/apache/karaf/features/internal/service/fpi04.xml", null));
+        URI uri = URI.create("file:src/test/resources/org/apache/karaf/features/internal/service/fp04.xml");
+        RepositoryImpl repo = (RepositoryImpl) new RepositoryCacheImpl(processor).create(uri, true);
+
+        Feature f11_0 = repo.getFeatures()[0];
+        Feature f11_1 = repo.getFeatures()[1];
+        assertThat(f11_0.getBundles().size(), equalTo(1));
+        assertThat(f11_0.getBundles().get(0).getLocation(), equalTo("mvn:commons-io/commons-io/1.4"));
+        assertThat(f11_1.getBundles().size(), equalTo(2));
+        assertThat(f11_1.getBundles().get(0).getLocation(), equalTo("mvn:commons-io/commons-io/1.3"));
+        assertThat(f11_1.getBundles().get(1).getLocation(), equalTo("mvn:commons-codec/commons-codec/0.5"));
+    }
+
+    @Test
+    public void overrideDependencyFlag() {
+        FeaturesProcessorImpl processor = new FeaturesProcessorImpl(new FeaturesServiceConfig(
+                null, null,
+                "file:src/test/resources/org/apache/karaf/features/internal/service/fpi05.xml", null));
+        URI uri = URI.create("file:src/test/resources/org/apache/karaf/features/internal/service/fp04.xml");
+        RepositoryImpl repo = (RepositoryImpl) new RepositoryCacheImpl(processor).create(uri, true);
+
+        Feature f11_0 = repo.getFeatures()[0];
+        Feature f11_1 = repo.getFeatures()[1];
+        assertTrue(f11_0.getBundles().get(0).isDependency());
+        assertTrue(f11_0.getBundles().get(1).isDependency());
+        assertFalse(f11_1.getBundles().get(0).isDependency());
+        assertFalse(f11_1.getBundles().get(1).isDependency());
+    }
+
+    @Test
     public void resolvePlaceholders() throws Exception {
         Properties props = new Properties();
         props.put("version.jclouds", "1.9");
diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/fp04.xml b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fp04.xml
new file mode 100644
index 0000000..ec545e8
--- /dev/null
+++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fp04.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<features name="fp04" xmlns="http://karaf.apache.org/xmlns/features/v1.5.0">
+
+    <feature name="f1" version="1.0">
+        <bundle dependency="false">mvn:commons-io/commons-io/1.2</bundle>
+        <bundle>mvn:commons-codec/commons-codec/0.4</bundle>
+    </feature>
+
+    <feature name="f1" version="1.1">
+        <bundle>mvn:commons-io/commons-io/1.3</bundle>
+        <bundle dependency="true">mvn:commons-codec/commons-codec/0.5</bundle>
+    </feature>
+
+</features>
diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi04.xml b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi04.xml
new file mode 100644
index 0000000..f0c7abe
--- /dev/null
+++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi04.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<featuresProcessing xmlns="http://karaf.apache.org/xmlns/features-processing/v1.0.0"
+        xmlns:f="http://karaf.apache.org/xmlns/features/v1.5.0">
+
+    <featureReplacements>
+        <replacement mode="replace">
+            <feature name="f1" version="1.0">
+                <f:bundle>mvn:commons-io/commons-io/1.4</f:bundle>
+            </feature>
+        </replacement>
+    </featureReplacements>
+
+</featuresProcessing>
diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi05.xml b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi05.xml
new file mode 100644
index 0000000..349bee2
--- /dev/null
+++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/fpi05.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<featuresProcessing xmlns="http://karaf.apache.org/xmlns/features-processing/v1.0.0">
+
+    <overrideBundleDependency>
+        <feature name="f1" version="[0.9,1.0)" dependency="false" />
+        <feature name="f1" version="1.0" dependency="true" />
+        <bundle uri="mvn:commons-codec/commons-codec/[0.5,*)" dependency="false" />
+    </overrideBundleDependency>
+
+</featuresProcessing>

-- 
To stop receiving notification emails like this one, please contact
"commits@karaf.apache.org" <co...@karaf.apache.org>.