summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Campo2008-02-28 11:41:33 (EST)
committerChristian Campo2008-02-28 11:41:33 (EST)
commitab6af28c25d25f11f13b70dfc9e353783594c76f (patch)
tree856cb7ecf02c2dbaf6ac720edea43f34a5f81fe6
parenteb5937f96787eb42080368c50c50e39e45dd0dc7 (diff)
downloadorg.eclipse.riena-ab6af28c25d25f11f13b70dfc9e353783594c76f.zip
org.eclipse.riena-ab6af28c25d25f11f13b70dfc9e353783594c76f.tar.gz
org.eclipse.riena-ab6af28c25d25f11f13b70dfc9e353783594c76f.tar.bz2
changed ExtensionUtility to work correctly with multiple extensions and support isXXX for boolean values
-rw-r--r--org.eclipse.riena.core/src/org/eclipse/riena/core/extension/util/ExtensionUtility.java155
-rw-r--r--org.eclipse.riena.tests/plugin.xml13
-rw-r--r--org.eclipse.riena.tests/schema/core.test.extpoint.exsd41
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ExtensionUtilTest.java27
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ITest.java2
5 files changed, 140 insertions, 98 deletions
diff --git a/org.eclipse.riena.core/src/org/eclipse/riena/core/extension/util/ExtensionUtility.java b/org.eclipse.riena.core/src/org/eclipse/riena/core/extension/util/ExtensionUtility.java
index 4022a22..9651242 100644
--- a/org.eclipse.riena.core/src/org/eclipse/riena/core/extension/util/ExtensionUtility.java
+++ b/org.eclipse.riena.core/src/org/eclipse/riena/core/extension/util/ExtensionUtility.java
@@ -14,6 +14,7 @@ import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
+import java.util.ArrayList;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
@@ -22,85 +23,97 @@ import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.RegistryFactory;
/**
- * ExtensionUtility maps Extensions to Interfaces. Extension properties can then be read by access the getters in the
- * interface definition. ExtensionUtility instantiates generic proxies to do this.
+ * ExtensionUtility maps Extensions to Interfaces. Extension properties can then
+ * be read by access the getters in the interface definition. ExtensionUtility
+ * instantiates generic proxies to do this.
*
- * The ExtensionUtility does not know the schema so it can only trust that the extension and the interface match.
+ * The ExtensionUtility does not know the schema so it can only trust that the
+ * extension and the interface match.
*
*/
public class ExtensionUtility {
- /**
- * Static method to read extensions
- *
- * @param extensionPoint
- * @param interf
- * @return
- */
- public static <T> T[] readExtensions(String extensionPoint, Class<T> interf) {
- IExtensionRegistry registry = RegistryFactory.getRegistry();
- IExtensionPoint extPoint = registry.getExtensionPoint(extensionPoint);
- if (extPoint == null) {
- return null;
- }
- IExtension[] cfgExtensions = extPoint.getExtensions();
- if (cfgExtensions.length == 0) {
- return null;
- }
- T[] objects = (T[]) Array.newInstance(interf, cfgExtensions.length);
- int i = 0;
- for (IExtension cfgExt : cfgExtensions) {
- Array.set(objects, i++, Proxy.newProxyInstance(interf.getClassLoader(), new Class[] { interf }, new InterfaceInvocationHandler(cfgExt
- .getConfigurationElements()[0], interf)));
- }
- return objects;
- }
+ /**
+ * Static method to read extensions
+ *
+ * @param extensionPoint
+ * @param interf
+ * @return
+ */
+ public static <T> T[] readExtensions(String extensionPoint, Class<T> interf) {
+ IExtensionRegistry registry = RegistryFactory.getRegistry();
+ IExtensionPoint extPoint = registry.getExtensionPoint(extensionPoint);
+ if (extPoint == null) {
+ return null;
+ }
+ IExtension[] cfgExtensions = extPoint.getExtensions();
+ if (cfgExtensions.length == 0) {
+ return null;
+ }
+ ArrayList<Object> list = new ArrayList<Object>();
+ for (IExtension cfgExt : cfgExtensions) {
+ for (int x = 0; x < cfgExt.getConfigurationElements().length; x++) {
+ list.add(Proxy.newProxyInstance(interf.getClassLoader(), new Class[] { interf },
+ new InterfaceInvocationHandler(cfgExt.getConfigurationElements()[x], interf)));
+ }
+ }
- /**
- * InvocationHandler for managing proxies for the extension to interface proxy mapping
- *
- */
- private static class InterfaceInvocationHandler implements InvocationHandler {
+ T[] objects = (T[]) Array.newInstance(interf, list.size());
+ list.toArray(objects);
+ return objects;
+ }
- private IConfigurationElement config;
- private Class<?> interf;
+ /**
+ * InvocationHandler for managing proxies for the extension to interface
+ * proxy mapping
+ *
+ */
+ private static class InterfaceInvocationHandler implements InvocationHandler {
- InterfaceInvocationHandler(IConfigurationElement config, Class<?> interf) {
- this.config = config;
- this.interf = interf;
- }
+ private IConfigurationElement config;
+ private Class<?> interf;
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if (method.getName().startsWith("get")) {
- Method[] meths = interf.getMethods();
- for (Method meth : meths) {
- if (meth.getName().equals(method.getName())) {
- Class<?> retType = meth.getReturnType();
- String name = method.getName().substring("get".length());
- name = name.substring(0, 1).toLowerCase() + name.substring(1);
- if (retType.equals(String.class)) {
- return config.getAttribute(name);
- } else if (retType.equals(Boolean.TYPE)) {
- return Boolean.valueOf(config.getAttribute(name));
- } else if (retType.isInterface()) {
- IConfigurationElement cfgElement = config.getChildren(name)[0];
- if (cfgElement == null) {
- return null;
- }
- return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { retType }, new InterfaceInvocationHandler(cfgElement,
- retType));
- }
- }
- }
- throw new UnsupportedOperationException("property for method " + method.getName() + " not found.");
- }
- if (method.getName().startsWith("create")) {
- String name = method.getName().substring("create".length());
- name = name.substring(0, 1).toLowerCase() + name.substring(1);
- return config.createExecutableExtension(name);
- }
- throw new UnsupportedOperationException("only getXXX or createXXX is supported");
- }
- }
+ InterfaceInvocationHandler(IConfigurationElement config, Class<?> interf) {
+ this.config = config;
+ this.interf = interf;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ if (method.getName().startsWith("get") || method.getName().startsWith("is")) {
+ Method[] meths = interf.getMethods();
+ for (Method meth : meths) {
+ if (meth.getName().equals(method.getName())) {
+ Class<?> retType = meth.getReturnType();
+ String name;
+ if (method.getName().startsWith("get")) {
+ name = method.getName().substring("get".length());
+ } else {
+ name = method.getName().substring("is".length());
+ }
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);
+ if (retType.equals(String.class)) {
+ return config.getAttribute(name);
+ } else if (retType.equals(Boolean.TYPE)) {
+ return Boolean.valueOf(config.getAttribute(name));
+ } else if (retType.isInterface()) {
+ IConfigurationElement cfgElement = config.getChildren(name)[0];
+ if (cfgElement == null) {
+ return null;
+ }
+ return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { retType },
+ new InterfaceInvocationHandler(cfgElement, retType));
+ }
+ }
+ }
+ throw new UnsupportedOperationException("property for method " + method.getName() + " not found.");
+ }
+ if (method.getName().startsWith("create")) {
+ String name = method.getName().substring("create".length());
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);
+ return config.createExecutableExtension(name);
+ }
+ throw new UnsupportedOperationException("only getXXX, isXXX or createXXX is supported");
+ }
+ }
}
diff --git a/org.eclipse.riena.tests/plugin.xml b/org.eclipse.riena.tests/plugin.xml
index b63e33b..59fa3a2 100644
--- a/org.eclipse.riena.tests/plugin.xml
+++ b/org.eclipse.riena.tests/plugin.xml
@@ -29,5 +29,18 @@
executable="java.lang.String"
string="test1">
</test>
+ <test
+ boolean="false"
+ executable="java.util.HashMap"
+ string="test2">
+ </test>
+ </extension>
+ <extension
+ point="core.test.extpoint">
+ <test
+ boolean="true"
+ executable="java.util.ArrayList"
+ string="test3">
+ </test>
</extension>
</plugin>
diff --git a/org.eclipse.riena.tests/schema/core.test.extpoint.exsd b/org.eclipse.riena.tests/schema/core.test.extpoint.exsd
index 0aabc80..3add169 100644
--- a/org.eclipse.riena.tests/schema/core.test.extpoint.exsd
+++ b/org.eclipse.riena.tests/schema/core.test.extpoint.exsd
@@ -1,10 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
-<schema targetNamespace="de.compeople.scp.core.tests">
+<schema targetNamespace="de.compeople.scp.core.tests" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
- <appInfo>
+ <appinfo>
<meta.schema plugin="de.compeople.scp.core.tests" id="core.test.extpoint" name="extpoint"/>
- </appInfo>
+ </appinfo>
<documentation>
[Enter description of this extension point.]
</documentation>
@@ -12,7 +12,7 @@
<element name="extension">
<complexType>
- <sequence>
+ <sequence minOccurs="1" maxOccurs="unbounded">
<element ref="test"/>
</sequence>
<attribute name="point" type="string" use="required">
@@ -34,9 +34,9 @@
<documentation>
</documentation>
- <appInfo>
+ <appinfo>
<meta.attribute translatable="true"/>
- </appInfo>
+ </appinfo>
</annotation>
</attribute>
</complexType>
@@ -63,57 +63,50 @@
<documentation>
</documentation>
- <appInfo>
+ <appinfo>
<meta.attribute kind="java"/>
- </appInfo>
+ </appinfo>
</annotation>
</attribute>
</complexType>
</element>
<annotation>
- <appInfo>
+ <appinfo>
<meta.section type="since"/>
- </appInfo>
+ </appinfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>
<annotation>
- <appInfo>
+ <appinfo>
<meta.section type="examples"/>
- </appInfo>
+ </appinfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>
<annotation>
- <appInfo>
+ <appinfo>
<meta.section type="apiInfo"/>
- </appInfo>
+ </appinfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>
+
<annotation>
- <appInfo>
+ <appinfo>
<meta.section type="implementation"/>
- </appInfo>
+ </appinfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>
- <annotation>
- <appInfo>
- <meta.section type="copyright"/>
- </appInfo>
- <documentation>
-
- </documentation>
- </annotation>
</schema>
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ExtensionUtilTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ExtensionUtilTest.java
index 027c041..731fb41 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ExtensionUtilTest.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ExtensionUtilTest.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.riena.core.extension.util;
+import java.util.ArrayList;
+import java.util.HashMap;
+
import org.eclipse.riena.tests.RienaTestCase;
public class ExtensionUtilTest extends RienaTestCase {
@@ -18,8 +21,26 @@ public class ExtensionUtilTest extends RienaTestCase {
System.out.println("test");
ITest[] tests = ExtensionUtility.readExtensions("core.test.extpoint", ITest.class);
assertNotNull(tests);
- assertTrue(tests[0].getString().equals("test1"));
- assertTrue(tests[0].getBoolean());
- assertTrue(tests[0].createExecutable() instanceof String);
+ assertTrue("tests.length should be 3 but is " + tests.length, tests.length == 3);
+ for (ITest test : tests) {
+ assertNotNull(test.getString());
+ if (test.getString().equals("test1")) {
+ assertTrue(test.getBoolean());
+ assertTrue(test.isBoolean());
+ assertTrue(test.createExecutable() instanceof String);
+ } else {
+ if (test.getString().equals("test2")) {
+ assertFalse(test.getBoolean());
+ assertFalse(test.isBoolean());
+ assertTrue(test.createExecutable() instanceof HashMap);
+ } else {
+ if (test.getString().equals("test3")) {
+ assertTrue(test.getBoolean());
+ assertTrue(test.isBoolean());
+ assertTrue(test.createExecutable() instanceof ArrayList);
+ }
+ }
+ }
+ }
}
}
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ITest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ITest.java
index fb7218a..a820bd7 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ITest.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/core/extension/util/ITest.java
@@ -16,6 +16,8 @@ public interface ITest {
boolean getBoolean();
+ boolean isBoolean();
+
Object createExecutable();
}