Author: davidb
Date: Wed Apr 22 11:01:55 2009
New Revision: 767466
URL: http://svn.apache.org/viewvc?rev=767466&view=rev
Log:
Setting endpoint ID on the ServicePublication.
Unit tests included.
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtilsTest.java
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java?rev=767466&r1=767465&r2=767466&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
Wed Apr 22 11:01:55 2009
@@ -36,6 +36,7 @@
import org.apache.zookeeper.ZooDefs.Ids;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.discovery.ServicePublication;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class PublishToZooKeeperCustomizer implements ServiceTrackerCustomizer {
@@ -111,15 +112,27 @@
Properties p = new Properties();
Map<String, Object> serviceProps = (Map<String, Object>)
sr.getProperty("service.properties");
- for (Map.Entry<String, Object> prop : serviceProps.entrySet()) {
- p.setProperty(prop.getKey(), prop.getValue().toString());
+ if (serviceProps != null) {
+ for (Map.Entry<String, Object> prop : serviceProps.entrySet()) {
+ p.setProperty(prop.getKey(), prop.getValue().toString());
+ }
}
+ copyProperty(ServicePublication.PROP_KEY_ENDPOINT_ID, sr, p);
+ copyProperty(ServicePublication.PROP_KEY_ENDPOINT_LOCATION, sr, p);
+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
p.store(baos, "");
return baos.toByteArray();
}
+ private static void copyProperty(String key, ServiceReference sr,
Properties p) {
+ Object eID = sr.getProperty(key);
+ if (eID != null) {
+ p.setProperty(key, eID.toString());
+ }
+ }
+
static String getKey(String endpoint) throws UnknownHostException,
URISyntaxException {
URI uri = new URI(endpoint);
if ("localhost".equals(uri.getHost()) ||
"127.0.0.1".equals(uri.getHost())) {
Modified:
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java?rev=767466&r1=767465&r2=767466&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
Wed Apr 22 11:01:55 2009
@@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import java.util.UUID;
import junit.framework.TestCase;
@@ -38,23 +39,29 @@
import org.easymock.classextension.EasyMock;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.discovery.ServicePublication;
public class PublishToZooKeeperCustomizerTest extends TestCase {
public void testAddingService() throws Exception {
Hashtable<String, Object> srProps = new Hashtable<String, Object>();
srProps.put("osgi.remote.interfaces", "*");
- final Properties expected = new Properties();
- expected.put("osgi.remote.interfaces", "*");
- ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
- expected.store(expectedBytes, "");
-
+ String location = "http://somehost.someorg:80/abc/def";
+ String eid = UUID.randomUUID().toString();
ServiceReference sr = EasyMock.createMock(ServiceReference.class);
EasyMock.expect(sr.getProperty("service.interface")).andReturn(Arrays.asList("java.lang.String",
"org.example.interface.AnInterface"));
-
EasyMock.expect(sr.getProperty("osgi.remote.endpoint.location")).andReturn("http://somehost.someorg:80/abc/def");
+
EasyMock.expect(sr.getProperty("osgi.remote.endpoint.location")).andReturn(location).atLeastOnce();
+
EasyMock.expect(sr.getProperty("osgi.remote.endpoint.id")).andReturn(eid).atLeastOnce();
EasyMock.expect(sr.getProperty("service.properties")).andReturn(srProps).anyTimes();
EasyMock.replay(sr);
+ final Properties expected = new Properties();
+ expected.put("osgi.remote.interfaces", "*");
+ expected.put("osgi.remote.endpoint.location", location);
+ expected.put("osgi.remote.endpoint.id", eid);
+ ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream();
+ expected.store(expectedBytes, "");
+
BundleContext bc = EasyMock.createMock(BundleContext.class);
EasyMock.expect(bc.getService(sr)).andReturn("something");
EasyMock.replay(bc);
@@ -131,13 +138,19 @@
}
public void testGetData() throws Exception {
- Map<String, Object> expected = new HashMap<String, Object>();
- expected.put("osgi.remote.interfaces", "*");
- expected.put("osgi.remote.configuration.type", "pojo");
- expected.put("osgi.remote.configuration.pojo.address",
"http://localhost:9090/ps");
+ Map<String, Object> initial = new HashMap<String, Object>();
+ initial.put("osgi.remote.interfaces", "*");
+ initial.put("osgi.remote.configuration.type", "pojo");
+ initial.put("osgi.remote.configuration.pojo.address",
"http://localhost:9090/ps");
+
+ String eid = UUID.randomUUID().toString();
+ HashMap<String, Object> expected = new HashMap<String,
Object>(initial);
+ expected.put(ServicePublication.PROP_KEY_ENDPOINT_ID, eid);
ServiceReference sr = EasyMock.createMock(ServiceReference.class);
-
EasyMock.expect(sr.getProperty("service.properties")).andReturn(expected);
+
EasyMock.expect(sr.getProperty("service.properties")).andReturn(initial);
+
EasyMock.expect(sr.getProperty(ServicePublication.PROP_KEY_ENDPOINT_ID)).andReturn(eid);
+
EasyMock.expect(sr.getProperty(ServicePublication.PROP_KEY_ENDPOINT_LOCATION)).andReturn(null);
EasyMock.replay(sr);
byte[] data = PublishToZooKeeperCustomizer.getData(sr);
Modified:
cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java?rev=767466&r1=767465&r2=767466&view=diff
==============================================================================
---
cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
(original)
+++
cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
Wed Apr 22 11:01:55 2009
@@ -18,35 +18,31 @@
*/
package org.apache.cxf.dosgi.dsw.hooks;
-import java.util.Collections;
+import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_ENDPOINT_ID;
+import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_ENDPOINT_LOCATION;
+import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME;
+import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_SERVICE_PROPERTIES;
+
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
-import java.util.Iterator;
import java.util.Map;
+import java.util.UUID;
import java.util.logging.Logger;
import org.apache.cxf.dosgi.dsw.ClassUtils;
import org.apache.cxf.dosgi.dsw.Constants;
-import org.apache.cxf.dosgi.dsw.OsgiService;
-import org.apache.cxf.dosgi.dsw.OsgiUtils;
import org.apache.cxf.dosgi.dsw.handlers.ConfigTypeHandlerFactory;
import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
import org.apache.cxf.dosgi.dsw.handlers.IntentUnsatifiedException;
import org.apache.cxf.dosgi.dsw.service.CxfDistributionProvider;
import org.apache.cxf.endpoint.Server;
-
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.discovery.ServiceEndpointDescription;
import org.osgi.service.discovery.ServicePublication;
-
-import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME;
-import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_SERVICE_PROPERTIES;
-import static
org.osgi.service.discovery.ServicePublication.PROP_KEY_ENDPOINT_LOCATION;
-
public final class ServiceHookUtils {
private static final Logger LOG =
Logger.getLogger(ServiceHookUtils.class.getName());
@@ -148,10 +144,12 @@
return props;
}
+ @SuppressWarnings("unchecked")
private static Dictionary
getPublicationProperties(ServiceEndpointDescription sd) {
Dictionary props = new Hashtable();
props.put(PROP_KEY_SERVICE_INTERFACE_NAME, sd.getProvidedInterfaces());
props.put(PROP_KEY_SERVICE_PROPERTIES, getServiceProperties(sd));
+ props.put(PROP_KEY_ENDPOINT_ID, UUID.randomUUID().toString());
if (sd.getLocation() != null) {
props.put(PROP_KEY_ENDPOINT_LOCATION, sd.getLocation());
}
Modified:
cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtilsTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtilsTest.java?rev=767466&r1=767465&r2=767466&view=diff
==============================================================================
---
cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtilsTest.java
(original)
+++
cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtilsTest.java
Wed Apr 22 11:01:55 2009
@@ -18,21 +18,29 @@
*/
package org.apache.cxf.dosgi.dsw.hooks;
+import java.net.URL;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
import java.util.List;
+import java.util.Map;
+import java.util.UUID;
import junit.framework.TestCase;
-import org.apache.cxf.dosgi.dsw.TestUtils;
import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
import org.apache.cxf.dosgi.dsw.service.ServiceEndpointDescriptionImpl;
import org.apache.cxf.endpoint.Server;
import org.easymock.EasyMock;
+import org.easymock.IAnswer;
import org.easymock.IMocksControl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
import org.osgi.service.discovery.ServiceEndpointDescription;
-
+import org.osgi.service.discovery.ServicePublication;
public class ServiceHookUtilsTest extends TestCase {
public void testCreateServer() {
@@ -72,18 +80,51 @@
assertNull(ServiceHookUtils.createServer(handler, serviceReference,
dswContext, callingContext, sd, service));
}
+ public void testPublish() throws Exception {
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put("foo", "bar");
+ props.put(ServicePublication.PROP_KEY_ENDPOINT_LOCATION,
"http://localhost/xyz");
+ ServiceEndpointDescriptionImpl sed = new
ServiceEndpointDescriptionImpl(String.class.getName(), props);
+ assertEquals(new URL("http://localhost/xyz"), sed.getLocation());
+
+ final Dictionary<String, Object> expectedProps = new Hashtable<String,
Object>();
+ expectedProps.put(ServicePublication.PROP_KEY_SERVICE_PROPERTIES,
props);
+ expectedProps.put(ServicePublication.PROP_KEY_SERVICE_INTERFACE_NAME,
Collections.singleton(String.class.getName()));
+ expectedProps.put(ServicePublication.PROP_KEY_ENDPOINT_LOCATION, new
URL("http://localhost/xyz"));
+
+ BundleContext bc = EasyMock.createMock(BundleContext.class);
+ EasyMock.expect(bc.registerService(
+ EasyMock.eq(ServicePublication.class.getName()),
+ EasyMock.anyObject(),
+ (Dictionary<?, ?>) EasyMock.anyObject()))
+ .andAnswer(new IAnswer<ServiceRegistration>() {
+ public ServiceRegistration answer() throws Throwable {
+ assertTrue(EasyMock.getCurrentArguments()[1]
instanceof ServicePublication);
+ Dictionary<?, ?> actualProps =
+ (Dictionary<?, ?>)
EasyMock.getCurrentArguments()[2];
+ UUID uuid =
UUID.fromString(actualProps.get(ServicePublication.PROP_KEY_ENDPOINT_ID).toString());
+
expectedProps.put(ServicePublication.PROP_KEY_ENDPOINT_ID, uuid.toString());
+ assertEquals(expectedProps, actualProps);
+ return EasyMock.createMock(ServiceRegistration.class);
+ }
+ });
+ EasyMock.replay(bc);
+
+ ServiceHookUtils.publish(bc, sed);
+ EasyMock.verify(bc);
+ }
+
private ServiceEndpointDescription mockServiceDescription(IMocksControl
control,
- String...
interfaceNames) {
+ String...
interfaceNames) {
List<String> iList = new ArrayList<String>();
for (String iName : interfaceNames) {
- iList.add(iName);
- }
+ iList.add(iName);
+ }
ServiceEndpointDescription sd =
control.createMock(ServiceEndpointDescription.class);
sd.getProvidedInterfaces();
EasyMock.expectLastCall().andReturn(iList);
return sd;
}
-
}