FELIX-4412 wire only to bundles that don't require an extender or are wired to us

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1667030 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/bnd.bnd b/scr/bnd.bnd
index 0a175a2..f6225aa 100644
--- a/scr/bnd.bnd
+++ b/scr/bnd.bnd
@@ -41,6 +41,7 @@
  org.osgi.framework;version="[1.6,2)", \
  org.osgi.service.log;version="[1.3,2)";resolution:=optional, \
  org.osgi.service.packageadmin;version="[1.2,2)";resolution:=optional, \
+ org.osgi.framework.wiring;version="[1.0,2)", \
  *
 
 DynamicImport-Package: \
diff --git a/scr/pom.xml b/scr/pom.xml
index c69a658..e78455b 100644
--- a/scr/pom.xml
+++ b/scr/pom.xml
@@ -398,6 +398,17 @@
                 <dependency>
                     <groupId>org.apache.felix</groupId>
                     <artifactId>org.apache.felix.framework</artifactId>
+                    <version>4.6.0</version>
+                    <scope>test</scope>
+                </dependency>
+            </dependencies>
+        </profile>
+        <profile>
+            <id>felix-R5</id>
+            <dependencies>
+                <dependency>
+                    <groupId>org.apache.felix</groupId>
+                    <artifactId>org.apache.felix.framework</artifactId>
                     <version>4.0.3</version>
                     <scope>test</scope>
                 </dependency>
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
index d134db9..27b9874 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
@@ -22,6 +22,7 @@
 import java.io.PrintStream;
 import java.text.MessageFormat;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -34,6 +35,10 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.namespace.extender.ExtenderNamespace;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.runtime.ServiceComponentRuntime;
 import org.osgi.service.log.LogService;
@@ -280,7 +285,7 @@
      */
     private void loadComponents( Bundle bundle )
     {
-        if ( bundle.getHeaders().get( "Service-Component" ) == null )
+        if ( bundle.getHeaders().get( ComponentConstants.SERVICE_COMPONENT ) == null )
         {
             // no components in the bundle, abandon
             return;
@@ -294,6 +299,31 @@
                 new Object[] {bundle.getSymbolicName(), bundle.getBundleId()}, null );
             return;
         }
+        
+        //Examine bundle for extender requirement; if present check if bundle is wired to us.
+        BundleWiring wiring = bundle.adapt(BundleWiring.class);
+        List<BundleWire> extenderWires = wiring.getRequiredWires(ExtenderNamespace.EXTENDER_NAMESPACE);
+        try 
+        {
+            for (BundleWire wire: extenderWires) 
+            {
+                if (ComponentConstants.COMPONENT_CAPABILITY_NAME.equals(wire.getCapability().getAttributes().get(ExtenderNamespace.EXTENDER_NAMESPACE)))
+                {
+                    if (!m_bundle.adapt(BundleRevision.class).equals(wire.getProvider()))
+                    {
+                        log( LogService.LOG_DEBUG, m_bundle, "Bundle {0}/{1} wired to a different extender: {2}",
+                            new Object[] {bundle.getSymbolicName(), bundle.getBundleId(), wire.getProvider().getSymbolicName()}, null );
+                        return;
+                    }
+                    break;
+                }
+            }
+        } 
+        catch (NoSuchMethodError e) 
+        {
+            log( LogService.LOG_DEBUG, m_bundle, "Cannot determine bundle wiring on pre R6 framework",
+                null, null );
+        }
 
         // FELIX-1666 method is called for the LAZY_ACTIVATION event and
         // the started event. Both events cause this method to be called;
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
index 9ea45fd..d6f60bf 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
@@ -76,6 +76,7 @@
 import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.namespace.extender.ExtenderNamespace;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.ComponentFactory;
@@ -686,6 +687,7 @@
                 .set(Constants.BUNDLE_VERSION, version)
                 .set(Constants.IMPORT_PACKAGE, componentPackage)
                 .set("Service-Component", "OSGI-INF/components.xml")
+                .set(Constants.REQUIRE_CAPABILITY, ExtenderNamespace.EXTENDER_NAMESPACE + ";filter:=\"(&(osgi.extender=osgi.component)(version>=1.3)(!(version>=2.0)))\"")
                 .build(withBnd());
 
         try
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ExtenderTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ExtenderTest.java
new file mode 100644
index 0000000..f35ff0b
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ExtenderTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.felix.scr.integration;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.namespace.extender.ExtenderNamespace;
+import org.osgi.service.component.ComponentConstants;
+
+@RunWith(JUnit4TestRunner.class)
+public class ExtenderTest extends ComponentTestBase
+{
+    static
+    {
+        // uncomment to enable debugging of this test class
+//          paxRunnerVmOption = DEBUG_VM_OPTION;
+    }
+    
+    @Test
+    public void testWired() throws BundleException 
+    {
+        if (isAtLeastR5())
+        {
+            BundleWiring scrWiring = bundle.adapt(BundleWiring.class);
+            List<BundleWire> extenderWires = scrWiring.getRequiredWires(ExtenderNamespace.EXTENDER_NAMESPACE);
+            boolean wired = false;
+            for (BundleWire wire: extenderWires) 
+            {
+                if (ComponentConstants.COMPONENT_CAPABILITY_NAME.equals(wire.getCapability().getAttributes().get(ExtenderNamespace.EXTENDER_NAMESPACE)))
+                {
+                    Assert.assertEquals("Not wired to us", "org.apache.felix.scr", wire.getProviderWiring().getBundle().getSymbolicName());
+                    wired = true;
+                    break;
+                }
+            }
+            Assert.assertTrue("should be wired to us", wired);
+        }
+    }
+
+}