summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Campo2008-01-25 10:53:38 (EST)
committerChristian Campo2008-01-25 10:53:38 (EST)
commit4a07e76777624be4f8fdab534697453ed6bd820f (patch)
tree1c241e1c9cc4587837e7b9f44cdd597fb09fa5a9
parent5ec230593b5cd55b7f85e1a89872621f98c0415e (diff)
downloadorg.eclipse.riena-4a07e76777624be4f8fdab534697453ed6bd820f.zip
org.eclipse.riena-4a07e76777624be4f8fdab534697453ed6bd820f.tar.gz
org.eclipse.riena-4a07e76777624be4f8fdab534697453ed6bd820f.tar.bz2
ConfigUtility allows to remove a ConfigProxy, ConfigTest also tests for configuration proxy with business classes that are subclasses of the class that takes the configuration
-rw-r--r--org.eclipse.riena.core/src/org/eclipse/riena/core/config/ConfigUtility.java94
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/config/ConfigTest.java49
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/config/DerivedBusinessClass.java22
3 files changed, 122 insertions, 43 deletions
diff --git a/org.eclipse.riena.core/src/org/eclipse/riena/core/config/ConfigUtility.java b/org.eclipse.riena.core/src/org/eclipse/riena/core/config/ConfigUtility.java
index 76e3b02..2f6b1f8 100644
--- a/org.eclipse.riena.core/src/org/eclipse/riena/core/config/ConfigUtility.java
+++ b/org.eclipse.riena.core/src/org/eclipse/riena/core/config/ConfigUtility.java
@@ -17,58 +17,68 @@ import org.eclipse.riena.internal.core.config.ConfigProxy;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
/**
- * This class makes configuration a regular Java POJO easier. It creates a proxy around a business Java POJO. All
- * configuration that is sent by ConfigAdmin is converted into setter calls.
+ * This class makes configuration a regular Java POJO easier. It creates a proxy
+ * around a business Java POJO. All configuration that is sent by ConfigAdmin is
+ * converted into setter calls.
*
*/
public class ConfigUtility {
- private BundleContext context;
+ private BundleContext context;
+ private ServiceRegistration configProxyReg;
- /**
- * standard constructor
- *
- * @param context
- */
- public ConfigUtility(BundleContext context) {
- this.context = context;
- }
+ /**
+ * standard constructor
+ *
+ * @param context
+ */
+ public ConfigUtility(BundleContext context) {
+ this.context = context;
+ }
- /**
- * create proxy for business Java POJO
- *
- * @param bObject
- * @param pid
- */
- public void createConfigProxy(Object bObject, String pid) {
- ConfigProxy ms = new ConfigProxy(bObject);
- Hashtable<String, String> ht = new Hashtable<String, String>();
- ht.put(Constants.SERVICE_PID, pid);
- context.registerService(ManagedService.class.getName(), ms, ht);
- }
+ /**
+ * create proxy for business Java POJO
+ *
+ * @param bObject
+ * @param pid
+ */
+ public void createConfigProxy(Object bObject, String pid) {
+ ConfigProxy ms = new ConfigProxy(bObject);
+ Hashtable<String, String> ht = new Hashtable<String, String>();
+ ht.put(Constants.SERVICE_PID, pid);
+ configProxyReg = context.registerService(ManagedService.class.getName(), ms, ht);
+ }
- /**
- * easy access to configuration for a SERVICE_PID
- *
- * @param pid
- * @return
- */
- public Configuration getConfig(String pid) {
- ServiceReference ref = context.getServiceReference(ConfigurationAdmin.class.getName());
- ConfigurationAdmin cfgAdmin = (ConfigurationAdmin) context.getService(ref);
- // get config
- Configuration cfg;
- try {
- cfg = cfgAdmin.getConfiguration(pid);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- return cfg;
- }
+ public void removeConfigProxy() {
+ if (configProxyReg != null) {
+ configProxyReg.unregister();
+ configProxyReg = null;
+ }
+ }
+
+ /**
+ * easy access to configuration for a SERVICE_PID
+ *
+ * @param pid
+ * @return
+ */
+ public Configuration getConfig(String pid) {
+ ServiceReference ref = context.getServiceReference(ConfigurationAdmin.class.getName());
+ ConfigurationAdmin cfgAdmin = (ConfigurationAdmin) context.getService(ref);
+ // get config
+ Configuration cfg;
+ try {
+ cfg = cfgAdmin.getConfiguration(pid);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ return cfg;
+ }
} \ No newline at end of file
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/ConfigTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/ConfigTest.java
index cf4a9e1..25d3e3f 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/ConfigTest.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/ConfigTest.java
@@ -36,6 +36,9 @@ public class ConfigTest extends RienaTestCase {
protected void tearDown() throws Exception {
}
+ /**
+ * test that config is set in business class
+ */
public void testConfigBusinessClass() {
// create new business class instance
final BusinessClass bc = new BusinessClass();
@@ -44,7 +47,50 @@ public class ConfigTest extends RienaTestCase {
assertTrue(bc.getName().equals("default"));
// wrap with proxy
- new ConfigUtility(Activator.getContext()).createConfigProxy(bc, "org.eclipse.riena.business.pid");
+ ConfigUtility cu = new ConfigUtility(Activator.getContext());
+ cu.createConfigProxy(bc, "org.eclipse.riena.business.pid");
+
+ Thread t = new Thread() {
+ @Override
+ public void run() {
+ try {
+ // wait a short time until config is applied
+ Thread.sleep(200);
+ // invoke the business call method
+ bc.invoke("first call");
+ assertTrue(bc.getName().equals("christian campo compeople ag"));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (AssertionFailedError e) {
+ status.setTestSuccesfull(false);
+ }
+ }
+
+ };
+ t.start();
+ try {
+ t.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ cu.removeConfigProxy();
+ assertTrue(status.isTestSuccesfull());
+ }
+
+ /**
+ * test that config is also set in a derived business class (=subclass)
+ */
+ public void testConfigDerivedBusinessClass() {
+ // create new business class instance
+ final DerivedBusinessClass bc = new DerivedBusinessClass();
+ final ErrorStatus status = new ErrorStatus();
+
+ assertTrue(bc.getName().equals("default"));
+
+ // wrap with proxy
+ ConfigUtility cu = new ConfigUtility(Activator.getContext());
+ cu.createConfigProxy(bc, "org.eclipse.riena.business.pid");
Thread t = new Thread() {
@Override
@@ -70,6 +116,7 @@ public class ConfigTest extends RienaTestCase {
e.printStackTrace();
}
+ cu.removeConfigProxy();
assertTrue(status.isTestSuccesfull());
}
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/DerivedBusinessClass.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/DerivedBusinessClass.java
new file mode 100644
index 0000000..3e1c989
--- /dev/null
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/config/DerivedBusinessClass.java
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2007 compeople AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * compeople AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.riena.core.config;
+
+/**
+ *
+ */
+public class DerivedBusinessClass extends BusinessClass {
+
+ public DerivedBusinessClass() {
+ super();
+ }
+
+}