FELIX-578 Ensure ComponentFactoryImpl.newInstance() enables the created component
synchronously. For components created due to new configuration, the enablement
still is done asynchronously.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@661830 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 87572e0..faa0d17 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
@@ -98,6 +98,31 @@
/**
+ * Enables this component and - if satisfied - also activates it
+ * synchronously or asynchronously. If enabling the component fails for
+ * any reason, the component ends up disabled.
+ * <p>
+ * This method ignores the <i>enabled</i> flag of the component metadata
+ * and just enables as requested.
+ *
+ * @param synchronous If <code>true</code> the component is immediately
+ * enabled synchronously. Otherwise the component enabled is scheduled
+ * for asynchronous enabled by calling {@link #enable()}.
+ */
+ protected final void enable( boolean synchronous )
+ {
+ if ( synchronous )
+ {
+ enableInternal();
+ }
+ else
+ {
+ enable();
+ }
+ }
+
+
+ /**
* Activates this component if satisfied. If any of the dependencies is
* not met, the component is not activated and remains unsatisifed.
* <p>
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 db17352..52c2640 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
@@ -63,7 +63,7 @@
*/
public ComponentInstance newInstance( Dictionary dictionary )
{
- return ( ComponentInstance ) createComponentManager( dictionary );
+ return ( ComponentInstance ) createComponentManager( dictionary, true );
}
@@ -126,7 +126,6 @@
//---------- ManagedServiceFactory interface ------------------------------
-
public void updated( String pid, Dictionary configuration )
{
ComponentManager cm;
@@ -143,7 +142,7 @@
if ( cm == null )
{
// create a new instance with the current configuration
- cm = createComponentManager( configuration );
+ cm = createComponentManager( configuration, false );
// keep a reference for future updates
m_configuredServices.put( pid, cm );
@@ -186,11 +185,10 @@
* with the ComponentRegistry. Therefore, any configuration update to these
* components must be effected by this class !
*/
- private ComponentManager createComponentManager( Dictionary configuration )
+ private ComponentManager createComponentManager( Dictionary configuration, boolean synchronous )
{
long componentId = m_componentRegistry.createComponentId();
- ComponentManager cm = ManagerFactory.createManager( getActivator(), getComponentMetadata(),
- componentId );
+ ComponentManager cm = ManagerFactory.createManager( getActivator(), getComponentMetadata(), componentId );
// add the new component to the activators instances
getActivator().getInstanceReferences().add( cm );
@@ -203,9 +201,16 @@
{
( ( ImmediateComponentManager ) cm ).setFactoryProperties( configuration );
}
-
- // immediately enable this ComponentManager
- cm.enable();
+
+ // enable synchronously or asynchronously depending on the flag
+ if ( cm instanceof AbstractComponentManager )
+ {
+ ( ( AbstractComponentManager ) cm ).enable( synchronous );
+ }
+ else
+ {
+ cm.enable();
+ }
return cm;
}