FELIX-1223 Refactor Configuration support
- use ConfigurationAdmin.listConfiguration to get initial configuration
- implement ConfigurationListener interface for configuration update
- provide configuration through ComponentRegistry
- Remove Component interface since the only implementation is the
   AbstractComponentManager and it is internal only
- Remove ManagerFactory and inline the functionality in the
   BundleComponentActivator

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@784729 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
index 2954dbc..bd6dc97 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
@@ -28,6 +28,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.felix.scr.Component;
 import org.apache.felix.scr.Reference;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.InvalidSyntaxException;
@@ -41,7 +42,7 @@
  * implementation object's lifecycle.
  *
  */
-abstract class AbstractComponentManager implements ComponentManager, ComponentInstance
+abstract class AbstractComponentManager implements Component, ComponentInstance
 {
     // the ID of this component
     private long m_componentId;
@@ -448,12 +449,12 @@
      * @param metadata
      * @param componentId
      */
-    protected AbstractComponentManager( BundleComponentActivator activator,
-            ComponentMetadata metadata, long componentId )
+    protected AbstractComponentManager( BundleComponentActivator activator, ComponentMetadata metadata,
+        ComponentRegistry componentRegistry )
     {
         m_activator = activator;
         m_componentMetadata = metadata;
-        m_componentId = componentId;
+        m_componentId = componentRegistry.createComponentId();
 
         m_state = Disabled.getInstance();
         loadDependencyManagers( metadata );
@@ -628,7 +629,7 @@
         // The reason for this is that we just want to have the state return
         // the service reference which comes from the service registration.
         // The only thing that may happen is that the service registration is
-        // still set on this instance but the service has already been 
+        // still set on this instance but the service has already been
         // unregistered. In this case an IllegalStateException may be thrown
         // which we just catch and ignore returning null
         State state = m_state;
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 b537330..273106e 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
@@ -168,7 +168,7 @@
                         metadata.validate( this );
 
                         // Request creation of the component manager
-                        ComponentManager manager;
+                        AbstractComponentManager manager;
                         if ( metadata.isFactory() )
                         {
                             // 112.2.4 SCR must register a Component Factory
@@ -176,10 +176,26 @@
                             // as soon as the component factory is satisfied
                             manager = new ComponentFactoryImpl( this, metadata, m_componentRegistry );
                         }
+                        else if ( metadata.isImmediate() )
+                        {
+                            manager = new ImmediateComponentManager( this, metadata, m_componentRegistry );
+                        }
+                        else if ( metadata.getServiceMetadata() != null )
+                        {
+                            if ( metadata.getServiceMetadata().isServiceFactory() )
+                            {
+                                manager = new ServiceFactoryComponentManager( this, metadata, m_componentRegistry );
+                            }
+                            else
+                            {
+                                manager = new DelayedComponentManager( this, metadata, m_componentRegistry );
+                            }
+                        }
                         else
                         {
-                            manager = ManagerFactory.createManager( this, metadata, m_componentRegistry
-                                .createComponentId() );
+                            // if we get here, which is not expected after all, we fail
+                            throw new IllegalArgumentException( "Cannot create a component manager for "
+                                + metadata.getName() );
                         }
 
                         // register the component after validation
@@ -251,7 +267,7 @@
 
         while ( m_managers.size() != 0 )
         {
-            ComponentManager manager = ( ComponentManager ) m_managers.get( 0 );
+            AbstractComponentManager manager = ( AbstractComponentManager ) m_managers.get( 0 );
             try
             {
                 m_managers.remove( manager );
@@ -341,7 +357,7 @@
      */
     void enableComponent( String name )
     {
-        final ComponentManager[] cm = getSelectedComponents( name );
+        final AbstractComponentManager[] cm = getSelectedComponents( name );
         if ( cm == null )
         {
             return;
@@ -374,7 +390,7 @@
      */
     void disableComponent( String name )
     {
-        final ComponentManager[] cm = getSelectedComponents( name );
+        final AbstractComponentManager[] cm = getSelectedComponents( name );
         if ( cm == null )
         {
             return;
@@ -410,12 +426,12 @@
      *      to the <code>name</code> parameter or <code>null</code> if no
      *      component manager with the given name is currently registered.
      */
-    private ComponentManager[] getSelectedComponents( String name )
+    private AbstractComponentManager[] getSelectedComponents( String name )
     {
         // if all components are selected
         if ( name == null )
         {
-            return (org.apache.felix.scr.impl.ComponentManager[] ) m_managers.toArray( new ComponentManager[m_managers.size()] );
+            return ( AbstractComponentManager[] ) m_managers.toArray( new AbstractComponentManager[m_managers.size()] );
         }
 
         if ( m_componentRegistry.getComponent( name ) != null )
@@ -424,10 +440,10 @@
             Iterator it = m_managers.iterator();
             while ( it.hasNext() )
             {
-                ComponentManager cm = ( ComponentManager ) it.next();
+                AbstractComponentManager cm = ( AbstractComponentManager ) it.next();
                 if ( name.equals( cm.getComponentMetadata().getName() ) )
                 {
-                    return new ComponentManager[]
+                    return new AbstractComponentManager[]
                         { cm };
                 }
             }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentActivatorTask.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentActivatorTask.java
index 642f4b5..bf51715 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ComponentActivatorTask.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ComponentActivatorTask.java
@@ -34,10 +34,10 @@
 {
 
     private final String taskName;
-    private final ComponentManager component;
+    private final AbstractComponentManager component;
 
 
-    protected ComponentActivatorTask( String taskName, ComponentManager component )
+    protected ComponentActivatorTask( String taskName, AbstractComponentManager component )
     {
         this.taskName = taskName;
         this.component = component;
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentFactoryImpl.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentFactoryImpl.java
index b7f0a78..7c94c27 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ComponentFactoryImpl.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ComponentFactoryImpl.java
@@ -26,7 +26,7 @@
 
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.service.cm.Configuration;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.ComponentFactory;
 import org.osgi.service.component.ComponentInstance;
@@ -35,14 +35,14 @@
 /**
  * The <code>ComponentFactoryImpl</code> TODO
  */
-class ComponentFactoryImpl extends AbstractComponentManager implements ComponentFactory, ManagedServiceFactory
+class ComponentFactoryImpl extends AbstractComponentManager implements ComponentFactory
 {
 
     // The component registry used to retrieve component IDs
     private ComponentRegistry m_componentRegistry;
 
     // The map of components created from Configuration objects
-    // maps PID to ComponentManager for configuration updating
+    // maps PID to ImmediateComponentManager for configuration updating
     // this map is lazily created
     private Map m_configuredServices;
 
@@ -54,7 +54,7 @@
     ComponentFactoryImpl( BundleComponentActivator activator, ComponentMetadata metadata,
         ComponentRegistry componentRegistry )
     {
-        super( activator, metadata, componentRegistry.createComponentId() );
+        super( activator, metadata, componentRegistry );
         m_componentRegistry = componentRegistry;
         m_createdComponents = new IdentityHashMap();
     }
@@ -65,7 +65,7 @@
      */
     public ComponentInstance newInstance( Dictionary dictionary )
     {
-        return ( ComponentInstance ) createComponentManager( dictionary, true );
+        return createComponentManager( dictionary, true );
     }
 
 
@@ -86,10 +86,19 @@
     {
         log( LogService.LOG_DEBUG, "registering component factory", getComponentMetadata(), null );
 
+        Configuration[] cfg = m_componentRegistry.getConfigurations( getActivator().getBundleContext(),
+            getComponentMetadata().getName() );
+        if ( cfg != null )
+        {
+            for ( int i = 0; i < cfg.length; i++ )
+            {
+                updated( cfg[i].getPid(), cfg[i].getProperties() );
+            }
+        }
+
         Dictionary serviceProperties = getProperties();
         return getActivator().getBundleContext().registerService( new String[]
-            { ComponentFactory.class.getName(), ManagedServiceFactory.class.getName() }, getService(),
-            serviceProperties );
+            { ComponentFactory.class.getName() }, getService(), serviceProperties );
     }
 
 
@@ -128,39 +137,42 @@
 
     //---------- ManagedServiceFactory interface ------------------------------
 
-    public void updated( String pid, Dictionary configuration )
+    void updated( String pid, Dictionary configuration )
     {
-        ComponentManager cm;
-        if ( m_configuredServices != null )
+        if ( getState() == STATE_FACTORY )
         {
-            cm = ( ComponentManager ) m_configuredServices.get( pid );
-        }
-        else
-        {
-            m_configuredServices = new HashMap();
-            cm = null;
-        }
+            ImmediateComponentManager cm;
+            if ( m_configuredServices != null )
+            {
+                cm = ( ImmediateComponentManager ) m_configuredServices.get( pid );
+            }
+            else
+            {
+                m_configuredServices = new HashMap();
+                cm = null;
+            }
 
-        if ( cm == null )
-        {
-            // create a new instance with the current configuration
-            cm = createComponentManager( configuration, false );
+            if ( cm == null )
+            {
+                // create a new instance with the current configuration
+                cm = createComponentManager( configuration, false );
 
-            // keep a reference for future updates
-            m_configuredServices.put( pid, cm );
-        }
-        else if ( cm instanceof ImmediateComponentManager )
-        {
-            // update the configuration as if called as ManagedService
-            ( ( ImmediateComponentManager ) cm ).reconfigure( configuration );
+                // keep a reference for future updates
+                m_configuredServices.put( pid, cm );
+            }
+            else
+            {
+                // update the configuration as if called as ManagedService
+                cm.reconfigure( configuration );
+            }
         }
     }
 
-    public void deleted( String pid )
+    void deleted( String pid )
     {
-        if ( m_configuredServices != null )
+        if ( getState() == STATE_FACTORY && m_configuredServices != null )
         {
-            ComponentManager cm = ( ComponentManager ) m_configuredServices.remove( pid );
+            ImmediateComponentManager cm = ( ImmediateComponentManager ) m_configuredServices.remove( pid );
             if ( cm != null )
             {
                 log( LogService.LOG_DEBUG, "Disposing component after configuration deletion", getComponentMetadata(),
@@ -196,11 +208,10 @@
      *      used as the normal configuration from configuration admin (not the
      *      factory configuration) and the component is enabled asynchronously.
      */
-    private ComponentManager createComponentManager( Dictionary configuration, boolean isNewInstance )
+    private ImmediateComponentManager createComponentManager( Dictionary configuration, boolean isNewInstance )
     {
-        long componentId = m_componentRegistry.createComponentId();
         ImmediateComponentManager cm = new ImmediateComponentManager( getActivator(), getComponentMetadata(),
-            componentId );
+            m_componentRegistry );
 
         // add the new component to the activators instances
         getActivator().getInstanceReferences().add( cm );
@@ -228,7 +239,7 @@
         return cm;
     }
 
-    private void disposeComponentManager( ComponentManager cm )
+    private void disposeComponentManager( ImmediateComponentManager cm )
     {
         // remove from created components
         m_createdComponents.remove( cm );
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentManager.java
deleted file mode 100644
index a71b58e..0000000
--- a/scr/src/main/java/org/apache/felix/scr/impl/ComponentManager.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*

- * 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.apache.felix.scr.Component;

-

-

-/**

- * This interface is provided so that there can be multiple implementations of

- * managers that are responsible for managing component's lifecycle.

- *

- */

-public interface ComponentManager extends Component {

-

-	/**

-	 * Enable the component

-	 */

-	public void enable();

-

-    /**

-     * Reconfigure the component with configuration data newly retrieved from

-     * the Configuration Admin Service.

-     */

-    public void reconfigure();

-

-    /**

-     * Disable the component. After disabling the component may be re-enabled

-     * by calling the {@link #enable()} method.

-     */

-    public void disable();

-

-	/**

-	 * Dispose the component. After disposing the component manager it must not

-     * be used anymore.

-	 */

-	public void dispose();

-

-	/**

-	 * Get the component information

-	 *

-	 * @return a ComponentMetadata object

-	 */

-	public ComponentMetadata getComponentMetadata();

-}

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 0fea12d..5866520 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
@@ -19,6 +19,7 @@
 package org.apache.felix.scr.impl;
 
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashMap;
@@ -31,7 +32,13 @@
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationEvent;
+import org.osgi.service.cm.ConfigurationListener;
 import org.osgi.service.component.ComponentException;
 
 
@@ -40,7 +47,7 @@
  *
  * @author fmeschbe
  */
-public class ComponentRegistry implements ScrService
+public class ComponentRegistry implements ScrService, ConfigurationListener
 {
 
     // Known and registered ComponentManager instances
@@ -66,7 +73,8 @@
         Dictionary props = new Hashtable();
         props.put( Constants.SERVICE_DESCRIPTION, "Declarative Services Management Agent" );
         props.put( Constants.SERVICE_VENDOR, "The Apache Software Foundation" );
-        m_registration = context.registerService( ScrService.class.getName(), this, props );
+        m_registration = context.registerService( new String[]
+            { ScrService.class.getName(), ConfigurationListener.class.getName() }, this, props );
     }
 
 
@@ -80,22 +88,25 @@
     }
 
 
-    //---------- ScrService interface -----------------------------------------
-
+    //---------- ScrService interface
 
     public Component[] getComponents()
     {
-        if (m_componentsById.isEmpty()) {
+        if ( m_componentsById.isEmpty() )
+        {
             return null;
         }
 
-        return (org.apache.felix.scr.Component[] ) m_componentsById.values().toArray( new Component[m_componentsById.size()] );
+        return ( org.apache.felix.scr.Component[] ) m_componentsById.values().toArray(
+            new Component[m_componentsById.size()] );
     }
 
+
     public Component[] getComponents( Bundle bundle )
     {
         Component[] all = getComponents();
-        if (all == null || all.length == 0) {
+        if ( all == null || all.length == 0 )
+        {
             return null;
         }
 
@@ -104,28 +115,155 @@
 
         // scan the components for the the desired components
         List perBundle = new ArrayList();
-        for (int i=0; i < all.length; i++) {
-            if (all[i].getBundle().getBundleId() == bundleId) {
+        for ( int i = 0; i < all.length; i++ )
+        {
+            if ( all[i].getBundle().getBundleId() == bundleId )
+            {
                 perBundle.add( all[i] );
             }
         }
 
         // nothing to return
-        if (perBundle.isEmpty()) {
+        if ( perBundle.isEmpty() )
+        {
             return null;
         }
 
-        return (org.apache.felix.scr.Component[] ) perBundle.toArray( new Component[perBundle.size()] );
+        return ( org.apache.felix.scr.Component[] ) perBundle.toArray( new Component[perBundle.size()] );
     }
 
 
     public Component getComponent( long componentId )
     {
-        return (Component) m_componentsById.get(new Long(componentId));
+        return ( Component ) m_componentsById.get( new Long( componentId ) );
     }
 
 
-    //---------- ComponentManager m_registration support ------------------------
+    //---------- ConfigurationListener
+
+    public void configurationEvent( ConfigurationEvent event )
+    {
+        final String pid = event.getPid();
+        final String factoryPid = event.getFactoryPid();
+
+        final AbstractComponentManager cm;
+        if ( factoryPid == null )
+        {
+            cm = getComponent( pid );
+        }
+        else
+        {
+            cm = getComponent( factoryPid );
+        }
+        
+        if (cm == null) {
+            // this configuration is not for a SCR component
+            return;
+        }
+
+        switch ( event.getType() )
+        {
+            case ConfigurationEvent.CM_DELETED:
+                if ( cm instanceof ImmediateComponentManager )
+                {
+                    ( ( ImmediateComponentManager ) cm ).reconfigure( null );
+                }
+                else if ( cm instanceof ComponentFactoryImpl )
+                {
+                    ( ( ComponentFactoryImpl ) cm ).deleted( pid );
+                }
+                break;
+            case ConfigurationEvent.CM_UPDATED:
+                BundleContext ctx = cm.getActivator().getBundleContext();
+                Dictionary dict = getConfiguration( event.getReference(), ctx, pid );
+                if ( dict != null )
+                {
+                    if ( cm instanceof ImmediateComponentManager )
+                    {
+                        ( ( ImmediateComponentManager ) cm ).reconfigure( dict );
+                    }
+                    else if ( cm instanceof ComponentFactoryImpl )
+                    {
+                        ( ( ComponentFactoryImpl ) cm ).updated( pid, dict );
+                    }
+                }
+                break;
+        }
+
+    }
+
+
+    private Dictionary getConfiguration( final ServiceReference cfgAdmin, final BundleContext ctx, final String pid )
+    {
+        final ConfigurationAdmin ca = ( ConfigurationAdmin ) ctx.getService( cfgAdmin );
+        if ( ca != null )
+        {
+            try
+            {
+                final Configuration cfg = ca.getConfiguration( pid );
+                if ( ctx.getBundle().getLocation().equals( cfg.getBundleLocation() ) )
+                {
+                    return cfg.getProperties();
+                }
+            }
+            catch ( IOException ioe )
+            {
+                // TODO: log
+            }
+            finally
+            {
+                ctx.ungetService( cfgAdmin );
+            }
+        }
+
+        return null;
+    }
+
+
+    Configuration getConfiguration( final BundleContext ctx, final String pid )
+    {
+        final String filter = "(service.pid=" + pid + ")";
+        Configuration[] cfg = getConfigurationInternal( ctx, filter );
+        return ( cfg == null || cfg.length == 0 ) ? null : cfg[0];
+    }
+
+
+    Configuration[] getConfigurations( final BundleContext ctx, final String factoryPid )
+    {
+        final String filter = "(service.factoryPid=" + factoryPid + ")";
+        return getConfigurationInternal( ctx, filter );
+    }
+
+
+    private Configuration[] getConfigurationInternal( final BundleContext ctx, final String filter )
+    {
+        final ServiceReference cfgAdmin = ctx.getServiceReference( ConfigurationAdmin.class.getName() );
+        final ConfigurationAdmin ca = ( ConfigurationAdmin ) ctx.getService( cfgAdmin );
+        if ( ca != null )
+        {
+            try
+            {
+                return ca.listConfigurations( filter );
+            }
+            catch ( IOException ioe )
+            {
+                // TODO: log
+            }
+            catch ( InvalidSyntaxException ise )
+            {
+                // TODO: log
+            }
+            finally
+            {
+                ctx.ungetService( cfgAdmin );
+            }
+        }
+
+        return null;
+    }
+
+
+    //---------- ComponentManager registration support
 
     long createComponentId()
     {
@@ -141,9 +279,9 @@
             String message = "The component name '" + name + "' has already been registered";
 
             Object co = m_componentsByName.get( name );
-            if ( co instanceof ComponentManager )
+            if ( co instanceof AbstractComponentManager )
             {
-                ComponentManager c = ( ComponentManager ) co;
+                AbstractComponentManager c = ( AbstractComponentManager ) co;
                 StringBuffer buf = new StringBuffer( message );
                 buf.append( " by Bundle " ).append( c.getBundle().getBundleId() );
                 if ( c.getBundle().getSymbolicName() != null )
@@ -163,7 +301,7 @@
     }
 
 
-    void registerComponent( String name, ComponentManager component )
+    void registerComponent( String name, AbstractComponentManager component )
     {
         // only register the component if there is a m_registration for it !
         if ( !name.equals( m_componentsByName.get( name ) ) )
@@ -173,18 +311,18 @@
         }
 
         m_componentsByName.put( name, component );
-        m_componentsById.put( new Long(component.getId()), component );
+        m_componentsById.put( new Long( component.getId() ), component );
     }
 
 
-    ComponentManager getComponent( String name )
+    AbstractComponentManager getComponent( String name )
     {
         Object entry = m_componentsByName.get( name );
 
         // only return the entry if non-null and not a reservation
-        if ( entry instanceof ComponentManager )
+        if ( entry instanceof AbstractComponentManager )
         {
-            return ( ComponentManager ) entry;
+            return ( AbstractComponentManager ) entry;
         }
 
         return null;
@@ -194,9 +332,9 @@
     void unregisterComponent( String name )
     {
         Object entry = m_componentsByName.remove( name );
-        if ( entry instanceof ComponentManager )
+        if ( entry instanceof AbstractComponentManager )
         {
-            Long id = new Long( ( ( ComponentManager ) entry ).getId() );
+            Long id = new Long( ( ( AbstractComponentManager ) entry ).getId() );
             m_componentsById.remove( id );
         }
     }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/DelayedComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/DelayedComponentManager.java
index ae36600..e79d1cf 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/DelayedComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/DelayedComponentManager.java
@@ -35,9 +35,10 @@
      * @param metadata
      * @param componentId
      */
-    public DelayedComponentManager( BundleComponentActivator activator, ComponentMetadata metadata, long componentId )
+    public DelayedComponentManager( BundleComponentActivator activator, ComponentMetadata metadata,
+        ComponentRegistry componentRegistry )
     {
-        super( activator, metadata, componentId );
+        super( activator, metadata, componentRegistry );
     }
 
 
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ImmediateComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/ImmediateComponentManager.java
index 662538e..bd14894 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ImmediateComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ImmediateComponentManager.java
@@ -28,6 +28,7 @@
 
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ManagedService;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.ComponentContext;
@@ -52,11 +53,6 @@
     // the component properties, also used as service properties
     private Dictionary m_properties;
 
-    // the managed service registration object created in the constructor
-    // to receive configuration from the Configuration Admin Service
-    // null, if this is a component created by a component factory
-    private ServiceRegistration m_managedServiceRegistration;
-
     // the component properties from the Configuration Admin Service
     // this is null, if none exist or none are provided
     private Dictionary m_configurationProperties;
@@ -68,30 +64,21 @@
      * @param activator
      * @param metadata
      */
-    ImmediateComponentManager( BundleComponentActivator activator, ComponentMetadata metadata, long componentId )
+    ImmediateComponentManager( BundleComponentActivator activator, ComponentMetadata metadata,
+        ComponentRegistry componentRegistry )
     {
-        super( activator, metadata, componentId );
+        super( activator, metadata, componentRegistry );
 
-        // only register as ManagedService if not created by a Component Factory
+        // only ask for configuration if not created by a Component Factory, in
+        // which case the configuration is provided by the Component Factory
         if ( !getComponentMetadata().isFactory() )
         {
-            Dictionary props = new Hashtable();
-            props.put( Constants.SERVICE_PID, getComponentMetadata().getName() );
-            props.put( Constants.SERVICE_DESCRIPTION, "ManagedService for Component "
-                + getComponentMetadata().getName() );
-            props.put( Constants.SERVICE_VENDOR, "The Apache Software Foundation" );
-
-            // register an anonymous managed service instance
-            ManagedService ms = new ManagedService()
+            Configuration cfg = componentRegistry.getConfiguration( activator.getBundleContext(),
+                getComponentMetadata().getName() );
+            if ( cfg != null )
             {
-                public void updated( Dictionary properties )
-                {
-                    reconfigure( properties );
-                }
-            };
-
-            m_managedServiceRegistration = activator.getBundleContext().registerService(
-                ManagedService.class.getName(), ms, props );
+                m_configurationProperties = cfg.getProperties();
+            }
         }
     }
 
@@ -102,19 +89,6 @@
      */
     public synchronized void dispose()
     {
-        if ( m_managedServiceRegistration != null )
-        {
-            try
-            {
-                m_managedServiceRegistration.unregister();
-                m_managedServiceRegistration = null;
-            }
-            catch ( Throwable t )
-            {
-                log( LogService.LOG_INFO, "Unexpected problem unregistering ManagedService", getComponentMetadata(), t );
-            }
-        }
-
         // really dispose off this manager instance
         disposeInternal();
     }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ManagerFactory.java b/scr/src/main/java/org/apache/felix/scr/impl/ManagerFactory.java
deleted file mode 100644
index 7f2a902..0000000
--- a/scr/src/main/java/org/apache/felix/scr/impl/ManagerFactory.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/* 

- * 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.service.log.LogService;

-

-

-/**

- * This factory allows other types of ComponentManagers to be provided.

- * 

- * 

- */

-public class ManagerFactory

-{

-

-    static ComponentManager createManager( BundleComponentActivator activator, ComponentMetadata metadata,

-        long componentId )

-    {

-        activator.log( LogService.LOG_DEBUG, "ManagerFactory.createManager", metadata, null );

-        if ( metadata.isImmediate() )

-        {

-            return new ImmediateComponentManager( activator, metadata, componentId );

-        }

-        else if ( metadata.getServiceMetadata() != null )

-        {

-            if ( metadata.getServiceMetadata().isServiceFactory() )

-            {

-                return new ServiceFactoryComponentManager( activator, metadata, componentId );

-            }

-

-            return new DelayedComponentManager( activator, metadata, componentId );

-        }

-

-        // if we get here, which is not expected after all, we fail

-        throw new IllegalArgumentException( "Cannot create a component manager for " + metadata.getName() );

-    }

-}
\ No newline at end of file
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ServiceFactoryComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/ServiceFactoryComponentManager.java
index 05db1dd..02e3fb7 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ServiceFactoryComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ServiceFactoryComponentManager.java
@@ -46,9 +46,9 @@
 	 * @param componentId
      */
     public ServiceFactoryComponentManager( BundleComponentActivator activator, ComponentMetadata metadata,
-        long componentId )
+        ComponentRegistry componentRegistry )
     {
-        super( activator, metadata, componentId );
+        super( activator, metadata, componentRegistry );
     }