Return-Path: Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: (qmail 19015 invoked from network); 22 Apr 2009 11:02:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 22 Apr 2009 11:02:20 -0000 Received: (qmail 56561 invoked by uid 500); 22 Apr 2009 11:02:19 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 56484 invoked by uid 500); 22 Apr 2009 11:02:19 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 56475 invoked by uid 99); 22 Apr 2009 11:02:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 22 Apr 2009 11:02:19 +0000 X-ASF-Spam-Status: No, hits=-1998.5 required=10.0 tests=ALL_TRUSTED,WEIRD_PORT X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 22 Apr 2009 11:02:17 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 336872388B75; Wed, 22 Apr 2009 11:01:56 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r767466 - in /cxf/dosgi/trunk: discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/ dsw/cxf-dsw/src/main/java... Date: Wed, 22 Apr 2009 11:01:55 -0000 To: commits@cxf.apache.org From: davidb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090422110156.336872388B75@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 serviceProps = (Map) sr.getProperty("service.properties"); - for (Map.Entry prop : serviceProps.entrySet()) { - p.setProperty(prop.getKey(), prop.getValue().toString()); + if (serviceProps != null) { + for (Map.Entry 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 srProps = new Hashtable(); 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 expected = new HashMap(); - expected.put("osgi.remote.interfaces", "*"); - expected.put("osgi.remote.configuration.type", "pojo"); - expected.put("osgi.remote.configuration.pojo.address", "http://localhost:9090/ps"); + Map initial = new HashMap(); + 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 expected = new HashMap(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 props = new HashMap(); + 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 expectedProps = new Hashtable(); + 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() { + 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 iList = new ArrayList(); for (String iName : interfaceNames) { - iList.add(iName); - } + iList.add(iName); + } ServiceEndpointDescription sd = control.createMock(ServiceEndpointDescription.class); sd.getProvidedInterfaces(); EasyMock.expectLastCall().andReturn(iList); return sd; } - }