FELIX-3679 As of DS 1.1 component names need not be globablly unique but must be unique within a bundle. Introducing a ComponentRegistryKey allowing for registration of same named components from different bundles but preventing such registration from within the same bundle.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1389240 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
index a4501ac..8ae7c33 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
@@ -220,10 +220,14 @@
while ( i.hasNext() )
{
ComponentMetadata metadata = ( ComponentMetadata ) i.next();
+ ComponentRegistryKey key = null;
try
{
- // check and reserve the component name
- m_componentRegistry.checkComponentName( metadata.getName() );
+ // check and reserve the component name (if not null)
+ if ( metadata.getName() != null )
+ {
+ key = m_componentRegistry.checkComponentName( m_context.getBundle(), metadata.getName() );
+ }
// validate the component metadata
metadata.validate( this );
@@ -232,7 +236,7 @@
ComponentHolder holder = m_componentRegistry.createComponentHolder( this, metadata );
// register the component after validation
- m_componentRegistry.registerComponentHolder( metadata.getName(), holder );
+ m_componentRegistry.registerComponentHolder( key, holder );
m_managers.add( holder );
// enable the component
@@ -248,7 +252,10 @@
log( LogService.LOG_ERROR, "Cannot register Component", metadata, t );
// make sure the name is not reserved any more
- m_componentRegistry.unregisterComponentHolder( metadata.getName() );
+ if ( key != null )
+ {
+ m_componentRegistry.unregisterComponentHolder( key );
+ }
}
}
}
@@ -313,7 +320,8 @@
}
finally
{
- m_componentRegistry.unregisterComponentHolder( holder.getComponentMetadata().getName() );
+ m_componentRegistry.unregisterComponentHolder( m_context.getBundle(), holder.getComponentMetadata()
+ .getName() );
}
}
@@ -449,7 +457,7 @@
return ( ComponentHolder[] ) m_managers.toArray( new ComponentHolder[m_managers.size()] );
}
- if ( m_componentRegistry.getComponentHolder( name ) != null )
+ if ( m_componentRegistry.getComponentHolder( m_context.getBundle(), name ) != null )
{
// otherwise just find it
Iterator it = m_managers.iterator();
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistry.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistry.java
index e0d8378..deb4e99 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistry.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistry.java
@@ -20,11 +20,13 @@
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -78,18 +80,18 @@
* @see #registerComponentHolder(String, ComponentHolder)
* @see #unregisterComponentHolder(String)
*/
- private final Map m_componentHoldersByName;
+ private final Map /* <ComponentRegistryKey, Object> */ m_componentHoldersByName;
/**
* The map of known components indexed by component configuration pid. The values are
* Sets of the {@link ComponentHolder} interface. Normally, the configuration pid
- * is the component name, but since DS 1.2 (OSGi 4.3), a component may specify a specific
- * pid, and it is possible that different components refer to the same pid. That's why
+ * is the component name, but since DS 1.2 (OSGi 4.3), a component may specify a specific
+ * pid, and it is possible that different components refer to the same pid. That's why
* the values of this map are Sets of ComponentHolders, allowing to lookup all components
* which are using a given configuration pid.
- * This map is used when the ConfigurationSupport detects that a CM pid is updated. When
- * a PID is updated, the ConfigurationSupport listener class invokes the
- * {@link #getComponentHoldersByPid(String)} method which returns an iterator over all
+ * This map is used when the ConfigurationSupport detects that a CM pid is updated. When
+ * a PID is updated, the ConfigurationSupport listener class invokes the
+ * {@link #getComponentHoldersByPid(String)} method which returns an iterator over all
* components that are using the given pid for configuration.
* <p>
*
@@ -129,7 +131,7 @@
protected ComponentRegistry( BundleContext context )
{
m_bundleContext = context;
- m_componentHoldersByName = new HashMap();
+ m_componentHoldersByName = new HashMap /* <ComponentRegistryKey, Object> */ ();
m_componentHoldersByPid = new HashMap();
m_componentsById = new HashMap();
m_componentCounter = -1;
@@ -248,13 +250,20 @@
public Component[] getComponents( String componentName )
{
- final ComponentHolder holder = getComponentHolder( componentName );
- if ( holder != null )
+ List /* <ComponentHolder */list = new ArrayList/* <ComponentHolder> */();
+ synchronized ( m_componentHoldersByName )
{
- return holder.getComponents();
+ for ( Iterator ci = m_componentHoldersByName.values().iterator(); ci.hasNext(); )
+ {
+ ComponentHolder c = ( ComponentHolder ) ci.next();
+ if ( componentName.equals( c.getComponentMetadata().getName() ) )
+ {
+ list.addAll( Arrays.asList( c.getComponents() ) );
+ }
+ }
}
- return null;
+ return ( list.isEmpty() ) ? null : ( Component[] ) list.toArray( new Component[list.size()] );
}
@@ -311,20 +320,22 @@
* If a component with the same name has already been reserved or registered
* a ComponentException is thrown with a descriptive message.
*
+ * @param bundle the bundle registering the component
* @param name the component name to check and reserve
* @throws ComponentException if the name is already in use by another
* component.
*/
- final void checkComponentName( String name )
+ final ComponentRegistryKey checkComponentName( final Bundle bundle, final String name )
{
// register the name if no registration for that name exists already
+ final ComponentRegistryKey key = new ComponentRegistryKey( bundle, name );
final Object existingRegistration;
synchronized ( m_componentHoldersByName )
{
- existingRegistration = m_componentHoldersByName.get( name );
+ existingRegistration = m_componentHoldersByName.get( key );
if ( existingRegistration == null )
{
- m_componentHoldersByName.put( name, name );
+ m_componentHoldersByName.put( key, key );
}
}
@@ -352,6 +363,8 @@
throw new ComponentException( message );
}
+
+ return key;
}
@@ -366,18 +379,19 @@
* @throws ComponentException if the name has not been reserved through
* {@link #checkComponentName(String)} yet.
*/
- final void registerComponentHolder( String name, ComponentHolder component )
+ final void registerComponentHolder( final ComponentRegistryKey key, ComponentHolder component )
{
synchronized ( m_componentHoldersByName )
{
// only register the component if there is a m_registration for it !
- if ( !name.equals( m_componentHoldersByName.get( name ) ) )
+ if ( !key.equals( m_componentHoldersByName.get( key ) ) )
{
// this is not expected if all works ok
- throw new ComponentException( "The component name '" + name + "' has already been registered." );
+ throw new ComponentException( "The component name '" + component.getComponentMetadata().getName()
+ + "' has already been registered." );
}
- m_componentHoldersByName.put( name, component );
+ m_componentHoldersByName.put( key, component );
}
synchronized (m_componentHoldersByPid)
@@ -387,7 +401,7 @@
// Since several components may refer to the same configuration pid, we have to
// store the component holder in a Set, in order to be able to lookup every
- // components from a given pid.
+ // components from a given pid.
Set set = (Set) m_componentHoldersByPid.get(configurationPid);
if (set == null)
{
@@ -402,12 +416,12 @@
* Returns the component registered under the given name or <code>null</code>
* if no component is registered yet.
*/
- public final ComponentHolder getComponentHolder( String name )
+ public final ComponentHolder getComponentHolder( final Bundle bundle, final String name )
{
Object entry;
synchronized ( m_componentHoldersByName )
{
- entry = m_componentHoldersByName.get( name );
+ entry = m_componentHoldersByName.get( new ComponentRegistryKey( bundle, name ) );
}
// only return the entry if non-null and not a reservation
@@ -420,10 +434,10 @@
}
/**
- * Returns the list of ComponentHolder instances whose configuration pids are matching
+ * Returns the list of ComponentHolder instances whose configuration pids are matching
* the given pid.
* @param pid the pid candidate
- * @return a iterator of ComponentHolder, or an empty iterator if no ComponentHolders
+ * @return a iterator of ComponentHolder, or an empty iterator if no ComponentHolders
* are found
*/
public final Iterator getComponentHoldersByPid(String pid)
@@ -462,14 +476,26 @@
* <p>
* After calling this method, the name can be reused by other components.
*/
- final void unregisterComponentHolder( String name )
+ final void unregisterComponentHolder( final Bundle bundle, final String name )
+ {
+ unregisterComponentHolder( new ComponentRegistryKey( bundle, name ) );
+ }
+
+
+ /**
+ * Removes the component registered under that name. If no component is
+ * yet registered but the name is reserved, it is unreserved.
+ * <p>
+ * After calling this method, the name can be reused by other components.
+ */
+ final void unregisterComponentHolder( final ComponentRegistryKey key )
{
Object component;
synchronized ( m_componentHoldersByName )
{
- component = m_componentHoldersByName.remove(name);
+ component = m_componentHoldersByName.remove( key );
}
-
+
if (component instanceof ComponentHolder) {
synchronized (m_componentHoldersByPid)
{
@@ -478,7 +504,7 @@
if (componentsForPid != null)
{
componentsForPid.remove(component);
- if (componentsForPid.size() == 0)
+ if (componentsForPid.size() == 0)
{
m_componentHoldersByPid.remove(configurationPid);
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistryKey.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistryKey.java
new file mode 100644
index 0000000..ca9cef4
--- /dev/null
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ComponentRegistryKey.java
@@ -0,0 +1,69 @@
+/*
+ * 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;
+
+
+import org.osgi.framework.Bundle;
+
+
+/**
+ * The <code>ComponentRegistryKey</code> isused as the key in the
+ * component register to register components by their names.
+ * <p>
+ * Two instances of this class are equal if they are the same or if there
+ * component name and bundle ID is equal.
+ */
+final class ComponentRegistryKey
+{
+
+ private final long bundleId;
+ private final String componentName;
+
+
+ ComponentRegistryKey( final Bundle bundle, final String componentName )
+ {
+ this.bundleId = bundle.getBundleId();
+ this.componentName = componentName;
+ }
+
+
+ public int hashCode()
+ {
+ int code = ( int ) this.bundleId;
+ code += 31 * this.componentName.hashCode();
+ return code;
+ }
+
+
+ public boolean equals( Object obj )
+ {
+ if ( this == obj )
+ {
+ return true;
+ }
+
+ if ( obj instanceof ComponentRegistryKey )
+ {
+ ComponentRegistryKey other = ( ComponentRegistryKey ) obj;
+ return this.bundleId == other.bundleId && this.componentName.equals( other.componentName );
+ }
+
+ return false;
+ }
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/ComponentRegistryKeyTest.java b/scr/src/test/java/org/apache/felix/scr/impl/ComponentRegistryKeyTest.java
new file mode 100644
index 0000000..df4877f
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/impl/ComponentRegistryKeyTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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;
+
+
+import junit.framework.TestCase;
+
+
+public class ComponentRegistryKeyTest extends TestCase
+{
+
+ private final ComponentRegistryKey b1_a_0 = key( 1, "a" );
+ private final ComponentRegistryKey b2_a_0 = key( 2, "a" );
+
+ private final ComponentRegistryKey b1_a_1 = key( 1, "a" );
+ private final ComponentRegistryKey b2_a_1 = key( 2, "a" );
+
+ private final ComponentRegistryKey b1_b = key( 1, "b" );
+ private final ComponentRegistryKey b2_b = key( 2, "b" );
+
+
+ public void test_globally_unique_key()
+ {
+ // same
+ TestCase.assertEquals( b1_a_0, b1_a_0 );
+
+ // equals both ways
+ TestCase.assertEquals( b1_a_0, b1_a_1 );
+ TestCase.assertEquals( b1_a_1, b1_a_0 );
+
+ // not equals both ways
+ TestCase.assertFalse( b1_a_0.equals( b2_a_0 ) );
+ TestCase.assertFalse( b2_a_0.equals( b1_a_0 ) );
+
+ // not equals both ways
+ TestCase.assertFalse( b1_a_0.equals( b1_b ) );
+ TestCase.assertFalse( b1_b.equals( b1_a_0 ) );
+
+ // not equals both ways
+ TestCase.assertFalse( b1_a_0.equals( b2_b ) );
+ TestCase.assertFalse( b2_b.equals( b1_a_0 ) );
+ }
+
+
+ private static ComponentRegistryKey key( final long bundleId, final String name )
+ {
+ return new ComponentRegistryKey( new MockBundle()
+ {
+// @Override
+ public long getBundleId()
+ {
+ return bundleId;
+ }
+ }, name );
+ }
+}