Author: davidb
Date: Fri Jan 9 05:03:48 2009
New Revision: 733025
URL: http://svn.apache.org/viewvc?rev=733025&view=rev
Log:
Fix for CXF-1960
New unit tests included.
Modified:
cxf/sandbox/dosgi/distribution/multi-bundle/src/main/resources/distro_bundles.xml
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/Activator.java
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHook.java
cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/ActivatorTest.java
cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHookTest.java
Modified: cxf/sandbox/dosgi/distribution/multi-bundle/src/main/resources/distro_bundles.xml
URL: http://svn.apache.org/viewvc/cxf/sandbox/dosgi/distribution/multi-bundle/src/main/resources/distro_bundles.xml?rev=733025&r1=733024&r2=733025&view=diff
==============================================================================
--- cxf/sandbox/dosgi/distribution/multi-bundle/src/main/resources/distro_bundles.xml (original)
+++ cxf/sandbox/dosgi/distribution/multi-bundle/src/main/resources/distro_bundles.xml Fri
Jan 9 05:03:48 2009
@@ -29,11 +29,11 @@
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.bundles.neethi-2.0.4_1.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.bundles.woodstox-3.2.7_1.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/cxf-bundle-minimal-2.2-SNAPSHOT.jar</bundle>
- <bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/cxf-dosgi-ri-discovery-local-1.0-SNAPSHOT.jar</bundle>
- <bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/cxf-dosgi-ri-dsw-cxf-1.0-SNAPSHOT.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.specs.saaj-api-1.3-1.1.1.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.specs.stax-api-1.0-1.1.1.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.specs.jaxb-api-2.1-1.1.1.jar</bundle>
<bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/org.apache.servicemix.specs.jaxws-api-2.1-1.1.1.jar</bundle>
+ <bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/cxf-dosgi-ri-discovery-local-1.0-SNAPSHOT.jar</bundle>
+ <bundle>cxf-dosgi-ri-multibundle-distribution-1.0-SNAPSHOT-dosgi-ri-multi-bundle-assembly.dir/bundles/cxf-dosgi-ri-dsw-cxf-1.0-SNAPSHOT.jar</bundle>
</bundles>
Modified: cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/Activator.java
URL: http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/Activator.java?rev=733025&r1=733024&r2=733025&view=diff
==============================================================================
--- cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/Activator.java (original)
+++ cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/Activator.java Fri
Jan 9 05:03:48 2009
@@ -18,7 +18,6 @@
*/
package org.apache.cxf.dosgi.dsw;
-
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.ExecutorService;
@@ -34,6 +33,7 @@
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
@@ -46,15 +46,15 @@
private static final String CONFIG_SERVICE_PID = "cxf-dsw";
private BundleContext bc;
-
- private CxfPublishHook pHook;
- private CxfListenerHook lHook;
private ExecutorService execService =
new ThreadPoolExecutor(5, 10, 50, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
+
CxfDistributionProvider dpService;
+ CxfPublishHook pHook;
+ CxfListenerHook lHook;
- public void start(BundleContext context) {
+ public void start(BundleContext context) throws Exception {
// Disable the fast infoset as it's not compatible (yet) with OSGi
System.setProperty("org.apache.cxf.nofastinfoset", "true");
@@ -69,6 +69,7 @@
lHook = new CxfListenerHook(context, dpService);
context.registerService(ListenerHook.class.getName(), lHook, new Hashtable());
context.addServiceListener(this);
+ checkExistingServices();
}
private CxfDistributionProvider registerDistributionProviderService() {
@@ -120,6 +121,17 @@
}
}
+ private void checkExistingServices() throws InvalidSyntaxException {
+ ServiceReference[] references = bc.getServiceReferences(null,
+ "(" + org.apache.cxf.dosgi.dsw.Constants.REMOTE_INTERFACES_PROPERTY + "=*)");
+
+ if (references != null) {
+ for (ServiceReference sref : references) {
+ pHook.publishEndpoint(sref);
+ }
+ }
+ }
+
public void serviceChanged(ServiceEvent event) {
final ServiceReference sref = event.getServiceReference();
Modified: cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHook.java
URL: http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHook.java?rev=733025&r1=733024&r2=733025&view=diff
==============================================================================
--- cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHook.java
(original)
+++ cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHook.java
Fri Jan 9 05:03:48 2009
@@ -24,7 +24,6 @@
import java.util.List;
import java.util.Map;
-import org.apache.cxf.dosgi.dsw.Constants;
import org.apache.cxf.dosgi.dsw.OsgiUtils;
import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
import org.apache.cxf.dosgi.dsw.service.CxfDistributionProvider;
@@ -35,7 +34,7 @@
public class CxfPublishHook extends AbstractHook {
- private Map<ServiceReference, List<EndpointInfo>> endpoints
+ Map<ServiceReference, List<EndpointInfo>> endpoints
= new LinkedHashMap<ServiceReference, List<EndpointInfo>>();
public CxfPublishHook(BundleContext bc, CxfDistributionProvider dpService) {
@@ -43,6 +42,11 @@
}
public void publishEndpoint(ServiceReference sref) {
+ synchronized (endpoints) {
+ if (endpoints.containsKey(sref)) {
+ return;
+ }
+ }
if (ServiceHookUtils.isCreatedByDsw(sref)) {
return;
@@ -69,15 +73,8 @@
ServiceEndpointDescription[] flatList =
OsgiUtils.flattenServiceDescription(sd);
for (int i = 0; i < publishableInterfaces.length; i++) {
- Server server = null;
boolean isPublished = false;
- server = ServiceHookUtils.createServer(
- getHandler(sd, getHandlerProperties()),
- sref,
- getContext(),
- sref.getBundle().getBundleContext(),
- sd,
- getContext().getService(sref));
+ Server server = createServer(sref, sd);
if (server != null && ServiceHookUtils.publish(getContext(), sd)) {
isPublished = true;
}
@@ -98,6 +95,12 @@
}
}
}
+
+ Server createServer(ServiceReference sref, ServiceEndpointDescription sd) {
+ return ServiceHookUtils.createServer(
+ getHandler(sd, getHandlerProperties()), sref, getContext(),
+ sref.getBundle().getBundleContext(), sd, getContext().getService(sref));
+ }
public void removeEndpoint(ServiceReference sref) {
List<EndpointInfo> endpointList = null;
Modified: cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/ActivatorTest.java
URL: http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/ActivatorTest.java?rev=733025&r1=733024&r2=733025&view=diff
==============================================================================
--- cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/ActivatorTest.java
(original)
+++ cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/ActivatorTest.java
Fri Jan 9 05:03:48 2009
@@ -10,6 +10,7 @@
import junit.framework.TestCase;
+import org.apache.cxf.dosgi.dsw.hooks.TestService;
import org.apache.cxf.dosgi.dsw.qos.IntentMap;
import org.apache.cxf.dosgi.dsw.service.CxfDistributionProvider;
import org.apache.cxf.feature.AbstractFeature;
@@ -18,6 +19,7 @@
import org.easymock.IMocksControl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.distribution.DistributionProvider;
@@ -48,8 +50,8 @@
control.replay();
return bc;
}
-
- public void testCreateAndShutdownDistributionProviderService() {
+
+ public void testCreateAndShutdownDistributionProviderService() throws Exception {
BundleContext bc = getMockBundleContext();
Activator a = new Activator() {
@@ -74,7 +76,7 @@
}
@SuppressWarnings("unchecked")
- public void testCreateDistributionProviderService() {
+ public void testCreateDistributionProviderService() throws Exception {
IMocksControl control = EasyMock.createNiceControl();
Bundle b = control.createMock(Bundle.class);
@@ -130,4 +132,50 @@
assertNotNull(serviceProps.get(DistributionProvider.PROP_KEY_PRODUCT_VERSION));
assertNotNull(serviceProps.get(DistributionProvider.PROP_KEY_VENDOR_NAME));
}
+
+ public void testPublishPreexistingServices() throws Exception {
+ TestService serviceObject = new TestServiceImpl();
+
+ IMocksControl control = EasyMock.createNiceControl();
+
+ Bundle b = control.createMock(Bundle.class);
+ Hashtable<String, String> ht = new Hashtable<String, String>();
+ EasyMock.expect(b.getHeaders()).andReturn(ht).anyTimes();
+
+ BundleContext bc = control.createMock(BundleContext.class);
+
+ ServiceReference sref = control.createMock(ServiceReference.class);
+ EasyMock.expect(sref.getBundle()).andReturn(b).anyTimes();
+ EasyMock.expect(sref.getProperty(org.osgi.framework.Constants.OBJECTCLASS)).
+ andReturn(new String [] {TestService.class.getName()}).anyTimes();
+ EasyMock.expect(sref.getPropertyKeys()).
+ andReturn(new String [] {"osgi.remote.interfaces"}).anyTimes();
+ EasyMock.expect(sref.getProperty("osgi.remote.interfaces")).
+ andReturn("*").anyTimes();
+
+ EasyMock.expect(bc.getServiceReferences(null, "(osgi.remote.interfaces=*)")).
+ andReturn(new ServiceReference[] {sref}).anyTimes();
+ EasyMock.expect(bc.getService(sref)).andReturn(serviceObject).anyTimes();
+
+ EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes();
+ EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+ control.replay();
+
+ Activator a = new Activator() {
+ @Override
+ IntentMap getIntentMap() {
+ IntentMap intentMap = new IntentMap();
+ intentMap.setIntents(new HashMap<String, Object>());
+ return intentMap;
+ }
+ };
+
+ a.start(bc);
+ assertEquals(1, a.pHook.getEndpoints().size());
+ ServiceReference key = a.pHook.getEndpoints().keySet().iterator().next();
+ assertSame(sref, key);
+
+ }
+
+ private static class TestServiceImpl implements TestService {}
}
Modified: cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHookTest.java
URL: http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHookTest.java?rev=733025&r1=733024&r2=733025&view=diff
==============================================================================
--- cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHookTest.java
(original)
+++ cxf/sandbox/dosgi/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/hooks/CxfPublishHookTest.java
Fri Jan 9 05:03:48 2009
@@ -18,6 +18,7 @@
*/
package org.apache.cxf.dosgi.dsw.hooks;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -30,6 +31,7 @@
import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
import org.apache.cxf.dosgi.dsw.hooks.CxfPublishHook;
import org.apache.cxf.endpoint.Server;
+import org.easymock.IAnswer;
import org.easymock.classextension.EasyMock;
import org.easymock.classextension.IMocksControl;
import org.junit.Assert;
@@ -130,7 +132,64 @@
String addrProp =
org.apache.cxf.dosgi.dsw.Constants.POJO_ADDRESS_PROPERTY;
assertEquals(addresses[i], sd.getProperties().get(addrProp));
- }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testPublishMultipleTimes() {
+ Bundle bundle = control.createMock(Bundle.class);
+ bundle.findEntries(EasyMock.eq("OSGI-INF/remote-services"),
+ EasyMock.eq("*.xml"), EasyMock.anyBoolean());
+ EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+ public Object answer() throws Throwable {
+ return Collections.enumeration(Arrays.asList(
+ getClass().getResource("/OSGI-INF/remote-services/remote-services.xml")));
+ }
+ }).anyTimes();
+ Dictionary<String, String> bundleHeaders = new Hashtable<String, String>();
+ bundleHeaders.put(org.osgi.framework.Constants.BUNDLE_NAME,
+ "org.apache.cxf.example.bundle");
+ bundleHeaders.put(org.osgi.framework.Constants.BUNDLE_VERSION,
+ "1.0.0");
+ bundle.getHeaders();
+ EasyMock.expectLastCall().andReturn(bundleHeaders).anyTimes();
+ BundleContext requestingContext = control.createMock(BundleContext.class);
+ bundle.getBundleContext();
+ EasyMock.expectLastCall().andReturn(requestingContext).anyTimes();
+
+ TestService serviceObject = new TestServiceImpl();
+ Dictionary serviceProps = new Hashtable();
+
+ ServiceReference sref = control.createMock(ServiceReference.class);
+ sref.getBundle();
+ EasyMock.expectLastCall().andReturn(bundle).anyTimes();
+ sref.getProperty(Constants.OBJECTCLASS);
+ String [] serviceNames = {TestService.class.getName()};
+ EasyMock.expectLastCall().andReturn(serviceNames).anyTimes();
+ sref.getPropertyKeys();
+ EasyMock.expectLastCall().andReturn(new String[]{}).anyTimes();
+
+ BundleContext dswContext = new BundleTestContext(bundle);
+ dswContext.registerService(serviceNames, serviceObject, serviceProps);
+
+ final Server server = control.createMock(Server.class);
+ control.replay();
+
+ CxfPublishHook hook = new CxfPublishHook(dswContext, null) {
+ @Override
+ Server createServer(ServiceReference sref, ServiceEndpointDescription sd) {
+ return server;
+ }
+ };
+ assertNull("Precondition not met", hook.getEndpoints().get(sref));
+ hook.publishEndpoint(sref);
+ assertEquals(1, hook.getEndpoints().get(sref).size());
+
+ hook.endpoints.put(sref, new ArrayList<EndpointInfo>());
+ assertEquals("Precondition failed", 0, hook.getEndpoints().get(sref).size());
+ hook.publishEndpoint(sref);
+ assertEquals(0, hook.getEndpoints().get(sref).size());
}
private static class TestPublishHook extends CxfPublishHook {
|