You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by tj...@apache.org on 2016/10/11 13:41:34 UTC

svn commit: r1764242 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/integration/

Author: tjwatson
Date: Tue Oct 11 13:41:34 2016
New Revision: 1764242

URL: http://svn.apache.org/viewvc?rev=1764242&view=rev
Log:
FELIX-5356 - fix for Component Factory and CM factory Configurations behave badly

 Also added tests for factory configuration

Added:
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/Felix5356Test.java
Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ConfigurableComponentHolder.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/manager/ConfiguredComponentHolderTest.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java?rev=1764242&r1=1764241&r2=1764242&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java Tue Oct 11 13:41:34 2016
@@ -46,6 +46,7 @@ import org.apache.felix.scr.impl.helper.
 import org.apache.felix.scr.impl.metadata.ComponentMetadata;
 import org.apache.felix.scr.impl.metadata.ReferenceMetadata;
 import org.apache.felix.scr.impl.metadata.ServiceMetadata;
+import org.apache.felix.scr.impl.metadata.TargetedPID;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceException;
@@ -1414,7 +1415,7 @@ public abstract class AbstractComponentM
 
     abstract void preDeregister();
 
-    public abstract void reconfigure(Map<String, Object> configuration, boolean configurationDeleted);
+    public abstract void reconfigure(Map<String, Object> configuration, boolean configurationDeleted, TargetedPID factoryPid);
 
     public abstract void getComponentManagers(List<AbstractComponentManager<S>> cms);
 

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java?rev=1764242&r1=1764241&r2=1764242&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java Tue Oct 11 13:41:34 2016
@@ -123,7 +123,7 @@ public class ComponentFactoryImpl<S> ext
 
         cm.setFactoryProperties( dictionary );
         //configure the properties
-        cm.reconfigure( m_configuration, false );
+        cm.reconfigure( m_configuration, false, null );
         // enable
         cm.enableInternal();
 
@@ -397,7 +397,11 @@ public class ComponentFactoryImpl<S> ext
 
 
 	@Override
-	public void reconfigure(Map<String, Object> configuration, boolean configurationDeleted) {
+	public void reconfigure(Map<String, Object> configuration, boolean configurationDeleted, TargetedPID factoryPid) {
+	    if ( factoryPid != null ) {
+	        // ignore factory configuration changes for component factories.
+	        return;
+	    }
 		m_configuration = configuration;
 		List<SingleComponentManager<S>> cms;
 		synchronized (m_componentInstances)
@@ -406,7 +410,7 @@ public class ComponentFactoryImpl<S> ext
         }
 		for (SingleComponentManager<S> cm: cms)
 		{
-		    cm.reconfigure( configuration, configurationDeleted);
+		    cm.reconfigure( configuration, configurationDeleted, factoryPid);
 		}
 	}
 

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ConfigurableComponentHolder.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ConfigurableComponentHolder.java?rev=1764242&r1=1764241&r2=1764242&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ConfigurableComponentHolder.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ConfigurableComponentHolder.java Tue Oct 11 13:41:34 2016
@@ -320,7 +320,7 @@ public abstract class ConfigurableCompon
         for ( Map.Entry<AbstractComponentManager<S>,Map<String, Object>> entry: scms.entrySet())
         {
             if ( reconfigure ) {
-                entry.getKey().reconfigure( entry.getValue(), true);
+                entry.getKey().reconfigure( entry.getValue(), true, factoryPid);
             } else {
                 entry.getKey().dispose(ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_DELETED);
             }
@@ -423,7 +423,7 @@ public abstract class ConfigurableCompon
         for ( Map.Entry<AbstractComponentManager<S>,Map<String, Object>> entry: scms.entrySet())
         {
             // configure the component
-            entry.getKey().reconfigure(entry.getValue(), false);
+            entry.getKey().reconfigure(entry.getValue(), false, factoryPid);
             log(LogService.LOG_DEBUG,
                 "ImmediateComponentHolder Finished configuring the dependency managers for component for pid {0} ",
                 new Object[] { pid }, null);
@@ -637,14 +637,14 @@ public abstract class ConfigurableCompon
                     {
                         m_singleComponent = createComponentManager(false);
                         cms.add( m_singleComponent );
-                        m_singleComponent.reconfigure(mergeProperties( null ), false);
+                        m_singleComponent.reconfigure(mergeProperties( null ), false, null);
                     }
                     if ( m_factoryPidIndex != null)
                     {
                         for (String pid: m_factoryConfigurations.keySet()) {
                             AbstractComponentManager<S> scm = createComponentManager(true);
                             m_components.put(pid, scm);
-                            scm.reconfigure( mergeProperties( pid ), false);
+                            scm.reconfigure( mergeProperties( pid ), false, new TargetedPID(pid));
                             cms.add( scm );
                         }
                     }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java?rev=1764242&r1=1764241&r2=1764242&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/SingleComponentManager.java Tue Oct 11 13:41:34 2016
@@ -30,6 +30,7 @@ import java.util.concurrent.atomic.Atomi
 import org.apache.felix.scr.impl.helper.ComponentMethod;
 import org.apache.felix.scr.impl.helper.ComponentMethods;
 import org.apache.felix.scr.impl.helper.MethodResult;
+import org.apache.felix.scr.impl.metadata.TargetedPID;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceFactory;
@@ -597,11 +598,10 @@ public class SingleComponentManager<S> e
      *                      the Configuration Admin Service or <code>null</code> if there is
      *                      no configuration or if the configuration has just been deleted.
      * @param configurationDeleted TODO
-     * @param changeCount Change count for the configuration
-     * @param targetedPID TargetedPID for the configuration
+     * @param factoryPid TODO
      */
     @Override
-    public void reconfigure( Map<String, Object> configuration, boolean configurationDeleted )
+    public void reconfigure( Map<String, Object> configuration, boolean configurationDeleted, TargetedPID factoryPid)
     {
         // store the properties
         m_configurationProperties = configuration;

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/manager/ConfiguredComponentHolderTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/manager/ConfiguredComponentHolderTest.java?rev=1764242&r1=1764241&r2=1764242&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/manager/ConfiguredComponentHolderTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/manager/ConfiguredComponentHolderTest.java Tue Oct 11 13:41:34 2016
@@ -223,8 +223,8 @@ public class ConfiguredComponentHolderTe
             return m_configuration != null;
         }
 
-
-        public void reconfigure( Map<String, Object> configuration, boolean configurationDeleted )
+        @Override
+        public void reconfigure( Map<String, Object> configuration, boolean configurationDeleted, TargetedPID factoryPid)
         {
             this.m_configuration = configuration;
         }

Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/Felix5356Test.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/Felix5356Test.java?rev=1764242&view=auto
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/Felix5356Test.java (added)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/Felix5356Test.java Tue Oct 11 13:41:34 2016
@@ -0,0 +1,125 @@
+/*
+ * 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.Arrays;
+import java.util.Hashtable;
+
+import org.apache.felix.scr.integration.components.SimpleService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentFactory;
+import org.osgi.service.component.ComponentInstance;
+import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests of nonstandard ComponentFactory behavior
+ */
+
+@RunWith(JUnit4TestRunner.class)
+public class Felix5356Test extends ComponentTestBase
+{
+
+    private static final String PROP_NAME_FACTORY = ComponentTestBase.PROP_NAME + ".factory";
+
+    static
+    {
+        descriptorFile = "/integration_test_simple_factory_components.xml";
+        // uncomment to enable debugging of this test class
+        // paxRunnerVmOption = DEBUG_VM_OPTION;
+    }
+
+    @Test
+    public void test_factory_component_instance_with_factory_configuration_after_enable() throws Exception
+    {
+        do_test_factory_component_with_factory_configuration(false);
+    }
+
+    @Test
+    public void test_factory_component_instance_with_factory_configuration_before_enable() throws Exception
+    {
+        do_test_factory_component_with_factory_configuration(true);
+    }
+
+    private void do_test_factory_component_with_factory_configuration(boolean createConfigBeforeEnable) throws Exception {
+        final String componentname = "factory.component.referred";
+        final String componentfactory = "factory.component.factory.referred";
+
+        String factoryConfigPid = null;
+        if ( createConfigBeforeEnable ) {
+            // create the factory configuration before enabling
+            factoryConfigPid = createFactoryConfiguration( componentname, "?" );
+            delay();
+        }
+
+        getConfigurationsDisabledThenEnable( componentname, 0, -1 );
+
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put( "service.pid", "myFactoryInstance" );
+        final ComponentFactory factory = getComponentFactory( componentfactory );
+
+        final ComponentInstance instance = factory.newInstance( props );
+        TestCase.assertNotNull( instance );
+
+        TestCase.assertNotNull( instance.getInstance() );
+        TestCase.assertTrue( instance.getInstance() instanceof SimpleService );
+
+        // check registered components
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
+        // check registered service
+        findServices(SimpleService.class.getName(), null, 1);
+
+        if ( !createConfigBeforeEnable ) {
+            factoryConfigPid = createFactoryConfiguration( componentname, "?" );
+            delay();
+
+            // check registered components, again should only be 1
+            checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
+            // check registered service
+            findServices(SimpleService.class.getName(), null, 1);
+            TestCase.assertNotNull(instance.getInstance());
+        }
+
+        // delete the factory config
+        getConfigurationAdmin().getConfiguration( factoryConfigPid ).delete();
+        delay();
+
+        // check registered components, again should only be 1
+        checkConfigurationCount( componentname, 1, ComponentConfigurationDTO.ACTIVE );
+
+        instance.dispose();
+
+        // check registered components
+        checkConfigurationCount( componentname, 0, ComponentConfigurationDTO.ACTIVE );
+    }
+
+    private void findServices(String clazz, String filter, int expected) throws InvalidSyntaxException {
+        ServiceReference<?>[] services = bundleContext.getServiceReferences(clazz, filter);
+        if (services == null) {
+            services = new ServiceReference<?>[0];
+        }
+        TestCase.assertEquals("Wrong number of services found for clazz \"" + clazz + "\" and filter \"" + filter + "\" found: " + Arrays.toString(services), expected, services.length);
+    }
+
+}