Fix FELIX-2694 Instance state not recomputed after reconfiguration when the instance is stopped

When the instance is stopped, after the reconfiguration the instance is restarted.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1033647 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
index 77a5403..e8d6210 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
@@ -1158,12 +1158,17 @@
      * @see org.apache.felix.ipojo.ComponentInstance#reconfigure(java.util.Dictionary)
      */
     public void reconfigure(Dictionary configuration) {
+    	 m_factory.getLogger().log(Logger.INFO, "Reconfiguring " + getInstanceName());
         for (int i = 0; i < m_handlers.length; i++) {
             m_handlers[i].getHandler().reconfigure(configuration);
         }
         // We synchronized the state computation.
         synchronized (this) {
-            if (m_state == INVALID) {
+        	if (m_state == STOPPED) {
+        		m_factory.getLogger().log(Logger.INFO, "Instance stopped during reconfiguration - Try to restart");
+        		start();
+        	} else if (m_state == INVALID) {
+        		m_factory.getLogger().log(Logger.INFO, "Instance invalid during reconfiguration - Recompute state");
                 // Try to revalidate the instance after reconfiguration
                 for (int i = 0; i < m_handlers.length; i++) {
                     if (m_handlers[i].getState() != VALID) {
diff --git a/ipojo/tests/core/factories/pom.xml b/ipojo/tests/core/factories/pom.xml
index d0926c1..afd45fe 100644
--- a/ipojo/tests/core/factories/pom.xml
+++ b/ipojo/tests/core/factories/pom.xml
@@ -1,23 +1,15 @@
-<!--

-	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.

--->

+<!-- 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. -->

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 	<parent>

 		<groupId>ipojo.tests</groupId>

 		<artifactId>ipojo.tests</artifactId>

@@ -105,6 +97,23 @@
 					<target>1.4</target>

 				</configuration>

 			</plugin>

+			<plugin>

+				<groupId>org.apache.felix</groupId>

+				<artifactId>maven-junit4osgi-plugin</artifactId>

+				<version>1.1.0-SNAPSHOT</version>

+				<executions>

+					<execution>

+						<goals>

+							<goal>test</goal>

+						</goals>

+						<configuration>

+							<configuration>

+								<org.osgi.http.port>8083</org.osgi.http.port>

+							</configuration>

+						</configuration>

+					</execution>

+				</executions>

+			</plugin>

 		</plugins>

 	</build>

 </project>

diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java
new file mode 100644
index 0000000..81ebd04
--- /dev/null
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java
@@ -0,0 +1,17 @@
+package org.apache.felix.ipojo.test.scenarios.component;
+
+public class ReconfigurableSimpleType {
+
+
+	private String prop; // Property.
+
+	public void start () {
+		if (prop == null || prop.equals("KO")) {
+			throw new IllegalStateException("Bad Configuration : " + prop);
+		}
+		System.out.println("OK !!!!");
+	}
+
+
+
+}
diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java
index 973582c..4338ef3 100644
--- a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java
@@ -1,4 +1,4 @@
-/* 

+/*

  * 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

@@ -32,7 +32,7 @@
         ots.addTestSuite(ConfigAdminTest.class);

         ots.addTestSuite(ObedienceTest.class);

         ots.addTestSuite(FactoryProps.class);

-   //     ots.addTestSuite(EmptyArrayTest.class);

+        ots.addTestSuite(ReconfigurationTest.class);

         return ots;

     }

 

diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java
new file mode 100644
index 0000000..5628b4b
--- /dev/null
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java
@@ -0,0 +1,49 @@
+package org.apache.felix.ipojo.test.scenarios.factories;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.MissingHandlerException;
+import org.apache.felix.ipojo.UnacceptableConfiguration;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+
+public class ReconfigurationTest extends OSGiTestCase {
+
+
+	public void testRevalidationOnREconfiguration() {
+		ComponentFactory factory = (ComponentFactory) Utils.getFactoryByName(getContext(),
+				"org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType");
+
+		// First inject a configuration triggering an exception of the validate method.
+		Properties props = new Properties();
+		props.put("prop", "KO");
+		ComponentInstance ci = null;
+		try {
+			ci = factory.createComponentInstance(props);
+		} catch (UnacceptableConfiguration e) {
+			e.printStackTrace();
+		} catch (MissingHandlerException e) {
+			e.printStackTrace();
+		} catch (ConfigurationException e) {
+			e.printStackTrace();
+		}
+
+		assertNotNull(ci);
+		assertEquals("instance invalid", ComponentInstance.STOPPED, ci.getState());
+
+		// Reconfigure
+		props = new Properties();
+		props.put("prop", "OK");
+
+		ci.reconfigure(props);
+
+		assertNotNull(ci);
+		assertEquals("instance valid", ComponentInstance.VALID, ci.getState());
+	}
+
+
+
+}
diff --git a/ipojo/tests/core/factories/src/main/resources/metadata.xml b/ipojo/tests/core/factories/src/main/resources/metadata.xml
index ad57539..24be561 100644
--- a/ipojo/tests/core/factories/src/main/resources/metadata.xml
+++ b/ipojo/tests/core/factories/src/main/resources/metadata.xml
@@ -1,5 +1,5 @@
 <ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

-	xsi:schemaLocation="org.apache.felix.ipojo hhttp://felix.apache.org/ipojo/schemas/SNAPSHOT/core.xsd" 

+	xsi:schemaLocation="org.apache.felix.ipojo hhttp://felix.apache.org/ipojo/schemas/SNAPSHOT/core.xsd"

 	xmlns="org.apache.felix.ipojo">

 	<!-- Simple provider  -->

 	<component

@@ -7,14 +7,14 @@
 		name="Factories-FooProviderType-1" architecture="true">

 		<provides />

 	</component>

-	

+

 	<!-- Provider providing 2 services -->

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooBarProviderType1"

 		name="Factories-FooBarProviderType-1" architecture="true">

 		<provides />

 	</component>

-	

+

 	<!-- Provider with dynamic property -->

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn"

@@ -28,7 +28,7 @@
 			<property name="intAProp" field="intAProp" value="{ 1,2,3}" mandatory="true"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn"

 		name="Factories-FooProviderType-Dynopt" architecture="true">

@@ -41,7 +41,7 @@
 			<property name="intAProp" field="intAProp" value="{ 1,2,3}"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"

 		name="Factories-FooProviderType-2" architecture="true">

@@ -54,7 +54,7 @@
 			<property name="intAProp" type="int[]" value="{1,2,3}" mandatory="true"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"

 		name="Factories-FooProviderType-2opt" architecture="true">

@@ -67,7 +67,7 @@
 			<property name="intAProp" type="int[]" value="{1,2,3}"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn2"

 		name="Factories-FooProviderType-Dyn2" architecture="true">

@@ -80,7 +80,7 @@
 				value="{1, 2,3 }" mandatory="true"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn2"

 		name="Factories-FooProviderType-Dyn2opt" architecture="true">

@@ -93,7 +93,7 @@
 				value="{1, 2,3 }"/>

 		</provides>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"

 		name="Factories-FooProviderType-3" architecture="true">

@@ -107,7 +107,7 @@
 			<property name="bar" field="m_bar" mandatory="true"/>

 		</properties>

 	</component>

-	

+

 	<component

 		classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"

 		name="Factories-FooProviderType-3opt" architecture="true">

@@ -121,11 +121,18 @@
 			<property name="bar" field="m_bar"/>

 		</properties>

 	</component>

-	

+

 	<!-- type & instance used to check instance lifecycle against factory validation & invalidation -->

 	<component classname="org.apache.felix.ipojo.test.scenarios.component.SimpleType" architecture="true">

 		<controller field="m_controller"/>

 	</component>

 	<instance component="org.apache.felix.ipojo.test.scenarios.component.SimpleType" name="SimpleInstance"/>

-	

+

+	<!-- check that instance state is recomputed after reconfiguration -->

+	<component classname="org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType" architecture="true">

+		<properties>

+			<property name="prop" field="prop"/>

+		</properties>

+		<callback transition="validate" method="start"/>

+	</component>

 </ipojo>