You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2009/03/16 14:49:15 UTC

svn commit: r754890 - in /servicemix/maven-plugins/features-maven-plugin/trunk/src: main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java

Author: gertv
Date: Mon Mar 16 13:49:14 2009
New Revision: 754890

URL: http://svn.apache.org/viewvc?rev=754890&view=rev
Log:
SMX4-249: Avoid overriding well-known bundles with auto-discovered ones

Added:
    servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java   (with props)
Modified:
    servicemix/maven-plugins/features-maven-plugin/trunk/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java

Modified: servicemix/maven-plugins/features-maven-plugin/trunk/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/features-maven-plugin/trunk/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java?rev=754890&r1=754889&r2=754890&view=diff
==============================================================================
--- servicemix/maven-plugins/features-maven-plugin/trunk/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java (original)
+++ servicemix/maven-plugins/features-maven-plugin/trunk/src/main/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojo.java Mon Mar 16 13:49:14 2009
@@ -90,7 +90,7 @@
      * @parameter
      */
     private String kernelVersion;
-
+    
     /*
      * A list of packages exported by the kernel
      */
@@ -104,6 +104,11 @@
     private File bundles;
 
     /*
+     * A set of known bundles
+     */
+    private Set<String> knownBundles = new HashSet<String>();
+    
+    /*
      * A list of exports by the bundles
      */
     private Map<String, Map<VersionRange, Artifact>> bundleExports = new HashMap<String, Map<VersionRange, Artifact>>();
@@ -173,6 +178,7 @@
                     getLog().debug(" adding kernel export " + entry.getName() + " (" + entry.getVersion() + ")");
                 }
             }
+            registerBundle(artifact);
         }
         getLog().info("...done!");
     }
@@ -242,7 +248,7 @@
     private void discoverBundles(Artifact artifact) throws ArtifactResolutionException, ArtifactNotFoundException, ZipException, IOException {
         List<Artifact> dependencies = getDependencies(artifact);
         for (Artifact dependency : dependencies) {
-            if (isBundle(dependency) && !isFeature(dependency)) {
+            if (isDiscoverableBundle(dependency)) {
                 getLog().info("  Automatically discovered bundle " + dependency);
                 registerBundle(dependency);
             }
@@ -250,10 +256,31 @@
     }
 
     /*
+     * Only auto-discover an OSGi bundle
+     * - if it is not already known as a feature itself
+     * - if it is not another version of an already known bundle
+     */
+    private boolean isDiscoverableBundle(Artifact artifact) {
+        if (isBundle(artifact) && !isFeature(artifact)) {
+            for (String known : knownBundles) {
+                String[] elements = known.split("/");
+                if (artifact.getGroupId().equals(elements[0]) &&
+                    artifact.getArtifactId().equals(elements[1])) {
+                    getLog().debug(String.format("  Avoid auto-discovery for %s because of existing bundle %s", 
+                                                 toString(artifact), known));
+                    return false;
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
+    /*
      * Check if the given artifact is a bundle
      */
     private boolean isBundle(Artifact artifact) {
-        if (artifact.getArtifactHandler().getPackaging().equals("bundle")) {
+        if (knownBundles.contains(toString(artifact)) || artifact.getArtifactHandler().getPackaging().equals("bundle")) {
             return true;
         } else {
             try {
@@ -333,6 +360,7 @@
     private void registerBundle(Artifact artifact) throws ArtifactResolutionException, ArtifactNotFoundException, ZipException,
         IOException {
         getLog().debug("Registering bundle " + artifact);
+        knownBundles.add(toString(artifact));
         Manifest manifest = getManifest(artifact);
         for (ManifestEntry entry : getManifestEntries(manifest.getExports())) {
             Map<VersionRange, Artifact> versions = bundleExports.get(entry.getName());
@@ -422,7 +450,10 @@
         }
         return list;
     }
-
+    
+    public static String toString(Artifact artifact) {
+        return String.format("%s/%s/%s", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
+    }
 
     private class Feature {
 
@@ -466,7 +497,7 @@
                         artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion()));
 
             	} else {
-            		getLog().info(String.format("bundle %s/%s/%s is already included in inner feature, so don't count it in again", 
+            		getLog().debug(String.format("    bundle %s/%s/%s is already included in inner feature, so don't count it in again", 
                             artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion()));
             	}
             }

Added: servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java?rev=754890&view=auto
==============================================================================
--- servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java (added)
+++ servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java Mon Mar 16 13:49:14 2009
@@ -0,0 +1,48 @@
+/**
+ * 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.servicemix.tooling.features;
+
+import org.apache.maven.artifact.Artifact;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+
+import junit.framework.TestCase;
+
+/**
+ * Test cases for {@link GenerateFeaturesXmlMojo}
+ */
+public class GenerateFeaturesXmlMojoTest extends TestCase {
+    
+    private Mockery mockery;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mockery = new Mockery();
+    }
+    
+    public void testToString() throws Exception {
+        final Artifact artifact = mockery.mock(Artifact.class);
+        mockery.checking(new Expectations() {{
+            allowing(artifact).getGroupId(); will(returnValue("org.apache.servicemix.test"));
+            allowing(artifact).getArtifactId(); will(returnValue("test-artifact"));
+            allowing(artifact).getVersion(); will(returnValue("1.2.3"));
+        }});
+        assertEquals("org.apache.servicemix.test/test-artifact/1.2.3", GenerateFeaturesXmlMojo.toString(artifact));
+    }
+
+}

Propchange: servicemix/maven-plugins/features-maven-plugin/trunk/src/test/java/org/apache/servicemix/tooling/features/GenerateFeaturesXmlMojoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native