You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2013/04/29 14:59:53 UTC
svn commit: r1477027 [2/3] - in /felix/trunk/ipojo/runtime:
core-it/src/it/ipojo-core-configuration-admin-test/
core-it/src/it/ipojo-core-configuration-admin-test/src/
core-it/src/it/ipojo-core-configuration-admin-test/src/main/
core-it/src/it/ipojo-co...
Added: felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestManagedServiceTestForService.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestManagedServiceTestForService.java?rev=1477027&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestManagedServiceTestForService.java (added)
+++ felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-admin-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestManagedServiceTestForService.java Mon Apr 29 12:59:52 2013
@@ -0,0 +1,393 @@
+/*
+ * 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.ipojo.runtime.core;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.PrimitiveInstanceDescription;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.runtime.core.services.FooService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+public class TestManagedServiceTestForService extends Common {
+
+ private String factNameSvc = "CA-ConfigurableProvider";
+ private String msp = "foo";
+
+ private ComponentFactory factSvc;
+
+ private ConfigurationAdmin admin;
+
+ ConfigurationMonitor listener;
+
+ @Before
+ public void setUp() {
+ factSvc = (ComponentFactory) ipojoHelper.getFactory(factNameSvc);
+ admin = (ConfigurationAdmin) osgiHelper.getServiceObject(ConfigurationAdmin.class.getName(), null);
+ assertNotNull("Check configuration admin availability", admin);
+ cleanConfigurationAdmin();
+ listener = new ConfigurationMonitor(bc);
+ }
+
+ @After
+ public void tearDown() {
+ listener.stop();
+ cleanConfigurationAdmin();
+ admin = null;
+ }
+
+ private void cleanConfigurationAdmin() {
+ try {
+ Configuration[] configurations = admin.listConfigurations("(service.pid=" + msp + ")");
+ for (int i = 0; configurations != null && i < configurations.length; i++) {
+ configurations[i].delete();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testCreationUsingFactoryAndReconfigurationUsingManagedService() {
+ Properties props = new Properties();
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ ComponentInstance instance = null;
+ try {
+ instance = factSvc.createComponentInstance(props);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ FooService fs = (FooService) bc.getService(ref);
+ Properties p = fs.fooProps();
+ String mes = p.getProperty("message");
+ int count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message", mes);
+ assertEquals("Check count", 1, count);
+
+ //Update
+ Configuration configuration;
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message2");
+ configuration.update(prc);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ fs = (FooService) bc.getService(ref);
+ p = fs.fooProps();
+ mes = p.getProperty("message");
+ count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ if (mes.equals("message")) {
+ System.out.println("Warning, configuration not yet applied");
+ assertEquals("Check count - W", 1, count);
+ } else {
+ assertEquals("Check message", "message2", mes);
+ assertEquals("Check count", 2, count);
+ }
+
+ instance.dispose();
+
+ }
+
+ @Test
+ public void testCreationUsingMSFAndReconfigurationUsingManagedService() {
+ Configuration conf = null;
+ try {
+ conf = admin.createFactoryConfiguration(factNameSvc, getTestBundle().getLocation());
+ Dictionary props = conf.getProperties();
+ if (props == null) {
+ props = new Properties();
+ }
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ conf.update(props);
+ Thread.sleep(UPDATE_WAIT_TIME); // Wait for the creation.
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ Architecture arch = (Architecture) osgiHelper.getServiceObject(org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
+
+ ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), conf.getPid());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
+ FooService fs = (FooService) bc.getService(ref);
+ Properties p = fs.fooProps();
+ String mes = p.getProperty("message");
+ int count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message", mes);
+ assertEquals("Check count", 1, count);
+
+ //Update
+ Configuration configuration;
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message2");
+ configuration.update(prc);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
+ ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), conf.getPid());
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ // arch = (Architecture) osgiHelper.getServiceObject( org.apache.felix.ipojo.architecture.Architecture.class.getName(), "(architecture.instance=" + conf.getPid() + ")");
+ fs = (FooService) bc.getService(ref);
+ p = fs.fooProps();
+ mes = p.getProperty("message");
+ count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) arch.getInstanceDescription()).getCreatedObjects().length);
+ if (mes.equals("message")) {
+ System.out.println("Warning, configuration not yet applied");
+ assertEquals("Check count - W", 1, count);
+ } else {
+ assertEquals("Check message", "message2", mes);
+ assertEquals("Check count", 2, count);
+ }
+
+ try {
+ conf.delete();
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+
+ }
+
+ @Test
+ public void testConfigurationPushedBeforeInstantiationUsingFactory() {
+ // The configuration exists before the instance creation.
+
+ //Update
+ Configuration configuration;
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message2");
+ configuration.update(prc);
+ //listener.waitForEvent(msp, "1");
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ Properties props = new Properties();
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ ComponentInstance instance = null;
+ try {
+ instance = factSvc.createComponentInstance(props);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ FooService fs = (FooService) bc.getService(ref);
+ Properties p = fs.fooProps();
+ String mes = p.getProperty("message");
+ int count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message2", mes); // Already reconfigured.
+ assertEquals("Check count", 1, count);
+
+ instance.dispose();
+
+ //Reconfiguration
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message3");
+ configuration.update(prc);
+ listener.waitForEvent(msp, "2");
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ // Recreation of the instance.
+ props = new Properties();
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ instance = null;
+ try {
+ instance = factSvc.createComponentInstance(props);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ fs = (FooService) bc.getService(ref);
+ p = fs.fooProps();
+ mes = p.getProperty("message");
+ count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message3", mes); // Already reconfigured.
+ assertEquals("Check count", 1, count);
+
+ instance.dispose();
+
+
+ }
+
+ @Test
+ public void testConfigurationPushedBeforeInstantiationUsingFactoryAndReconfiguration() {
+ // The configuration exists before the instance creation.
+
+ //Update
+ Configuration configuration;
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message2");
+ configuration.update(prc);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ Properties props = new Properties();
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ ComponentInstance instance = null;
+ try {
+ instance = factSvc.createComponentInstance(props);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ServiceReference ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ FooService fs = (FooService) bc.getService(ref);
+ Properties p = fs.fooProps();
+ String mes = p.getProperty("message");
+ int count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message2", mes); // Already reconfigured.
+ assertEquals("Check count", 1, count);
+
+ //Reconfiguration
+ try {
+ configuration = admin.getConfiguration(msp, getTestBundle().getLocation());
+ Dictionary prc = configuration.getProperties();
+ if (prc == null) {
+ prc = new Properties();
+ }
+ prc.put("message", "message3");
+ configuration.update(prc);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ instance.dispose();
+
+ // Recreation of the instance.
+ props = new Properties();
+ props.put("managed.service.pid", msp);
+ props.put("message", "message");
+ instance = null;
+ try {
+ instance = factSvc.createComponentInstance(props);
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ assertEquals("Check no object", 0, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertNotNull("FS availability", ref);
+
+ fs = (FooService) bc.getService(ref);
+ p = fs.fooProps();
+ mes = p.getProperty("message");
+ count = (Integer) p.get("count");
+ assertEquals("Check 1 object", 1, ((PrimitiveInstanceDescription) instance.getInstanceDescription()).getCreatedObjects().length);
+ assertEquals("Check message", "message3", mes); // Already reconfigured.
+ assertEquals("Check count", 1, count);
+
+ instance.dispose();
+
+
+ }
+
+
+}
Modified: felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java?rev=1477027&r1=1477026&r2=1477027&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java (original)
+++ felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java Mon Apr 29 12:59:52 2013
@@ -109,7 +109,12 @@ public class Common {
public CompositeOption ipojoBundles() {
return new DefaultCompositeOption(
mavenBundle("org.apache.felix", "org.apache.felix.ipojo").versionAsInProject(),
- mavenBundle("org.ow2.chameleon.testing", "osgi-helpers").versionAsInProject());
+ mavenBundle("org.ow2.chameleon.testing", "osgi-helpers").versionAsInProject(),
+ mavenBundle("org.apache.felix", "org.apache.felix.configadmin").versionAsInProject());
+ }
+
+ public Bundle getTestBundle() {
+ return osgiHelper.getBundle("test.bundle");
}
public Option testedBundle() throws MalformedURLException {
Copied: felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurablePropertiesUsingConfigAdmin.java (from r1471456, felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurableProperties.java)
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurablePropertiesUsingConfigAdmin.java?p2=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurablePropertiesUsingConfigAdmin.java&p1=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurableProperties.java&r1=1471456&r2=1477027&rev=1477027&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurableProperties.java (original)
+++ felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestDynamicallyConfigurablePropertiesUsingConfigAdmin.java Mon Apr 29 12:59:52 2013
@@ -23,10 +23,16 @@ import org.apache.felix.ipojo.runtime.co
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
+import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
@@ -34,7 +40,12 @@ import static junit.framework.Assert.ass
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
-public class TestDynamicallyConfigurableProperties extends Common {
+/**
+ * iPOJO does not expose the ManagedServiceFactory anymore, we must use the configuration admin.
+ * To avoid conflicts with persisted configuration, we run one framework per tests
+ */
+@ExamReactorStrategy(PerMethod.class)
+public class TestDynamicallyConfigurablePropertiesUsingConfigAdmin extends Common {
ComponentInstance instance, instance2;
@@ -64,7 +75,7 @@ public class TestDynamicallyConfigurable
}
@Test
- public void testStatic() {
+ public void testStatic() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
String fooP = (String) fooRef.getProperty("foo");
@@ -74,21 +85,23 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality -1", barP, new Integer(2));
assertEquals("Check baz equality -1", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+ conf.put("instance.name", instance.getInstanceName());
+
+ configuration.update(conf);
+
+ // Asynchronous dispatching of the configuration
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -98,12 +111,11 @@ public class TestDynamicallyConfigurable
assertEquals("Check foo equality -2", fooP, "foo");
assertEquals("Check bar equality -2", barP, new Integer(2));
assertEquals("Check baz equality -2", bazP, "zab");
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testStaticNoValue() {
+ public void testStaticNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
Object fooP = fooRef.getProperty("foo");
@@ -113,21 +125,21 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -137,11 +149,10 @@ public class TestDynamicallyConfigurable
assertEquals("Check foo equality -2", fooP, "foo");
assertEquals("Check bar equality -2", barP, new Integer(2));
assertEquals("Check baz equality -2", bazP, "zab");
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testDynamic() {
+ public void testDynamic() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -153,20 +164,21 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -188,11 +200,10 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar field equality", barP, new Integer(0));
osgiHelper.getContext().ungetService(fooRef);
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testDynamicNoValue() {
+ public void testDynamicNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -203,20 +214,21 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -238,12 +250,12 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar field equality", barP, new Integer(0));
osgiHelper.getContext().ungetService(fooRef);
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testDynamicString() {
- ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
+ public void testDynamicString() throws IOException, InterruptedException {
+ ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(),
+ instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
String fooP = (String) fooRef.getProperty("foo");
@@ -254,20 +266,21 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", "0");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -289,11 +302,10 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar field equality", barP, new Integer(0));
osgiHelper.getContext().ungetService(fooRef);
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testPropagation() {
+ public void testPropagation() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -305,8 +317,11 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
@@ -315,12 +330,10 @@ public class TestDynamicallyConfigurable
conf.put("bar", new Integer(2));
conf.put("propagated1", "propagated");
conf.put("propagated2", new Integer(1));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -337,12 +350,10 @@ public class TestDynamicallyConfigurable
assertEquals("Check baz equality", bazP, "zab");
assertEquals("Check propagated1 equality", prop1, "propagated");
assertEquals("Check propagated2 equality", prop2, new Integer(1));
-
- osgiHelper.getContext().ungetService(msRef);
}
@Test
- public void testPropagationNoValue() {
+ public void testPropagationNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -353,8 +364,11 @@ public class TestDynamicallyConfigurable
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
@@ -363,12 +377,10 @@ public class TestDynamicallyConfigurable
conf.put("bar", new Integer(2));
conf.put("propagated1", "propagated");
conf.put("propagated2", new Integer(1));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getContext().getService(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -385,8 +397,6 @@ public class TestDynamicallyConfigurable
assertEquals("Check baz equality", bazP, "zab");
assertEquals("Check propagated1 equality", prop1, "propagated");
assertEquals("Check propagated2 equality", prop2, new Integer(1));
-
- osgiHelper.getContext().ungetService(msRef);
}
}
Copied: felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndConfigAdmin.java (from r1471456, felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndManagedServiceFactory.java)
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndConfigAdmin.java?p2=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndConfigAdmin.java&p1=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndManagedServiceFactory.java&r1=1471456&r2=1477027&rev=1477027&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndManagedServiceFactory.java (original)
+++ felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedMethodAndConfigAdmin.java Mon Apr 29 12:59:52 2013
@@ -24,12 +24,17 @@ import org.apache.felix.ipojo.runtime.co
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
import org.ow2.chameleon.testing.helpers.IPOJOHelper;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
+import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;
@@ -37,14 +42,11 @@ import java.util.Properties;
import static junit.framework.Assert.*;
-public class TestUpdatedMethodAndManagedServiceFactory extends Common {
-
-
+@ExamReactorStrategy(PerMethod.class)
+public class TestUpdatedMethodAndConfigAdmin extends Common {
ComponentInstance instance, instance2;
-
-
@Before
public void setUp() {
osgiHelper = new OSGiHelper(bc);
@@ -74,7 +76,7 @@ public class TestUpdatedMethodAndManaged
}
@Test
- public void testStatic() {
+ public void testStatic() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -85,21 +87,21 @@ public class TestUpdatedMethodAndManaged
assertEquals("Check bar equality -1", barP, new Integer(2));
assertEquals("Check baz equality -1", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -121,7 +123,7 @@ public class TestUpdatedMethodAndManaged
}
@Test
- public void testStaticNoValue() {
+ public void testStaticNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
Object fooP = fooRef.getProperty("foo");
@@ -131,21 +133,21 @@ public class TestUpdatedMethodAndManaged
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -166,7 +168,7 @@ public class TestUpdatedMethodAndManaged
}
@Test
- public void testDynamic() {
+ public void testDynamic() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -178,20 +180,21 @@ public class TestUpdatedMethodAndManaged
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -220,7 +223,7 @@ public class TestUpdatedMethodAndManaged
}
@Test
- public void testDynamicNoValue() {
+ public void testDynamicNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -231,20 +234,21 @@ public class TestUpdatedMethodAndManaged
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -275,7 +279,7 @@ public class TestUpdatedMethodAndManaged
@Test
- public void testDynamicString() {
+ public void testDynamicString() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -287,20 +291,21 @@ public class TestUpdatedMethodAndManaged
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", "0");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
Copied: felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndConfigAdmin.java (from r1471456, felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndManagedServiceFactory.java)
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndConfigAdmin.java?p2=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndConfigAdmin.java&p1=felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndManagedServiceFactory.java&r1=1471456&r2=1477027&rev=1477027&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndManagedServiceFactory.java (original)
+++ felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-configuration-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestUpdatedNoArgMethodAndConfigAdmin.java Mon Apr 29 12:59:52 2013
@@ -24,19 +24,24 @@ import org.apache.felix.ipojo.runtime.co
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
import org.ow2.chameleon.testing.helpers.IPOJOHelper;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
+import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
-import static org.junit.Assert.*;
+import static junit.framework.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
-public class TestUpdatedNoArgMethodAndManagedServiceFactory extends Common {
+@ExamReactorStrategy(PerMethod.class)
+public class TestUpdatedNoArgMethodAndConfigAdmin extends Common {
@@ -72,7 +77,7 @@ public class TestUpdatedNoArgMethodAndMa
}
@Test
- public void testStatic() {
+ public void testStatic() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -83,21 +88,21 @@ public class TestUpdatedNoArgMethodAndMa
assertEquals("Check bar equality -1", barP, new Integer(2));
assertEquals("Check baz equality -1", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -117,7 +122,7 @@ public class TestUpdatedNoArgMethodAndMa
}
@Test
- public void testStaticNoValue() {
+ public void testStaticNoValue() throws InterruptedException, IOException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
Object fooP = fooRef.getProperty("foo");
@@ -127,21 +132,21 @@ public class TestUpdatedNoArgMethodAndMa
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("bar", new Integer(2));
conf.put("foo", "foo");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -160,7 +165,7 @@ public class TestUpdatedNoArgMethodAndMa
}
@Test
- public void testDynamic() {
+ public void testDynamic() throws InterruptedException, IOException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -172,20 +177,21 @@ public class TestUpdatedNoArgMethodAndMa
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
@@ -212,7 +218,7 @@ public class TestUpdatedNoArgMethodAndMa
}
@Test
- public void testDynamicNoValue() {
+ public void testDynamicNoValue() throws IOException, InterruptedException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -223,20 +229,21 @@ public class TestUpdatedNoArgMethodAndMa
assertEquals("Check bar equality -1", barP, null);
assertEquals("Check baz equality -1", bazP, null);
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance2.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance2.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", new Integer(0));
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance2.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
@@ -264,7 +271,7 @@ public class TestUpdatedNoArgMethodAndMa
@Test
- public void testDynamicString() {
+ public void testDynamicString() throws InterruptedException, IOException {
ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
assertNotNull("Check FS availability", fooRef);
@@ -276,20 +283,21 @@ public class TestUpdatedNoArgMethodAndMa
assertEquals("Check bar equality", barP, new Integer(2));
assertEquals("Check baz equality", bazP, "baz");
- ServiceReference msRef = ipojoHelper.getServiceReferenceByName(ManagedServiceFactory.class.getName(), instance.getFactory().getName());
- assertNotNull("Check ManagedServiceFactory availability", msRef);
+ ConfigurationAdmin admin = osgiHelper.getServiceObject(ConfigurationAdmin.class);
+ assertNotNull("Check Configuration Admin availability", admin);
+
+ Configuration configuration = admin.getConfiguration(instance.getInstanceName(),
+ getTestBundle().getLocation());
// Configuration of baz
Properties conf = new Properties();
conf.put("baz", "zab");
conf.put("foo", "oof");
conf.put("bar", "0");
- ManagedServiceFactory ms = (ManagedServiceFactory) osgiHelper.getServiceObject(msRef);
- try {
- ms.updated(instance.getInstanceName(), conf);
- } catch (ConfigurationException e) {
- fail("Configuration Exception : " + e);
- }
+
+ // Asynchronous dispatching of the configuration
+ configuration.update(conf);
+ Thread.sleep(200);
// Recheck props
fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance.getInstanceName());
Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java?rev=1477027&r1=1477026&r2=1477027&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/ComponentFactory.java Mon Apr 29 12:59:52 2013
@@ -32,15 +32,13 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
-import java.net.URL;
import java.security.ProtectionDomain;
import java.util.*;
/**
* The component factory manages component instance objects. This management
* consists to create and manage component instances build with the current
- * component factory. This class could export Factory and ManagedServiceFactory
- * services.
+ * component factory. If the factory is public a {@see Factory} service is exposed.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
* @see IPojoFactory
@@ -182,16 +180,10 @@ public class ComponentFactory extends IP
} catch (ConfigurationException e) {
// An exception occurs while executing the configure or start
// methods.
- if (instance != null) {
- instance.dispose();
- instance = null;
- }
+ instance.dispose();
throw e;
} catch (Throwable e) { // All others exception are handled here.
- if (instance != null) {
- instance.dispose();
- instance = null;
- }
+ instance.dispose();
m_logger.log(Logger.ERROR, e.getMessage(), e);
throw new ConfigurationException(e.getMessage());
}
@@ -202,7 +194,7 @@ public class ComponentFactory extends IP
* Defines a class.
* This method needs to be synchronized to avoid that the classloader
* is created twice.
- * This method delegate the <code>define</code> method invocation to the
+ * This method delegates the <code>define</code> method invocation to the
* factory classloader.
*
* @param name the qualified name of the class
@@ -210,32 +202,18 @@ public class ComponentFactory extends IP
* @param domain the protection domain of the class
* @return the defined class object
*/
- public synchronized Class defineClass(String name, byte[] clazz, ProtectionDomain domain) {
+ public synchronized Class<? extends Object> defineClass(String name, byte[] clazz, ProtectionDomain domain) {
if (!m_useFactoryClassloader) {
m_logger.log(Log.WARNING, "A class definition was required even without the factory classloader enabled");
}
if (m_classLoader == null) {
- m_classLoader = new FactoryClassloader();
+ m_classLoader = new FactoryClassloader(this);
}
return m_classLoader.defineClass(name, clazz, domain);
}
/**
- * Returns the URL of a resource.
- * This methods delegates the invocation to the
- * {@link Bundle#getResource(String)} method.
- *
- * @param resName the resource name
- * @return the URL of the resource
- */
- public URL getResource(String resName) {
- //No synchronization needed, the context is immutable and
- //the call is managed by the underlying framework.
- return m_context.getBundle().getResource(resName);
- }
-
- /**
* Loads a class. This method checks if the class
* to load is the implementation class or not.
* If it is, the factory classloader is used, else
@@ -260,9 +238,7 @@ public class ComponentFactory extends IP
* This method is not called when holding the monitor lock.
*/
public void starting() {
- if (m_tracker != null) {
- return; // Already started
- } else {
+ if (m_tracker == null) {
if (m_requiredHandlers.size() != 0) {
try {
String filter = "(&(" + Handler.HANDLER_TYPE_PROPERTY + "=" + PrimitiveHandler.HANDLER_TYPE + ")" + "(factory.state=1)" + ")";
@@ -274,6 +250,7 @@ public class ComponentFactory extends IP
}
}
}
+ // Else, the tracking has already started.
}
/**
@@ -314,10 +291,9 @@ public class ComponentFactory extends IP
* @return the required handler list.
*/
public List getRequiredHandlerList() {
- List list = new ArrayList();
+ List<RequiredHandler> list = new ArrayList<RequiredHandler>();
Element[] elems = m_componentMetadata.getElements();
- for (int i = 0; i < elems.length; i++) {
- Element current = elems[i];
+ for (Element current : elems) {
if (!"manipulation".equals(current.getName())) { // Remove the manipulation element
RequiredHandler req = new RequiredHandler(current.getName(), current.getNameSpace());
if (!list.contains(req)) {
@@ -359,8 +335,8 @@ public class ComponentFactory extends IP
String v = System.getProperty(HANDLER_AUTO_PRIMITIVE);
if (v != null && v.length() != 0) {
String[] hs = ParseUtils.split(v, ",");
- for (int i = 0; i < hs.length; i++) {
- String h = hs[i].trim();
+ for (String h1 : hs) {
+ String h = h1.trim();
String[] segments = ParseUtils.split(h, ":");
RequiredHandler rq = null;
if (segments.length == 2) { // External handler
@@ -378,7 +354,6 @@ public class ComponentFactory extends IP
}
}
-
return list;
}
@@ -434,8 +409,8 @@ public class ComponentFactory extends IP
*/
public synchronized void removedService(ServiceReference reference, Object service) {
// Look for the implied reference and invalid the handler identifier
- for (int i = 0; i < m_requiredHandlers.size(); i++) {
- RequiredHandler req = (RequiredHandler) m_requiredHandlers.get(i);
+ for (Object m_requiredHandler : m_requiredHandlers) {
+ RequiredHandler req = (RequiredHandler) m_requiredHandler;
if (reference.equals(req.getReference())) {
req.unRef(); // This method will unget the service.
computeFactoryState();
@@ -480,251 +455,4 @@ public class ComponentFactory extends IP
return m_classLoader;
}
- /**
- * this class defines the classloader attached to a factory.
- * This class loader is used to load the implementation (e.g. manipulated)
- * class.
- *
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- * @see ClassLoader
- */
- private class FactoryClassloader extends ClassLoader {
-
- /**
- * The map of defined classes [Name, Class Object].
- */
- private final Map m_definedClasses = new HashMap();
-
- /**
- * The defineClass method.
- *
- * @param name name of the class
- * @param clazz the byte array of the class
- * @param domain the protection domain
- * @return the defined class.
- */
- public Class defineClass(String name, byte[] clazz, ProtectionDomain domain) {
- if (m_definedClasses.containsKey(name)) {
- return (Class) m_definedClasses.get(name);
- }
- Class clas = super.defineClass(name, clazz, 0, clazz.length, domain);
- m_definedClasses.put(name, clas);
- return clas;
- }
-
- /**
- * Returns the URL of the required resource.
- *
- * @param arg the name of the resource to find.
- * @return the URL of the resource.
- * @see java.lang.ClassLoader#getResource(java.lang.String)
- */
- public URL getResource(String arg) {
- return m_context.getBundle().getResource(arg);
- }
-
- /**
- * Loads the given class.
- *
- * @param name the name of the class
- * @param resolve should be the class resolve now ?
- * @return the loaded class object
- * @throws ClassNotFoundException if the class to load is not found
- * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
- * @see java.lang.ClassLoader#loadClass(String, boolean)
- */
- protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
- return m_context.getBundle().loadClass(name);
- }
- }
-
- /**
- * This class defines the description of primitive (non-composite) component
- * types. An instance of this class will be returned when invoking the
- * {@link ComponentFactory#getComponentDescription()} method.
- *
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- private final class PrimitiveTypeDescription extends ComponentTypeDescription {
-
- /*
- * Set to keep component's all super-class class-names.
- */
- private Set m_superClasses = new HashSet();
- /*
- * Set to keep component's all interface class-names.
- */
- private Set m_interfaces = new HashSet();
-
- /**
- * Creates a PrimitiveTypeDescription object.
- *
- * @param factory the factory attached to this component type description.
- */
- public PrimitiveTypeDescription(IPojoFactory factory) {
- super(factory);
-
- try {
- // The inspection can be done only for primitive components
- if (m_classname != null) {
- // Read inherited classes and interfaces into given Sets.
- new InheritanceInspector(getPojoMetadata(), getBundleContext().getBundle()).
- computeInterfacesAndSuperClasses(m_interfaces, m_superClasses);
- }
- } catch (ClassNotFoundException e) {
- m_interfaces.clear();
- m_superClasses.clear();
- }
-
- }
-
- /**
- * Computes the properties to publish.
- * The <code>component.class</code> property contains the implementation class name.
- *
- * @return the dictionary of properties to publish
- * @see org.apache.felix.ipojo.architecture.ComponentTypeDescription#getPropertiesToPublish()
- */
- public Dictionary getPropertiesToPublish() {
- Dictionary dict = super.getPropertiesToPublish();
- if (m_classname != null) {
- dict.put("component.class", m_classname);
- }
- return dict;
- }
-
- /**
- * Adds the "implementation-class" attribute to the type description.
- *
- * @return the component type description.
- * @see org.apache.felix.ipojo.architecture.ComponentTypeDescription#getDescription()
- */
- public Element getDescription() {
- Element elem = super.getDescription();
- elem.addAttribute(new Attribute("Implementation-Class", m_classname));
-
- /* Adding interfaces and super-classes of component into description */
- Element inheritance = new Element("Inherited", "");
-
- inheritance.addAttribute(new Attribute("Interfaces", m_interfaces.toString()));
- inheritance.addAttribute(new Attribute("SuperClasses", m_superClasses.toString()));
-
- elem.addElement(inheritance);
-
- return elem;
- }
-
- /**
- * This class is used to collect interfaces and super-classes of given component in specified Sets.
- *
- * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
- */
- private final class InheritanceInspector {
- /*
- * PojoMetadata of target Component.
- */
- private PojoMetadata m_pojoMetadata;
- /*
- * Bundle exposing target component.
- */
- private Bundle m_bundle;
-
-
- /**
- * Creates a TypeCollector object
- *
- * @param pojoMetadata PojoMetadata describing Component.
- * @param bundle Bundle which has been exposed the intended Component.
- */
- public InheritanceInspector(PojoMetadata pojoMetadata, Bundle bundle) {
- m_pojoMetadata = pojoMetadata;
- m_bundle = bundle;
- }
-
- /**
- * Collect interfaces implemented by the POJO into given Sets.
- *
- * @param interfaces : the set of implemented interfaces
- * @param classes : the set of extended classes
- * @throws ClassNotFoundException : occurs when an interface cannot be loaded.
- */
- public void computeInterfacesAndSuperClasses(Set interfaces, Set classes) throws ClassNotFoundException {
- String[] immediateInterfaces = m_pojoMetadata.getInterfaces();
- String parentClass = m_pojoMetadata.getSuperClass();
-
- // First iterate on found specification in manipulation metadata
- for (int i = 0; i < immediateInterfaces.length; i++) {
- interfaces.add(immediateInterfaces[i]);
- // Iterate on interfaces implemented by the current interface
- Class clazz = m_bundle.loadClass(immediateInterfaces[i]);
- collectInterfaces(clazz, interfaces, m_bundle);
- }
-
- // Look for parent class.
- if (parentClass != null) {
- Class clazz = m_bundle.loadClass(parentClass);
- collectInterfacesFromClass(clazz, interfaces, m_bundle);
- classes.add(parentClass);
- collectParentClassesFromClass(clazz, classes, m_bundle);
- }
-
- // Removing Object Class from the inherited classes list.
- classes.remove(Object.class.getName());
- }
-
- /**
- * Look for inherited interfaces.
- *
- * @param clazz : interface name to explore (class object)
- * @param acc : set (accumulator)
- * @param bundle : bundle
- * @throws ClassNotFoundException : occurs when an interface cannot be loaded.
- */
- private void collectInterfaces(Class clazz, Set acc, Bundle bundle) throws ClassNotFoundException {
- Class[] clazzes = clazz.getInterfaces();
- for (int i = 0; i < clazzes.length; i++) {
- acc.add(clazzes[i].getName());
- collectInterfaces(clazzes[i], acc, bundle);
- }
- }
-
- /**
- * Collect interfaces for the given class.
- * This method explores super class to.
- *
- * @param clazz : class object.
- * @param acc : set of implemented interface (accumulator)
- * @param bundle : bundle.
- * @throws ClassNotFoundException : occurs if an interface cannot be load.
- */
- private void collectInterfacesFromClass(Class clazz, Set acc, Bundle bundle) throws ClassNotFoundException {
- Class[] clazzes = clazz.getInterfaces();
- for (int i = 0; i < clazzes.length; i++) {
- acc.add(clazzes[i].getName());
- collectInterfaces(clazzes[i], acc, bundle);
- }
- // Iterate on parent classes
- Class sup = clazz.getSuperclass();
- if (sup != null) {
- collectInterfacesFromClass(sup, acc, bundle);
- }
- }
-
- /**
- * Collect parent classes for the given class.
- *
- * @param clazz : class object.
- * @param acc : set of extended classes (accumulator)
- * @param bundle : bundle.
- * @throws ClassNotFoundException : occurs if an interface cannot be load.
- */
- private void collectParentClassesFromClass(Class clazz, Set acc, Bundle bundle) throws ClassNotFoundException {
- Class parent = clazz.getSuperclass();
- if (parent != null) {
- acc.add(parent.getName());
- collectParentClassesFromClass(parent, acc, bundle);
- }
- }
- }
- }
}