FELIX-1232 Apply modified patch by Alin Dreghiciu (thanks). The service
properties for service registration or update are now retrieved using
a new getServiceProperties() method which in turns calls the new copyTo
method taking a new allProps flag indicating that only public properties
are to be copied.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@799618 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
index 2804f2b..d5f7777 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
@@ -723,7 +723,7 @@
log( LogService.LOG_DEBUG, "registering services", m_componentMetadata, null );
// get a copy of the component properties as service properties
- Dictionary serviceProperties = copyTo( null, getProperties() );
+ final Dictionary serviceProperties = getServiceProperties();
return getActivator().getBundleContext().registerService(
getComponentMetadata().getServiceMetadata().getProvides(),
@@ -925,6 +925,17 @@
public abstract Dictionary getProperties();
/**
+ * Returns the subset of component properties to be used as service
+ * properties. These properties are all component properties where property
+ * name does not start with dot (.), properties which are considered
+ * private.
+ */
+ public Dictionary getServiceProperties()
+ {
+ return copyTo( null, getProperties(), false);
+ }
+
+ /**
* Copies the properties from the <code>source</code> <code>Dictionary</code>
* into the <code>target</code> <code>Dictionary</code>.
*
@@ -938,7 +949,31 @@
* <code>source</code> is <code>null</code> or empty and
* <code>target</code> was <code>null</code>.
*/
- protected Dictionary copyTo( Dictionary target, Dictionary source )
+ protected static Dictionary copyTo( Dictionary target, Dictionary source )
+ {
+ return copyTo( target, source, true );
+ }
+
+ /**
+ * Copies the properties from the <code>source</code> <code>Dictionary</code>
+ * into the <code>target</code> <code>Dictionary</code> except for private
+ * properties (whose name has a leading dot) which are only copied if the
+ * <code>allProps</code> parameter is <code>true</code>.
+ *
+ * @param target The <code>Dictionary</code> into which to copy the
+ * properties. If <code>null</code> a new <code>Hashtable</code> is
+ * created.
+ * @param source The <code>Dictionary</code> providing the properties to
+ * copy. If <code>null</code> or empty, nothing is copied.
+ * @param allProps Whether all properties (<code>true</code>) or only the
+ * public properties (<code>false</code>) are to be copied.
+ *
+ * @return The <code>target</code> is returned, which may be empty if
+ * <code>source</code> is <code>null</code> or empty and
+ * <code>target</code> was <code>null</code> or all properties are
+ * private and had not to be copied
+ */
+ protected static Dictionary copyTo( Dictionary target, final Dictionary source, final boolean allProps )
{
if ( target == null )
{
@@ -949,14 +984,19 @@
{
for ( Enumeration ce = source.keys(); ce.hasMoreElements(); )
{
- Object key = ce.nextElement();
- target.put( key, source.get( key ) );
+ // cast is save, because key must be a string as per the spec
+ String key = ( String ) ce.nextElement();
+ if ( allProps || key.charAt( 0 ) != '.' )
+ {
+ target.put( key, source.get( key ) );
+ }
}
}
return target;
}
+
/**
*
*/
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
index 449f9a2..d57b505 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
@@ -440,7 +440,7 @@
{
try
{
- final Dictionary regProps = copyTo( null, props );
+ final Dictionary regProps = getServiceProperties();
sr.setProperties( regProps );
}
catch ( IllegalStateException ise )
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/manager/AbstractComponentManagerTest.java b/scr/src/test/java/org/apache/felix/scr/impl/manager/AbstractComponentManagerTest.java
new file mode 100644
index 0000000..5d34b99
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/impl/manager/AbstractComponentManagerTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.impl.manager;
+
+import java.util.Hashtable;
+import junit.framework.TestCase;
+
+public class AbstractComponentManagerTest extends TestCase
+{
+
+ public void test_copyTo_withoutExclusions()
+ {
+ final Hashtable ht = new Hashtable();
+ ht.put( "p1", "v1" );
+ ht.put( "p.2", "v2" );
+ ht.put( ".p3", "v3" );
+ final Hashtable dict = (Hashtable) AbstractComponentManager.copyTo( null, ht, true );
+ assertNotNull( "Copy result is not null", dict );
+ assertEquals( "Number of items", 3, dict.size() );
+ assertEquals( "Value for key p1", "v1", dict.get( "p1" ) );
+ assertEquals( "Value for key p.2", "v2", dict.get( "p.2" ) );
+ assertEquals( "Value for key .p3", "v3", dict.get( ".p3" ) );
+ }
+
+ public void test_copyTo_excludingStartingWithDot()
+ {
+ final Hashtable ht = new Hashtable();
+ ht.put( "p1", "v1" );
+ ht.put( "p.2", "v2" );
+ ht.put( ".p3", "v3" );
+ final Hashtable dict = (Hashtable) AbstractComponentManager.copyTo( null, ht, false );
+ assertNotNull( "Copy result is not null", dict );
+ assertEquals( "Number of items", 2, dict.size() );
+ assertEquals( "Value for key p1", "v1", dict.get( "p1" ) );
+ assertEquals( "Value for key p.2", "v2", dict.get( "p.2" ) );
+ }
+
+}