Author: davidb
Date: Tue Apr 13 12:28:48 2010
New Revision: 933580
URL: http://svn.apache.org/viewvc?rev=933580&view=rev
Log:
Functionality to generate an endpoint description XML document given a map of
properties.
The functionality is accessible through:
String LocalDiscoveryUtils.getEndpointDescriptionXML(Map m)
New tests included too.
Added:
cxf/dosgi/trunk/discovery/local/src/test/resources/ed2-generated.xml
Modified:
cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java
cxf/dosgi/trunk/discovery/local/src/test/resources/ed3.xml
Modified:
cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java?rev=933580&r1=933579&r2=933580&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
(original)
+++
cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
Tue Apr 13 12:28:48 2010
@@ -31,6 +31,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -453,5 +454,113 @@ public final class LocalDiscoveryUtils {
}
return Collections.emptyList();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static String getEndpointDescriptionXML(Map m) {
+ Document d = new Document();
+ Namespace ns =
Namespace.getNamespace("http://www.osgi.org/xmlns/rsa/v1.0.0");
+ Element rootEl = new Element("endpoint-descriptions", ns);
+ d.setRootElement(rootEl);
+ Element contentEl = new Element("endpoint-description", ns);
+ rootEl.addContent(contentEl);
+
+ for (Map.Entry entry : (Set<Map.Entry>) m.entrySet()) {
+ String key = entry.getKey().toString();
+ Object val = entry.getValue();
+
+ Element propEl = new Element("property", ns);
+ propEl.setAttribute("name", key);
+ if (val.getClass().isArray()) {
+ Element arrayEl = new Element("array", ns);
+ propEl.addContent(arrayEl);
+ for (Object o : normalizeArray(val)) {
+ setValueType(propEl, o);
+ Element valueEl = new Element("value", ns);
+ arrayEl.addContent(valueEl);
+ valueEl.addContent(o.toString());
+ }
+ } else if (val instanceof List) {
+ Element listEl = new Element("list", ns);
+ propEl.addContent(listEl);
+ handleCollectionValue(ns, (Collection) val, propEl, listEl);
+ } else if (val instanceof Set) {
+ Element setEl = new Element("set", ns);
+ propEl.addContent(setEl);
+ handleCollectionValue(ns, (Collection) val, propEl, setEl);
+ } else {
+ setValueType(propEl, val);
+ propEl.setAttribute("value", val.toString());
+ }
+ contentEl.addContent(propEl);
+ }
+
+ return new XMLOutputter(Format.getPrettyFormat()).outputString(d);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static Object [] normalizeArray(Object val) {
+ List l = new ArrayList();
+ if (val instanceof int[]) {
+ int[] ia = (int []) val;
+ for (int i : ia) {
+ l.add(i);
+ }
+ } else if (val instanceof long[]) {
+ long[] la = (long []) val;
+ for (long i : la) {
+ l.add(i);
+ }
+ } else if (val instanceof float[]) {
+ float[] fa = (float[]) val;
+ for (float f : fa) {
+ l.add(f);
+ }
+ } else if (val instanceof byte[]) {
+ byte[] ba = (byte []) val;
+ for (byte b : ba) {
+ l.add(b);
+ }
+ } else if (val instanceof boolean[]) {
+ boolean[] ba = (boolean []) val;
+ for (boolean b : ba) {
+ l.add(b);
+ }
+ } else if (val instanceof short[]) {
+ short[] sa = (short []) val;
+ for (short s : sa) {
+ l.add(s);
+ }
+ } else if (val instanceof char[]) {
+ char[] ca = (char []) val;
+ for (char c : ca) {
+ l.add(c);
+ }
+ } else {
+ return (Object []) val;
+ }
+ return l.toArray();
+ }
+
+ @SuppressWarnings("unchecked")
+ private static void handleCollectionValue(Namespace ns, Collection val,
Element propEl, Element listEl) {
+ for (Object o : val) {
+ setValueType(propEl, o);
+ Element valueEl = new Element("value", ns);
+ listEl.addContent(valueEl);
+ valueEl.addContent(o.toString());
+ }
+ }
+
+ private static void setValueType(Element propEl, Object val) {
+ if (val instanceof String) {
+ return;
+ }
+
+ String dataType = val.getClass().getName();
+ if (dataType.startsWith("java.lang.")) {
+ dataType = dataType.substring("java.lang.".length());
+ }
+ propEl.setAttribute("value-type", dataType);
}
}
Modified:
cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java?rev=933580&r1=933579&r2=933580&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java
(original)
+++
cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java
Tue Apr 13 12:28:48 2010
@@ -19,14 +19,21 @@
package org.apache.cxf.dosgi.discovery.local;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.net.URL;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Pattern;
import junit.framework.TestCase;
@@ -42,6 +49,8 @@ import org.osgi.service.remoteserviceadm
import org.osgi.service.remoteserviceadmin.RemoteConstants;
public class LocalDiscoveryUtilsTest extends TestCase {
+ private static final String LF = System.getProperty("line.separator");
+
public void testNoRemoteServicesXMLFiles() {
Bundle b = EasyMock.createNiceMock(Bundle.class);
EasyMock.replay(b);
@@ -209,6 +218,57 @@ public class LocalDiscoveryUtilsTest ext
assertEquals("5", ed1.getProperties().get("blah"));
}
+ @SuppressWarnings("unchecked")
+ public void testCreateXML() throws Exception {
+ Map m = new LinkedHashMap();
+ m.put("service.imported.configs", "org.apache.cxf.ws");
+ m.put("endpoint.id", "foo:bar");
+ m.put("objectClass", new String [] {"com.acme.HelloService",
"some.other.Service"});
+ m.put("long", 9223372036854775807L);
+ m.put("Long2", -1L);
+ m.put("double", 1.7976931348623157E308);
+ m.put("Double2", 1.0d);
+ m.put("float", 42.24f);
+ m.put("Float2", 1.0f);
+ m.put("int", 17);
+ m.put("Integer2", 42);
+ m.put("byte", (byte) 127);
+ m.put("Byte2", (byte) -128);
+ m.put("boolean", true);
+ m.put("Boolean2", false);
+ m.put("short", (short) 99);
+ m.put("Short2", (short) -99);
+ m.put("char", '@');
+ m.put("Character2", 'X');
+
+ List<Boolean> boolList = new ArrayList<Boolean>();
+ boolList.add(true);
+ boolList.add(false);
+ m.put("bool-list", boolList);
+ m.put("empty-set", new HashSet());
+
+ Set<String> stringSet= new LinkedHashSet<String>();
+ stringSet.add("Hello there");
+ stringSet.add("How are you?");
+ m.put("string-set", stringSet);
+
+ int[] intArray = new int[] {1, 2};
+ m.put("int-array", intArray);
+
+ String xml = "<xml>" + LF +
+ "<t1 xmlns=\"http://www.acme.org/xmlns/other/v1.0.0\">" + LF +
+ "<foo type='bar'>haha</foo>" + LF +
+ "</t1>" + LF +
+ "</xml>";
+ m.put("someXML", xml);
+
+ String actual = LocalDiscoveryUtils.getEndpointDescriptionXML(m);
+
+ URL edURL = getClass().getResource("/ed2-generated.xml");
+ String expected = new String(drainStream(edURL.openStream()));
+ assertEquals(normXML(expected), normXML(actual));
+ }
+
private static String normXML(String s) throws JDOMException, IOException {
String s2 = stripComment(s);
String s3 = stripProlog(s2);
@@ -218,10 +278,40 @@ public class LocalDiscoveryUtilsTest ext
}
private static String stripComment(String s) {
- return s.replaceAll("<!--(.*?)-->", "");
+ return Pattern.compile("<!--(.*?)-->",
Pattern.DOTALL).matcher(s).replaceAll("");
}
private static String stripProlog(String s) {
return s.replaceAll("<\\?(.*?)\\?>", "");
}
+
+ public static void drainStream(InputStream is, OutputStream os) throws
IOException {
+ byte[] bytes = new byte[8192];
+
+ int length = 0;
+ int offset = 0;
+
+ while ((length = is.read(bytes, offset, bytes.length - offset)) != -1)
{
+ offset += length;
+
+ if (offset == bytes.length) {
+ os.write(bytes, 0, bytes.length);
+ offset = 0;
+ }
+ }
+ if (offset != 0) {
+ os.write(bytes, 0, offset);
+ }
+ }
+
+ public static byte [] drainStream(InputStream is) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ drainStream(is, baos);
+ return baos.toByteArray();
+ } finally {
+ is.close();
+ }
+ }
+
}
Added: cxf/dosgi/trunk/discovery/local/src/test/resources/ed2-generated.xml
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/test/resources/ed2-generated.xml?rev=933580&view=auto
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/test/resources/ed2-generated.xml (added)
+++ cxf/dosgi/trunk/discovery/local/src/test/resources/ed2-generated.xml Tue
Apr 13 12:28:48 2010
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/rsa/v1.0.0">
+ <endpoint-description>
+ <property name="service.imported.configs" value="org.apache.cxf.ws"/>
+ <property name="endpoint.id" value="foo:bar"/>
+ <property name="objectClass">
+ <array>
+ <value>com.acme.HelloService</value>
+ <value>some.other.Service</value>
+ </array>
+ </property>
+
+ <property name="long" value-type="Long" value="9223372036854775807"/>
+ <property name="Long2" value-type="Long" value="-1"/>
+ <property name="double" value-type="Double"
value="1.7976931348623157E308"/>
+ <property name="Double2" value-type="Double" value="1.0"/>
+ <property name="float" value-type="Float" value="42.24"/>
+ <property name="Float2" value-type="Float" value="1.0"/>
+ <property name="int" value-type="Integer" value="17"/>
+ <property name="Integer2" value-type="Integer" value="42"/>
+ <property name="byte" value-type="Byte" value="127"/>
+ <property name="Byte2" value-type="Byte" value="-128"/>
+ <property name="boolean" value-type="Boolean" value="true"/>
+ <property name="Boolean2" value-type="Boolean" value="false"/>
+ <property name="short" value-type="Short" value="99"/>
+ <property name="Short2" value-type="Short" value="-99"/>
+ <property name="char" value-type="Character" value="@"/>
+ <property name="Character2" value-type="Character" value="X"/>
+
+ <property name="bool-list" value-type="Boolean">
+ <list>
+ <value>true</value>
+ <value>false</value>
+ </list>
+ </property>
+ <property name="empty-set">
+ <set/>
+ </property>
+ <property name="string-set">
+ <set>
+ <value>Hello there</value>
+ <value>How are you?</value>
+ </set>
+ </property>
+ <property name="int-array" value-type="Integer">
+ <array>
+ <value>1</value>
+ <value>2</value>
+ </array>
+ </property>
+ <property name="someXML" value="<xml>
<t1
xmlns="http://www.acme.org/xmlns/other/v1.0.0">
<foo
type='bar'>haha</foo>
</t1>
</xml>"/>
+ </endpoint-description>
+</endpoint-descriptions>
Modified: cxf/dosgi/trunk/discovery/local/src/test/resources/ed3.xml
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/test/resources/ed3.xml?rev=933580&r1=933579&r2=933580&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/test/resources/ed3.xml (original)
+++ cxf/dosgi/trunk/discovery/local/src/test/resources/ed3.xml Tue Apr 13
12:28:48 2010
@@ -19,8 +19,7 @@
under the License.
-->
-<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/rsa/v1.0.0"
- xmlns:other="http://www.acme.org/xmlns/other/v1.0.0">
+<endpoint-descriptions xmlns="http://www.osgi.org/xmlns/rsa/v1.0.0">
<endpoint-description>
<property name="objectClass">
<array>