felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r830994 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/integration/ test/java/org/apache/felix/scr/integration/components/
Date Thu, 29 Oct 2009 15:10:51 GMT
Author: fmeschbe
Date: Thu Oct 29 15:10:50 2009
New Revision: 830994

URL: http://svn.apache.org/viewvc?rev=830994&view=rev
Log:
FELIX-1826 Throw ComponentException if newInstance fails to create a
component instance (for any one reason, which is logged actually)

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java?rev=830994&r1=830993&r2=830994&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
Thu Oct 29 15:10:50 2009
@@ -30,6 +30,7 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentConstants;
+import org.osgi.service.component.ComponentException;
 import org.osgi.service.component.ComponentFactory;
 import org.osgi.service.component.ComponentInstance;
 import org.osgi.service.log.LogService;
@@ -110,8 +111,14 @@
         cm.activateInternal();
 
         final ComponentInstance instance = cm.getComponentInstance();
-        m_componentInstances.put( cm, instance );
+        if ( instance == null )
+        {
+            // activation failed, clean up component manager
+            cm.dispose();
+            throw new ComponentException( "Failed activating component" );
+        }
 
+        m_componentInstances.put( cm, instance );
         return instance;
     }
 

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java?rev=830994&r1=830993&r2=830994&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
(original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java
Thu Oct 29 15:10:50 2009
@@ -32,6 +32,7 @@
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentConstants;
+import org.osgi.service.component.ComponentException;
 import org.osgi.service.component.ComponentFactory;
 import org.osgi.service.component.ComponentInstance;
 
@@ -94,4 +95,110 @@
         TestCase.assertEquals( 0, instanceMap.size() );
         TestCase.assertFalse( instanceMap.containsValue( instance ) );
     }
+
+
+    @Test
+    public void test_component_factory_disable_factory() throws InvalidSyntaxException
+    {
+        // tests components remain alive after factory has been disabled
+
+        final String componentname = "factory.component";
+        final String componentfactory = "factory.component.factory";
+
+        final Component component = findComponentByName( componentname );
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        final ServiceReference[] refs = bundleContext.getServiceReferences( ComponentFactory.class.getName(),
"("
+            + ComponentConstants.COMPONENT_FACTORY + "=" + componentfactory + ")" );
+        TestCase.assertNotNull( refs );
+        TestCase.assertEquals( 1, refs.length );
+        final ComponentFactory factory = ( ComponentFactory ) bundleContext.getService( refs[0]
);
+        TestCase.assertNotNull( factory );
+
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put( PROP_NAME, PROP_NAME );
+        final ComponentInstance instance = factory.newInstance( props );
+        TestCase.assertNotNull( instance );
+
+        TestCase.assertNotNull( instance.getInstance() );
+        TestCase.assertEquals( SimpleComponent.INSTANCE, instance.getInstance() );
+        TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME
) );
+
+        final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component,
"m_componentInstances" );
+        TestCase.assertNotNull( instanceMap );
+        TestCase.assertEquals( 1, instanceMap.size() );
+        TestCase.assertTrue( instanceMap.containsValue( instance ) );
+
+        // disable the factory
+        component.disable();
+        delay();
+
+        // factory is disabled but the instance is still alive
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNotNull( SimpleComponent.INSTANCE );
+        TestCase.assertEquals( 1, instanceMap.size() );
+        TestCase.assertTrue( instanceMap.containsValue( instance ) );
+
+        instance.dispose();
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        TestCase.assertEquals( 0, instanceMap.size() );
+        TestCase.assertFalse( instanceMap.containsValue( instance ) );
+    }
+
+
+    @Test
+    public void test_component_factory_newInstance_failure() throws InvalidSyntaxException
+    {
+        final String componentname = "factory.component";
+        final String componentfactory = "factory.component.factory";
+
+        final Component component = findComponentByName( componentname );
+
+        TestCase.assertNotNull( component );
+        TestCase.assertFalse( component.isDefaultEnabled() );
+
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+
+        final ServiceReference[] refs = bundleContext.getServiceReferences( ComponentFactory.class.getName(),
"("
+            + ComponentConstants.COMPONENT_FACTORY + "=" + componentfactory + ")" );
+        TestCase.assertNotNull( refs );
+        TestCase.assertEquals( 1, refs.length );
+        final ComponentFactory factory = ( ComponentFactory ) bundleContext.getService( refs[0]
);
+        TestCase.assertNotNull( factory );
+
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put( PROP_NAME, PROP_NAME );
+        props.put( SimpleComponent.PROP_ACTIVATE_FAILURE, "Requested Failure" );
+        try {
+            final ComponentInstance instance = factory.newInstance( props );
+            TestCase.assertNotNull( instance );
+            TestCase.fail("Expected newInstance method to fail with ComponentException");
+        } catch (ComponentException ce) {
+            // this is expected !
+        }
+
+        final Map<?, ?> instanceMap = ( Map<?, ?> ) getFieldValue( component,
"m_componentInstances" );
+        TestCase.assertNotNull( instanceMap );
+        TestCase.assertTrue( instanceMap.isEmpty() );
+        TestCase.assertNull( SimpleComponent.INSTANCE );
+    }
 }

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java?rev=830994&r1=830993&r2=830994&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
(original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java
Thu Oct 29 15:10:50 2009
@@ -33,12 +33,16 @@
 public class SimpleComponent
 {
 
-    public static SimpleComponent INSTANCE;
+    // component configuration property whose existence causes the
+    // activate method to fail
+    public static final String PROP_ACTIVATE_FAILURE = "request.activation.failure";
 
     public static final Map<Long, SimpleComponent> INSTANCES = new HashMap<Long,
SimpleComponent>();
 
     public static final Set<SimpleComponent> PREVIOUS_INSTANCES = new HashSet<SimpleComponent>();
 
+    public static SimpleComponent INSTANCE;
+
     private Map<?, ?> m_config;
 
     public long m_id;
@@ -53,6 +57,12 @@
     @SuppressWarnings("unused")
     private void activate( ComponentContext activateContext, Map<?, ?> config )
     {
+        // fail activation if requested so
+        if ( config.containsKey( PROP_ACTIVATE_FAILURE ) )
+        {
+            throw new RuntimeException( String.valueOf( config.get( PROP_ACTIVATE_FAILURE
) ) );
+        }
+
         m_id = ( Long ) config.get( ComponentConstants.COMPONENT_ID );
         m_activateContext = activateContext;
 



Mime
View raw message