Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 14587 invoked from network); 21 Aug 2009 11:52:17 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 21 Aug 2009 11:52:17 -0000 Received: (qmail 77185 invoked by uid 500); 21 Aug 2009 11:52:40 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 77133 invoked by uid 500); 21 Aug 2009 11:52:40 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 77121 invoked by uid 99); 21 Aug 2009 11:52:40 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Aug 2009 11:52:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Aug 2009 11:52:37 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9DEF42388878; Fri, 21 Aug 2009 11:52:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r806511 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/config/ main/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/impl/config/ test/java/org/apache/felix/scr/impl/helper/ test/java/org/apache/felix/s... Date: Fri, 21 Aug 2009 11:52:17 -0000 To: commits@felix.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090821115217.9DEF42388878@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: fmeschbe Date: Fri Aug 21 11:52:16 2009 New Revision: 806511 URL: http://svn.apache.org/viewvc?rev=806511&view=rev Log: FELIX-1503 Add API to ComponentHolder to allow ImmediateComponentManager instances to inform their holder about their disposal. Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java (with props) felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java (with props) Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/AbstractComponentHolder.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolder.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/UnconfiguredComponentHolder.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DelayedComponentManager.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ServiceFactoryComponentManager.java felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolderTest.java felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent.java felix/trunk/scr/src/test/resources/integration_test_simple_components.xml Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/AbstractComponentHolder.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/AbstractComponentHolder.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/AbstractComponentHolder.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/AbstractComponentHolder.java Fri Aug 21 11:52:16 2009 @@ -51,17 +51,17 @@ } else if ( m_componentMetadata.isImmediate() ) { - manager = new ImmediateComponentManager( m_activator, m_componentMetadata ); + manager = new ImmediateComponentManager( m_activator, this, m_componentMetadata ); } else if ( m_componentMetadata.getServiceMetadata() != null ) { if ( m_componentMetadata.getServiceMetadata().isServiceFactory() ) { - manager = new ServiceFactoryComponentManager( m_activator, m_componentMetadata ); + manager = new ServiceFactoryComponentManager( m_activator, this, m_componentMetadata ); } else { - manager = new DelayedComponentManager( m_activator, m_componentMetadata ); + manager = new DelayedComponentManager( m_activator, this, m_componentMetadata ); } } else Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java Fri Aug 21 11:52:16 2009 @@ -22,6 +22,7 @@ import java.util.Dictionary; import org.apache.felix.scr.impl.BundleComponentActivator; +import org.apache.felix.scr.impl.manager.ImmediateComponentManager; import org.apache.felix.scr.impl.metadata.ComponentMetadata; @@ -86,4 +87,11 @@ * @param reason */ void disposeComponents( int reason ); + + + /** + * Informs the holder that the component has been disposed as a result of + * calling the dispose method. + */ + void disposed( ImmediateComponentManager component ); } Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolder.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolder.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolder.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolder.java Fri Aug 21 11:52:16 2009 @@ -21,6 +21,7 @@ import java.util.Dictionary; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import org.apache.felix.scr.impl.BundleComponentActivator; @@ -278,6 +279,42 @@ } + public void disposed( ImmediateComponentManager component ) + { + // ensure the component is removed from the components map + synchronized ( m_components ) + { + for ( Iterator vi = m_components.values().iterator(); vi.hasNext(); ) + { + if ( component == vi.next() ) + { + vi.remove(); + break; + } + } + } + + // if the component is the single component, we have to replace it + // by another entry in the map + if ( component == m_singleComponent ) + { + synchronized ( m_components ) + { + if ( m_components.isEmpty() ) + { + // now what ?? + // is it correct to create a new manager ??? + m_singleComponent = createComponentManager(); + } + else + { + m_singleComponent = ( ImmediateComponentManager ) m_components.values().iterator().next(); + } + } + } + } + + //---------- internal private ImmediateComponentManager getComponentManager( String pid ) Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/UnconfiguredComponentHolder.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/UnconfiguredComponentHolder.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/UnconfiguredComponentHolder.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/config/UnconfiguredComponentHolder.java Fri Aug 21 11:52:16 2009 @@ -71,4 +71,10 @@ { m_component.dispose( reason ); } + + + public void disposed( ImmediateComponentManager component ) + { + // nothing to do here... + } } 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=806511&r1=806510&r2=806511&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 Fri Aug 21 11:52:16 2009 @@ -347,6 +347,15 @@ } + public void disposed( ImmediateComponentManager component ) + { + synchronized ( m_componentInstances ) + { + m_componentInstances.remove( component ); + } + } + + //---------- internal /** @@ -357,7 +366,7 @@ */ private ImmediateComponentManager createComponentManager() { - return new ImmediateComponentManager( getActivator(), getComponentMetadata() ); + return new ImmediateComponentManager( getActivator(), this, getComponentMetadata() ); } Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DelayedComponentManager.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DelayedComponentManager.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DelayedComponentManager.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DelayedComponentManager.java Fri Aug 21 11:52:16 2009 @@ -20,6 +20,7 @@ import org.apache.felix.scr.impl.BundleComponentActivator; +import org.apache.felix.scr.impl.config.ComponentHolder; import org.apache.felix.scr.impl.metadata.ComponentMetadata; import org.osgi.framework.Bundle; import org.osgi.framework.ServiceFactory; @@ -36,9 +37,10 @@ * @param activator * @param metadata */ - public DelayedComponentManager( BundleComponentActivator activator, ComponentMetadata metadata ) + public DelayedComponentManager( BundleComponentActivator activator, ComponentHolder componentHolder, + ComponentMetadata metadata ) { - super( activator, metadata ); + super( activator, componentHolder, metadata ); } Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java Fri Aug 21 11:52:16 2009 @@ -24,6 +24,7 @@ import java.util.List; import org.apache.felix.scr.impl.BundleComponentActivator; +import org.apache.felix.scr.impl.config.ComponentHolder; import org.apache.felix.scr.impl.helper.ActivateMethod; import org.apache.felix.scr.impl.helper.DeactivateMethod; import org.apache.felix.scr.impl.helper.ModifiedMethod; @@ -49,6 +50,9 @@ // The context that will be passed to the implementationObject private ComponentContextImpl m_componentContext; + // the component holder responsible for managing this component + private ComponentHolder m_componentHolder; + // the activate method private ActivateMethod m_activateMethod; @@ -75,9 +79,12 @@ * @param activator * @param metadata */ - public ImmediateComponentManager( BundleComponentActivator activator, ComponentMetadata metadata ) + public ImmediateComponentManager( BundleComponentActivator activator, ComponentHolder componentHolder, + ComponentMetadata metadata ) { super( activator, metadata ); + + m_componentHolder = componentHolder; } @@ -92,6 +99,23 @@ } + ComponentHolder getComponentHolder() + { + return m_componentHolder; + } + + + void clear() + { + if ( m_componentHolder != null ) + { + m_componentHolder.disposed( this ); + } + + super.clear(); + } + + // 1. Load the component implementation class // 2. Create the component instance and component context // 3. Bind the target services Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ServiceFactoryComponentManager.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ServiceFactoryComponentManager.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ServiceFactoryComponentManager.java (original) +++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ServiceFactoryComponentManager.java Fri Aug 21 11:52:16 2009 @@ -22,6 +22,7 @@ import java.util.IdentityHashMap; import org.apache.felix.scr.impl.BundleComponentActivator; +import org.apache.felix.scr.impl.config.ComponentHolder; import org.apache.felix.scr.impl.metadata.ComponentMetadata; import org.osgi.framework.Bundle; import org.osgi.framework.ServiceFactory; @@ -46,9 +47,10 @@ * @param activator * @param metadata */ - public ServiceFactoryComponentManager( BundleComponentActivator activator, ComponentMetadata metadata ) + public ServiceFactoryComponentManager( BundleComponentActivator activator, ComponentHolder componentHolder, + ComponentMetadata metadata ) { - super( activator, metadata ); + super( activator, componentHolder, metadata ); } Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolderTest.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolderTest.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolderTest.java (original) +++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/config/ConfiguredComponentHolderTest.java Fri Aug 21 11:52:16 2009 @@ -221,7 +221,7 @@ protected ImmediateComponentManager createComponentManager() { - return new MockImmediateComponentManager( getActivator(), getComponentMetadata() ); + return new MockImmediateComponentManager( getActivator(), this, getComponentMetadata() ); } } @@ -231,9 +231,9 @@ private Dictionary m_configuration; - public MockImmediateComponentManager( BundleComponentActivator activator, ComponentMetadata metadata ) + public MockImmediateComponentManager( BundleComponentActivator activator, ComponentHolder componentHolder, ComponentMetadata metadata ) { - super( activator, metadata ); + super( activator, componentHolder, metadata ); } Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java (original) +++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java Fri Aug 21 11:52:16 2009 @@ -55,7 +55,7 @@ { super.setUp(); - m_ctx = ( ComponentContext ) EasyMock.createNiceMock( ComponentContext.class ); + m_ctx = (ComponentContext) EasyMock.createNiceMock( ComponentContext.class ); EasyMock.expect( m_ctx.getProperties() ).andReturn( new Hashtable() ).anyTimes(); EasyMock.replay( new Object[] { m_ctx } ); @@ -228,7 +228,7 @@ return true; } }; - ImmediateComponentManager icm = new ImmediateComponentManager( null, metadata ); + ImmediateComponentManager icm = new ImmediateComponentManager( null, null, metadata ); ActivateMethod am = new ActivateMethod( icm, methodName, obj.getClass() ); am.invoke( obj, new ActivateMethod.ActivatorParameter( m_ctx, -1 ) ); Method m = get(am, "m_method"); @@ -256,7 +256,7 @@ return true; } }; - ImmediateComponentManager icm = new ImmediateComponentManager( null, metadata ); + ImmediateComponentManager icm = new ImmediateComponentManager( null, null, metadata ); ActivateMethod am = new ActivateMethod( icm, methodName, obj.getClass() ); am.invoke( obj, new ActivateMethod.ActivatorParameter( m_ctx, -1 ) ); assertNull( get( am, "m_method" ) ); Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java (original) +++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java Fri Aug 21 11:52:16 2009 @@ -44,8 +44,8 @@ public void setUp() { - m_serviceReference = ( ServiceReference ) EasyMock.createNiceMock( ServiceReference.class ); - m_serviceInstance = ( FakeService ) EasyMock.createNiceMock( FakeService.class ); + m_serviceReference = (ServiceReference) EasyMock.createNiceMock( ServiceReference.class ); + m_serviceInstance = (FakeService) EasyMock.createNiceMock( FakeService.class ); m_service = new BindMethod.Service() { public ServiceReference getReference() @@ -439,7 +439,7 @@ return isDS11; } }; - ImmediateComponentManager icm = new ImmediateComponentManager( null, metadata ); + ImmediateComponentManager icm = new ImmediateComponentManager( null, null, metadata ); BindMethod bm = new BindMethod( icm, methodName, component.getClass(), "reference", FakeService.class.getName() ); bm.invoke( component, m_service ); Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java?rev=806511&view=auto ============================================================================== --- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java (added) +++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java Fri Aug 21 11:52:16 2009 @@ -0,0 +1,134 @@ +/* + * Copyright 1997-2009 Day Management AG + * Barfuesserplatz 6, 4001 Basel, Switzerland + * All Rights Reserved. + * + * This software is the confidential and proprietary information of + * Day Management AG, ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Day. + */ +package org.apache.felix.scr.integration; + + +import java.lang.reflect.Field; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.felix.scr.Component; +import org.apache.felix.scr.integration.components.SimpleComponent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.junit.JUnit4TestRunner; +import org.osgi.service.component.ComponentContext; + + +@RunWith(JUnit4TestRunner.class) +public class ComponentDisposeTest extends ComponentTestBase +{ + static + { + // uncomment to enable debugging of this test class + // paxRunnerVmOption = DEBUG_VM_OPTION; + } + + + @Test + public void test_SimpleComponent_factory_configuration() + { + final String factoryPid = "FactoryConfigurationComponent"; + + deleteFactoryConfigurations( factoryPid ); + delay(); + + // one single component exists without configuration + final Component[] noConfigurations = findComponentsByName( factoryPid ); + TestCase.assertNotNull( noConfigurations ); + TestCase.assertEquals( 1, noConfigurations.length ); + TestCase.assertEquals( Component.STATE_DISABLED, noConfigurations[0].getState() ); + TestCase.assertTrue( SimpleComponent.INSTANCES.isEmpty() ); + + // enable the component, configuration required, hence unsatisfied + noConfigurations[0].enable(); + delay(); + + final Component[] enabledNoConfigs = findComponentsByName( factoryPid ); + TestCase.assertNotNull( enabledNoConfigs ); + TestCase.assertEquals( 1, enabledNoConfigs.length ); + TestCase.assertEquals( Component.STATE_UNSATISFIED, enabledNoConfigs[0].getState() ); + TestCase.assertTrue( SimpleComponent.INSTANCES.isEmpty() ); + + // create two factory configurations expecting two components + final String pid0 = createFactoryConfiguration( factoryPid ); + final String pid1 = createFactoryConfiguration( factoryPid ); + delay(); + + // expect two components, only first is active, second is disabled + final Component[] twoConfigs = findComponentsByName( factoryPid ); + TestCase.assertNotNull( twoConfigs ); + TestCase.assertEquals( 2, twoConfigs.length ); + TestCase.assertEquals( Component.STATE_ACTIVE, twoConfigs[0].getState() ); + TestCase.assertEquals( Component.STATE_DISABLED, twoConfigs[1].getState() ); + TestCase.assertEquals( 1, SimpleComponent.INSTANCES.size() ); + TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[0].getId() ) ); + TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) ); + + // enable second component + twoConfigs[1].enable(); + delay(); + + // ensure both components active + TestCase.assertEquals( Component.STATE_ACTIVE, twoConfigs[0].getState() ); + TestCase.assertEquals( Component.STATE_ACTIVE, twoConfigs[1].getState() ); + TestCase.assertEquals( 2, SimpleComponent.INSTANCES.size() ); + TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[0].getId() ) ); + TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[1].getId() ) ); + + // dispose an instance + final SimpleComponent anInstance = SimpleComponent.INSTANCE; + TestCase.assertNotNull( anInstance ); + TestCase.assertNotNull( anInstance.m_activateContext ); + anInstance.m_activateContext.getComponentInstance().dispose(); + delay(); + + // expect one component + final Component[] oneConfig = findComponentsByName( factoryPid ); + TestCase.assertNotNull( oneConfig ); + TestCase.assertEquals( 1, oneConfig.length ); + TestCase.assertEquals( Component.STATE_ACTIVE, oneConfig[0].getState() ); + TestCase.assertEquals( 1, SimpleComponent.INSTANCES.size() ); + TestCase.assertTrue( SimpleComponent.INSTANCES.containsKey( twoConfigs[0].getId() ) ); + TestCase.assertFalse( SimpleComponent.INSTANCES.containsKey( anInstance.m_id ) ); + + final SimpleComponent instance = SimpleComponent.INSTANCES.values().iterator().next(); + + final Object holder = getComponentHolder( instance.m_activateContext ); + TestCase.assertNotNull( holder ); + + Map m_components = ( Map ) getFieldValue( holder, "m_components" ); + TestCase.assertNotNull( m_components ); + TestCase.assertEquals( 1, m_components.size() ); + } + + + private static Object getComponentHolder( ComponentContext ctx ) + { + try + { + final Class ccImpl = getType( ctx, "ComponentContextImpl" ); + final Field m_componentManager = getField( ccImpl, "m_componentManager" ); + final Object acm = m_componentManager.get( ctx ); + + final Class cmImpl = getType( acm, "ImmediateComponentManager" ); + final Field m_componentHolder = getField( cmImpl, "m_componentHolder" ); + return m_componentHolder.get( acm ); + } + catch ( Throwable t ) + { + TestCase.fail( "Cannot get ComponentHolder for " + ctx + ": " + t ); + return null; // keep the compiler happy + } + } +} Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentDisposeTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev Url Added: 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=806511&view=auto ============================================================================== --- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java (added) +++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java Fri Aug 21 11:52:16 2009 @@ -0,0 +1,97 @@ +/* + * 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.integration; + + +import java.util.Hashtable; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.felix.scr.Component; +import org.apache.felix.scr.integration.components.SimpleComponent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.junit.JUnit4TestRunner; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.osgi.service.component.ComponentConstants; +import org.osgi.service.component.ComponentFactory; +import org.osgi.service.component.ComponentInstance; + + +@RunWith(JUnit4TestRunner.class) +public class ComponentFactoryTest extends ComponentTestBase +{ + + static + { + // uncomment to enable debugging of this test class + // paxRunnerVmOption = DEBUG_VM_OPTION; + } + + + @Test + public void test_component_factory() 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 props = new Hashtable(); + 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 ) ); + + instance.dispose(); + TestCase.assertNull( SimpleComponent.INSTANCE ); + + TestCase.assertEquals( 0, instanceMap.size() ); + TestCase.assertFalse( instanceMap.containsValue( instance ) ); + } +} Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ComponentFactoryTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev Url 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=806511&r1=806510&r2=806511&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 Fri Aug 21 11:52:16 2009 @@ -41,12 +41,19 @@ private Map m_config; + public long m_id; + + public ComponentContext m_activateContext; + @SuppressWarnings("unused") - private void activate( Map config ) + private void activate( ComponentContext activateContext, Map config ) { + m_id = ( Long ) config.get( ComponentConstants.COMPONENT_ID ); + m_activateContext = activateContext; + INSTANCE = this; - INSTANCES.put( ( Long ) config.get( ComponentConstants.COMPONENT_ID ), this ); + INSTANCES.put( m_id, this ); setConfig( config ); if ( PREVIOUS_INSTANCES.contains( this ) ) @@ -75,6 +82,8 @@ private void deactivate() { INSTANCES.remove( getProperty( ComponentConstants.COMPONENT_ID ) ); + + m_activateContext = null; INSTANCE = null; setConfig( new HashMap() ); } Modified: felix/trunk/scr/src/test/resources/integration_test_simple_components.xml URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/resources/integration_test_simple_components.xml?rev=806511&r1=806510&r2=806511&view=diff ============================================================================== --- felix/trunk/scr/src/test/resources/integration_test_simple_components.xml (original) +++ felix/trunk/scr/src/test/resources/integration_test_simple_components.xml Fri Aug 21 11:52:16 2009 @@ -71,4 +71,11 @@ + + + + +