You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2013/09/04 10:27:20 UTC

svn commit: r1519937 - in /felix/trunk/ipojo/runtime: core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ core-it/ipojo-core-configuration-admin-test/src/main/resources/ core-it/ipojo-core-configura...

Author: clement
Date: Wed Sep  4 08:27:20 2013
New Revision: 1519937

URL: http://svn.apache.org/r1519937
Log:
Fix FELIX-4207 ipojo @Component with propagation set to true doesn't propagate properties

All not excluded properties must be propagated when the propagation is enabled.

Added:
    felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ConfigurableFooProviderWithPropagation.java
    felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestPropagation.java
Modified:
    felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/resources/metadata.xml
    felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
    felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandlerDescription.java

Added: felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ConfigurableFooProviderWithPropagation.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ConfigurableFooProviderWithPropagation.java?rev=1519937&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ConfigurableFooProviderWithPropagation.java (added)
+++ felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/ConfigurableFooProviderWithPropagation.java Wed Sep  4 08:27:20 2013
@@ -0,0 +1,73 @@
+/*
+ * 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.ipojo.runtime.core.components;
+
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Property;
+import org.apache.felix.ipojo.annotations.Provides;
+import org.apache.felix.ipojo.runtime.core.services.FooService;
+
+import java.util.Properties;
+
+@Component(propagation = true)
+@Provides
+public class ConfigurableFooProviderWithPropagation implements FooService {
+
+    @Property
+    private String message;
+
+    @Property(name=".private")
+    private String myPrivateLife;
+
+    public boolean foo() {
+        return true;
+    }
+
+    public Properties fooProps() {
+        Properties props = new Properties();
+        if (message == null) {
+            props.put("message", "NULL");
+        } else {
+            props.put("message", message);
+        }
+        return props;
+    }
+
+    public boolean getBoolean() {
+        return false;
+    }
+
+    public double getDouble() {
+        return 0;
+    }
+
+    public int getInt() {
+        return 0;
+    }
+
+    public long getLong() {
+        return 0;
+    }
+
+    public Boolean getObject() {
+        return null;
+    }
+
+}

Modified: felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/resources/metadata.xml?rev=1519937&r1=1519936&r2=1519937&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/resources/metadata.xml (original)
+++ felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/main/resources/metadata.xml Wed Sep  4 08:27:20 2013
@@ -37,5 +37,4 @@
         </properties>
     </component>
 
-
 </ipojo>

Added: felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestPropagation.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestPropagation.java?rev=1519937&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestPropagation.java (added)
+++ felix/trunk/ipojo/runtime/core-it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestPropagation.java Wed Sep  4 08:27:20 2013
@@ -0,0 +1,92 @@
+/*
+ * 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.ipojo.runtime.core;
+
+import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.apache.felix.ipojo.handlers.configuration.ConfigurationHandlerDescription;
+import org.apache.felix.ipojo.runtime.core.services.FooService;
+import org.junit.Test;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.ow2.chameleon.testing.helpers.Dumps;
+
+import java.beans.PropertyDescriptor;
+import java.io.IOException;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNull;
+
+/**
+ * Test for FELIX-4207 - ipojo @Component with propagation set to true doesn't propagate properties
+ */
+public class TestPropagation extends Common {
+
+    private String factoryName = "org.apache.felix.ipojo.runtime.core.components.ConfigurableFooProviderWithPropagation";
+
+    @Test
+    public void testPropagationFromConfigurationAdminWhenCreatingTheInstance() throws IOException {
+        Properties props = new Properties();
+        props.put("message", "message");
+        props.put("propagated", "propagated");
+        props.put(".private", "wow");
+
+        Configuration configuration = admin.createFactoryConfiguration(factoryName, "?");
+        configuration.update(props);
+
+        ServiceReference ref = osgiHelper.waitForService(FooService.class.getName(),
+                "(instance.name=" + configuration.getPid() + ")",
+                1000);
+
+        // Check the propagation
+        assertEquals(ref.getProperty("propagated"), "propagated");
+        assertEquals(ref.getProperty("message"), "message");
+        assertNull(ref.getProperty(".private"));
+        assertNull(ref.getProperty("private"));
+
+        // Check the the .private property has the right value
+        ConfigurationHandlerDescription desc = (ConfigurationHandlerDescription) ipojoHelper.getArchitectureByName(configuration.getPid())
+                .getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:properties");
+        PropertyDescription prop = desc.getPropertyByName(".private");
+        assertEquals(prop.getValue(), "wow");
+
+        // Update the property
+        props.put("message", "message2");
+        props.put("propagated", "propagated2");
+        props.put(".private", "wow2");
+        configuration.update(props);
+
+        grace();
+
+        ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), configuration.getPid());
+        // Check the propagation
+        assertEquals(ref.getProperty("propagated"), "propagated2");
+        assertEquals(ref.getProperty("message"), "message2");
+
+        desc = (ConfigurationHandlerDescription) ipojoHelper.getArchitectureByName(configuration.getPid())
+                .getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:properties");
+        prop = desc.getPropertyByName(".private");
+        assertEquals(prop.getValue(), "wow2");
+
+        configuration.delete();
+    }
+
+
+}

Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java?rev=1519937&r1=1519936&r2=1519937&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandler.java Wed Sep  4 08:27:20 2013
@@ -92,7 +92,6 @@ public class ConfigurationHandler extend
      * This method is called when a reconfiguration is completed.
      */
     private Callback m_updated;
-
     /**
      * The configuration listeners.
      */
@@ -226,7 +225,7 @@ public class ConfigurationHandler extend
                 String key = (String) keys.nextElement();
                 // To conform with 'Property Propagation 104.4.4 (Config Admin spec)',
                 // we don't propagate properties starting with .
-                if (!key.startsWith(".")) {
+                if (!excluded(key)) {
                     m_toPropagate.put(key, configuration.get(key));
                 }
             }
@@ -360,7 +359,7 @@ public class ConfigurationHandler extend
 
             // Security Check
             if (SecurityHelper.hasPermissionToRegisterService(ManagedService.class.getName(),
-                    getInstanceManager().getContext())  && SecurityHelper.canRegisterService
+                    getInstanceManager().getContext()) && SecurityHelper.canRegisterService
                     (getInstanceManager().getContext())) {
                 m_sr = getInstanceManager().getContext().registerService(ManagedService.class.getName(), this, props);
             } else {
@@ -428,13 +427,16 @@ public class ConfigurationHandler extend
             // Check if the name is a configurable property
             for (Property prop : m_configurableProperties) {
                 if (prop.getName().equals(name)) {
-                    reconfigureProperty(prop, value);
+                    Object v = reconfigureProperty(prop, value);
                     found = true;
+                    if (m_mustPropagate && ! excluded(name)) {
+                        toPropagate.put(name, v);
+                    }
                     break; // Exit the search loop
                 }
             }
-            if (!found) {
-                // The property is not a configurable property, add it to the toPropagate list.
+
+            if (!found && m_mustPropagate && ! excluded(name)) {
                 toPropagate.put(name, value);
             }
         }
@@ -450,6 +452,18 @@ public class ConfigurationHandler extend
     }
 
     /**
+     * Checks whether the property with this given name must not be propagated.
+     * @param name the name of the property
+     * @return {@code true} if the property must not be propagated
+     */
+    private boolean excluded(String name) {
+        return name.startsWith(".")
+                || Factory.INSTANCE_NAME_PROPERTY.equals(name)
+                || Factory.FACTORY_VERSION_PROPERTY.equals(name)
+                || "factory.name".equals(name);
+    }
+
+    /**
      * Reconfigures the given property with the given value.
      * This methods handles {@link org.apache.felix.ipojo.InstanceManager#onSet(Object, String, Object)}
      * call and the callback invocation.
@@ -457,8 +471,9 @@ public class ConfigurationHandler extend
      *
      * @param prop  the property object to reconfigure
      * @param value the new value.
+     * @return the new property value
      */
-    public void reconfigureProperty(Property prop, Object value) {
+    public Object reconfigureProperty(Property prop, Object value) {
         if (prop.getValue() == null || !prop.getValue().equals(value)) {
             prop.setValue(value);
             if (prop.hasField()) {
@@ -470,6 +485,7 @@ public class ConfigurationHandler extend
                 }
             }
         }
+        return prop.getValue();
     }
 
     /**
@@ -680,7 +696,7 @@ public class ConfigurationHandler extend
      * Remove the given listener from the configuration handler's list of listeners.
      *
      * @param listener the {@code ConfigurationListener} object to be removed
-     * @throws NullPointerException if {@code listener} is {@code null}
+     * @throws NullPointerException   if {@code listener} is {@code null}
      * @throws NoSuchElementException if {@code listener} wasn't present the in configuration handler's list of listeners
      */
     public void removeListener(ConfigurationListener listener) {
@@ -691,7 +707,7 @@ public class ConfigurationHandler extend
             // We definitely cannot rely on listener's equals method...
             // ...so we need to manually search for the listener, using ==.
             int i = -1;
-            for(int j = m_listeners.size() -1; j>=0 ; j--) {
+            for (int j = m_listeners.size() - 1; j >= 0; j--) {
                 if (m_listeners.get(j) == listener) {
                     // Found!
                     i = j;
@@ -726,9 +742,9 @@ public class ConfigurationHandler extend
             } catch (Throwable e) {
                 // Failure inside a listener: put a warning on the logger, and continue
                 warn(String.format(
-                            "[%s] A ConfigurationListener has failed: %s",
-                            getInstanceManager().getInstanceName(),
-                            e.getMessage())
+                        "[%s] A ConfigurationListener has failed: %s",
+                        getInstanceManager().getInstanceName(),
+                        e.getMessage())
                         , e);
             }
         }

Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandlerDescription.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandlerDescription.java?rev=1519937&r1=1519936&r2=1519937&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandlerDescription.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/configuration/ConfigurationHandlerDescription.java Wed Sep  4 08:27:20 2013
@@ -103,6 +103,21 @@ public class ConfigurationHandlerDescrip
     public PropertyDescription[] getProperties() {
         return m_properties;
     }
+
+    /**
+     * Gets a property by name.
+     * @param name the property name
+     * @return the property description with the given name, {@code null} if there is no property with the given name.
+     * @since 1.10.2
+     */
+    public PropertyDescription getPropertyByName(String name) {
+        for (PropertyDescription desc :m_properties) {
+            if (name.equals(desc.getName())) {
+                return desc;
+            }
+        }
+        return null;
+    }
     
     /**
      * Gets the managed service pid.